背景
最近,全球都在制裁 Nginx
, ClickHouse
的诞生地。以前都是通过 yum
直接安装的 Nginx
,今天试试源码安装。
系统环境
在 CentOS7
上进行安装,虚拟主机信息如下:
[root@hadoop1 local]# uname -a Linux hadoop1 3.10.0-1127.el7.x86_64 #1 SMP Tue Mar 31 23:36:51 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux [root@hadoop1 local]# cat /proc/version Linux version 3.10.0-1127.el7.x86_64 (mockbuild@kbuilder.bsys.centos.org) (gcc version 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC) ) #1 SMP Tue Mar 31 23:36:51 UTC 2020 [root@hadoop1 local]# cat /etc/redhat-release CentOS Linux release 7.8.2003 (Core)
下载解压
# 下载 [root@hadoop1 local]# wget http://nginx.org/download/nginx-1.20.1.tar.gz # 解压 [root@hadoop1 local]# tar -xvf nginx-1.20.1.tar.gz
编译安装
[root@hadoop1 local]# cd nginx-1.20.1 [root@hadoop1 nginx-1.20.1]# ./configure [root@hadoop1 nginx-1.20.1]# make [root@hadoop1 nginx-1.20.1]# make install # 配置环境变量 [root@hadoop1 nginx-1.20.1]# nginx -V -bash: nginx: 未找到命令 [root@hadoop1 nginx-1.20.1]# vi /etc/profile export NGINX_HOME=/usr/local/nginx export PATH=$PATH:$NGINX_HOME/sbin # 刷新配置 [root@hadoop1 nginx-1.20.1]# source /etc/profile [root@hadoop1 nginx-1.20.1]# nginx -V nginx version: nginx/1.20.1 built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC) configure arguments:
启动验证
# 启动 [root@hadoop1 nginx-1.20.1]# nginx -c /usr/local/nginx/conf/nginx.conf
通过80端口访问,看 Nginx
是否启动成功。
配置开机自启
# 这里是用源码编译安装的,所以需要手动创建nginx.service服务文件。 [root@hadoop1 nginx-1.20.1]# vi /lib/systemd/system/nginx.service [Unit] Description=nginx service After=network.target [Service] Type=forking ExecStart=/usr/local/nginx/sbin/nginx ExecReload=/usr/local/nginx/sbin/nginx -s reload ExecStop=/usr/local/nginx/sbin/nginx -s quit PrivateTmp=true [Install] WantedBy=multi-user.target
验证开机自启
[root@hadoop1 nginx-1.20.1]# systemctl list-unit-files | grep nginx nginx.service disabled [root@hadoop1 nginx-1.20.1]# systemctl enable nginx Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service. [root@hadoop1 nginx-1.20.1]# systemctl list-unit-files | grep nginx nginx.service enabled
常用命令
# Nginx启停 systemctl start nginx.service 启动nginx服务 systemctl stop nginx.service 停止服务 systemctl restart nginx.service 重新启动服务 systemctl status nginx.service 查看服务状态 systemctl enable nginx.service 设置开机自启动 systemctl disable nginx.service 取消开机自启动 # 查看开机启动项 systemctl list-unit-files systemctl list-unit-files | grep enabled systemctl list-unit-files | grep nginx
Note: 01. 修改完配置后,可通过nginx -t
测试配置是否存在语法错误或者typo; 02. 修改完配置后,记得通过nginx -s reload
刷新使配置生效;