1、下载Nginx
[root@localhost ~]# wget -c https://nginx.org/download/nginx-1.24.0.tar.gz
2、解压
[root@localhost ~]# tar xf nginx-1.24.0.tar.gz -C /usr/local/src/
3、安装依赖
[root@localhost ~]# yum install gcc gcc-c++ make pcre-devel openssl-devel -y
4、 准备 Nginx 的编译环境,通过./configure 脚本,定义Nginx 安装后的根目录、可执行文件的位置、访问日志和错误日志的路径,以及主进程 PID 文件的存储位置
[root@localhost ~]# cd /usr/local/src/nginx-1.24.0/
[root@localhost nginx-1.24.0]# mkdir -p /var/log/nginx
[root@localhost nginx-1.24.0]# ./configure --prefix=/usr/local/nginx \
> --sbin-path=/usr/sbin/nginx \
> --http-log-path=/var/log/nginx/access.log \
> --error-log-path=/var/log/nginx/error.log \
> --pid-path=/run/nginx.pid
5、编译安装
[root@localhost nginx-1.24.0]# make
[root@localhost nginx-1.24.0]# make install
6、编写服务脚本
[root@localhost ~]# vim /etc/systemd/system/nginx.service
[root@localhost ~]# cat /etc/systemd/system/nginx.service
[Unit]
Description=The nginx HTTP and reverse proxy server
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
PIDFile=/run/nginx.pid
# Nginx will fail to start if /run/nginx.pid already exists but has the wrong
# SELinux context. This might happen when running `nginx -t` from the cmdline.
ExecStartPre=/usr/bin/rm -f /run/nginx.pid
ExecStartPre=/usr/sbin/nginx -t
ExecStart=/usr/sbin/nginx
ExecReload=/bin/kill -s HUP $MAINPID
KillSignal=SIGQUIT
TimeoutStopSec=5
KillMode=mixed
PrivateTmp=true
[Install]
WantedBy=multi-user.target
7、重新加载systemd配置,启用并启动服务
[root@localhost ~]# systemctl daemon-reload
[root@localhost ~]# systemctl enable nginx.service
Created symlink /etc/systemd/system/multi-user.target.wants/nginx.service → /etc/syste md/system/nginx.service.
[root@localhost ~]# systemctl start nginx.service
8、检查服务状态
9、测试nginx响应
或者在浏览器访问http:IP,出现以下页面则安装成功
10、测试重载和停止服务
[root@localhost ~]# systemctl reload nginx.service
[root@localhost ~]# systemctl stop nginx.service