官方链接
nginx newshttp://nginx.org/选择版本
这里我选择安装1.20.2版本
[root@rocky8 ~]#wget http://nginx.org/download/nginx-1.20.2.tar.gz
[root@rocky8 ~]#tar xf nginx-1.20.2.tar.gz
安装依赖编译的软件
[root@rocky8 ~]#yum -y install gcc pcre-devel openssl-devel zlib-devel
创建用户
[root@rocky8 ~]#useradd -s /sbin/nologin nginx
./config
[root@rocky8 ~]#cd /root/nginx-1.20.2
[root@rocky8 nginx-1.20.2]#./configure --prefix=/apps/nginx \
--user=nginx \
--group=nginx \
--with-http_ssl_module \
--with-http_v2_module \
--with-http_realip_module \
--with-http_stub_status_module \
--with-http_gzip_static_module \
--with-pcre \
--with-stream \
--with-stream_ssl_module \
--with-stream_realip_module
make && make install
[root@rocky8 nginx-1.20.2]#make -j 2 && make install
#一般编译报错需要安装以下软件
yum -y install gcc automake autoconf libtool make
修改权限
[root@rocky8 nginx-1.20.2]#chown nginx.nginx /apps/nginx
启动服务
[root@rocky8 ~]#/apps/nginx/sbin/nginx
[root@rocky8 ~]#ss -ntl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:80 0.0.0.0:*
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 [::]:22
用service文件启动
#创建service文件需要修改配置文件
[root@rocky8 ~]# mkdir /apps/nginx/run
[root@rocky8 ~]#vim /apps/nginx/conf/nginx.conf
pid /apps/nginx/run/nginx.pid;
[root@rocky8 ~]#vim /usr/lib/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=/apps/nginx/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.
# https://bugzilla.redhat.com/show_bug.cgi?id=1268621
ExecStartPre=/usr/bin/rm -f /apps/nginx/run/nginx.pid
ExecStartPre=/apps/nginx/sbin/nginx -t
ExecStart=/apps/nginx/sbin/nginx
ExecReload=/bin/kill -s HUP $MAINPID
KillSignal=SIGQUIT
TimeoutStopSec=5
KillMode=mixed
PrivateTmp=true
[Install]
WantedBy=multi-user.target
[root@rocky8 ~]#systemctl daemon-reload
[root@rocky8 ~]#systemctl enable --now nginx
[root@rocky8 ~]#ss -ntl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:80 0.0.0.0:*
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 [::]:22 [::]:*