[root@centos8 ~]#yum -y install gcc make gcc-c++ libtool pcre pcre-devel zlib zlib-devel openssl openssl-devel perl-ExtUtils-Embed
[root@centos8 ~]#useradd -s /sbin/nologin nginx
[root@centos8 ~]#cd /usr/local/src/
[root@centos8 src]#wget https://nginx.org/download/nginx-1.24.0.tar.gz
[root@centos8 src]#tar xf nginx-1.24.0.tar.gz
[root@centos8 src]#cd nginx-1.24.0/
[root@centos8 nginx-1.24.0]#./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
[root@centos8 nginx-1.24.0]#make && make install
[root@centos8 nginx-1.24.0]#chown -R nginx.nginx /apps/nginx
[root@centos8 nginx-1.24.0]#ll /apps/nginx/
total 0
drwxr-xr-x 2 root root 333 Sep 22 12:49 conf
drwxr-xr-x 2 root root 40 Sep 22 12:49 html
drwxr-xr-x 2 root root 6 Sep 22 12:49 logs
drwxr-xr-x 2 root root 19 Sep 22 12:49 sbin
#conf:保存nginx所有的配置文件,其中nginx.conf是nginx服务器的最核心最主要的配置文件,其它的.conf则是用来配置nginx相关的功能的,例如fastcgi功能使用的是fastcgi.conf和
fastcgi_params两个文件,配置文件一般都有一个样板配置文件,是以.default为后缀,使用时可将其复
制并将default后缀去掉即可。
#html目录中保存了nginx服务器的web文件,但是可以更改为其他目录保存web文件,另外还有一个50x的
web文件是默认的错误页面提示页面。
#logs:用来保存nginx服务器的访问日志错误日志等日志,logs目录可以放在其他路径,比
如/var/logs/nginx里面。
#sbin:保存nginx二进制启动脚本,可以接受不同的参数以实现不同的功能。
[root@centos8 nginx-1.24.0]#ls /apps/nginx/sbin/
nginx
[root@centos8 nginx-1.24.0]#ln -s /apps/nginx/sbin/nginx /usr/sbin/
#查看版本
[root@centos8 ~]#nginx -v
nginx version: nginx/1.24.0
#查看编译参数
[root@centos8 ~]#nginx -V
nginx version: nginx/1.24.0
built by gcc 8.5.0 20210514 (Red Hat 8.5.0-18) (GCC)
built with OpenSSL 1.1.1k FIPS 25 Mar 2021
TLS SNI support enabled
configure arguments: --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
[root@centos8 ~]#nginx
[root@centos8 ~]# ss -tnlp | grep nginx
LISTEN 0 511 0.0.0.0:80 0.0.0.0:* users:(("nginx",pid=49176,fd=6),("nginx",pid=49175,fd=6))
[root@centos8 ~]#nginx -s stop
[root@centos8 ~]#ss -tnlp | grep nginx
~
[root@centos8 ~]#vim /lib/systemd/system/nginx.service
#在文件里插入以下配置
[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target
[Service]
Type=forking
#指定pid文件的目录,默认在logs目录下,可选配置
PIDFile=/apps/nginx/run/nginx.pid
ExecStart=/apps/nginx/sbin/nginx -c /apps/nginx/conf/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s TERM $MAINPID
LimitNOFILE=100000
[Install]
WantedBy=multi-user.target
[root@centos8 ~]#mkdir /apps/nginx/run/
[root@centos8 ~]#vim /apps/nginx/conf/nginx.conf
pid /apps/nginx/run/nginx.pid;
[root@centos8 ~]#systemctl daemon-reload
[root@centos8 ~]#systemctl enable --now nginx
Created symlink /etc/systemd/system/multi-user.target.wants/nginx.service →
[root@centos8 ~]#ll /apps/nginx/run/
total 4
-rw-r--r-- 1 root root 5 Sep 22 13:01 nginx.pid
[root@centos8 ~]# ss -tnlp | grep nginx
LISTEN 0 511 0.0.0.0:80 0.0.0.0:* users:(("nginx",pid=49576,fd=6),("nginx",pid=49575,fd=6))
[root@centos8 ~]#systemctl stop nginx
[root@centos8 ~]#systemctl status nginx
● nginx.service - nginx - high performance web server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
Active: active (running) since Sat 2023-07-29 11:52:49 CST; 4s ago
Docs: http://nginx.org/en/docs/
Process: 49516 ExecStop=/bin/kill -s TERM $MAINPID (code=exited, status=1/FAILURE)
Process: 49661 ExecStart=/apps/nginx/sbin/nginx -c /apps/nginx/conf/nginx.conf (code=exited, status=0/SUCCESS)
Main PID: 49662 (nginx)
Tasks: 2 (limit: 10815)
Memory: 1.8M
CGroup: /system.slice/nginx.service
├─49662 nginx: master process /apps/nginx/sbin/nginx -c /apps/nginx/conf/nginx.conf
└─49663 nginx: worker process
7月 29 11:52:49 suian systemd[1]: Starting nginx - high performance web server...
7月 29 11:52:49 suian systemd[1]: nginx.service: Can't open PID file /apps/nginx/run/nginx.pid (yet?) after start: No such file or directo>
7月 29 11:52:49 suian systemd[1]: Started nginx - high performance web server.
到此,使用编译安装nginx就完成了。
官方源码包下载地址
nginx: download
有问题的小伙伴可以评论留言或者私信我都可以帮忙答疑解惑哦。