记录一下 Linux(CentOS7) 下 Nginx 安装过程

一、准备工作

版本说明:

  • Linux版本:CentOS 7 64位
  • Nginx版本:nginx-1.20.0

1. 下载安装文件

采用源码包方式安装,当然使用 yum 方式安装也可以,此处使用源码包安装。

进入目录(个人习惯/usr/local),下载安装文件,如果云服务器下载速度过慢也可本地下载后上传。

cd /usr/local
wget http://nginx.org/download/nginx-1.20.0.tar.gz

2. 安装Nginx所需要的依赖

# 安装gcc、gcc-c++
yum -y install gcc
yum -y install gcc-c++ # 安装pcre 、zilb
yum -y install pcre*
yum -y install zlib* # 安装openssl(若需要支持 https 协议)
yum -y install openssl
yum -y install openssl-devel

二、安装及配置Nginx

1. 安装Nginx

1、解压安装包。

tar -zxvf nginx-1.20.0.tar.gz

2、为编译安装做准备,进入解压目录。

cd nginx-1.20.0

3、为编译安装做准备。

./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module --with-pcre

注:

--prefix:设置安装路径。

--with-http_stub_status_module:支持nginx状态查询。

--with-http_ssl_module:支持https。

--with-pcre:为了支持rewrite重写功能,必须制定pcre。

4、编译安装。

make && make install

5、指定配置文件启动 Nginx。

/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

在浏览器访问 ip,看到如下信息则安装成功。

Welcome to nginx!
If you see this page, the nginx web server is successfully installed and working. Further configuration is required. For online documentation and support please refer to nginx.org.
Commercial support is available at nginx.com. Thank you for using nginx.

2. 一些命令

修改自己的配置之后,验证配置是否正确,重启 Nginx 命令

# 进入目录
cd /usr/local/nginx/sbin # 验证配置是否正确
./nginx -t
# 如果看到如下内容, 那么配置正确, 可以重启Nginx
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful # 重启Nginx, 之后就可以通过域名访问了, 哈哈
./nginx -s reload

3. Nginx开机自启

# 新建文件
vim /lib/systemd/system/nginx.service # 添加内容
[Unit]
Description=nginx.server
After=network.target [Service]
Type=forking
PIDFILE=/var/run/nginx.pid
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecRepload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true [Install]
WantedBy=multi-user.target

然后使用 systemctl enable nginx.service 开启 nginx 开机自启,重启 CentOS 查看效果。

# 启动nginx服务
systemctl start nginx.service # 停止nginx服务
systemctl stop nginx.service # 重启nginx服务
systemctl restart nginx.service # 查看nginx服务当前状态
systemctl status nginx.service # 设置nginx服务开机自启动
systemctl enable nginx.service # 停止nginx服务开机自启动
systemctl disable nginx.service

结语:当然Nginx除了反向代理,还有负载均衡等功能,这个就不在本文章范围内了。

都读到这里了,来个 点赞、评论、关注、收藏 吧!

文章作者:IT王小二

首发地址:https://www.itwxe.com/posts/1334c88e/

版权声明:文章内容遵循 署名-非商业性使用-禁止演绎 4.0 国际 进行许可,转载请在文章页面明显位置给出作者与原文链接。

最新文章

  1. salesforce 零基础学习(五十八)通过sObject的field返回其对应的基础类型
  2. code complete part2
  3. Jquery 鼠标事件解析
  4. BZOJ 2436 Noi嘉年华(优化DP)
  5. Struts框架搭建时所遇到的问题
  6. socket通信_笔记
  7. Python简单爬虫记录
  8. Spring学习笔记——02 Bean的命名及实例化
  9. 201521123088《java程序设计》第四次总结
  10. http://codeforces.com/contest/349
  11. python通过webservice接口实现配置下发
  12. 以编程方式使用 Microsoft Office Visio 2003 ActiveX 控件
  13. securecrt 方向键乱码解决
  14. linux_基本命令使用(后续更新)
  15. kettle 6.1 日志查询
  16. spring mvc中关于url中传递中文乱码的解决方法
  17. webpack-dev-server将文件产出到指定目录
  18. tell me one of your favorite project-练习英语
  19. git pull 分支问题
  20. 缓存技术内部交流_04_Cache Aside续篇

热门文章

  1. day11.迭代器与生成器
  2. 在Bootstrap开发框架基础上增加WebApi+Vue&Element的前端
  3. 缓冲区溢出分析第10课:Winamp缓冲区溢出研究
  4. WinDbg 配置联机调试环境搭建
  5. 19.Vuex详细使用说明-一篇文章涵盖所有知识点
  6. gitlab + php自动部署
  7. 缓冲流以及JAVA路径相关问题
  8. c++学习的一些忠告(转载)
  9. NtQuerySystemInformation获取进程/线程状态
  10. Rabbit MQ一些参数解释