1.下载编译安装相关依赖包
yum -y install gcc pcre-devel openssl-devel zlib-devel
2.下载nginx二进制包:nginx-1.20.2.tar.gz(或通过nignx官网进行下载,再拖拽到linux)
[root@localhost ~] wget https://nginx.org/download/nginx-1.20.2.tar.gz
3.对下载的nginx包进行解压缩
#解压缩
[root@localhost ~]#tar xf nginx-1.20.2.tar.gz
[root@localhost nginx]# ls
nginx-1.20.2 nginx-1.20.2.tar.gz
4.创建nginx用户进行管理
[root@localhost ~]#useradd -s /sbin/nologin nginx
5.进行编译,指定安装目录,用户,相关模块
[root@localhost 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
6.安装
[root@localhost nginx-1.20.2]# make && make install
7.验证版本及编译参数
[root@localhost nginx]# cd /apps/nginx/sbin/
[root@localhost nginx]# ls /apps/nginx/sbin/
nginx
[root@142 sbin]# ./nginx -v
nginx version: nginx/1.20.2
[root@142 sbin]# ./nginx -V
nginx version: nginx/1.20.2
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
8.创建nginx启动文件
vi /usr/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
PIDFile=/apps/nginx/run/nginx.pid #指定pid文件的目录,默认在logs目录下,可选配置
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
9.创建相关文件
如果启动失败,请检查日志 tail -f /var/log/messages ,根据提示信息,创建文件
[root@localhost ~]mkdir /apps/nginx/run/
[root@localhost ~]touch /apps/nginx/run/nginx.pid
[root@localhost ~]vi /apps/nginx/conf/nginx.conf
#指定nginx.pid目录
pid /apps/nginx/run/nginx.pid;
10.加载system配置
[root@localhost ~]systemctl daemon-reload
11.测试-使用systemctl启动nginx服务
[root@localhost ~]systemctl start nginx
12.检查nginx启动进程,此时状态正常(如果启动失败请从第8步开始向下逐一检查)
[root@localhost ~]# ps -ef | grep nginx
root 4610 1 0 10:57 ? 00:00:00 nginx: master process /apps/nginx/sbin/nginx -c /apps/nginx/conf/nginx.conf
nginx 4611 4610 0 10:57 ? 00:00:00 nginx: worker process
root 4625 4508 0 10:57 pts/1 00:00:00 grep --color=auto nginx
[root@142 ~]# cat /apps/nginx/run/nginx.pid
4610
13.测试访问,使用浏览器进行访问,效果如下图(该IP地址为你测试的地址)
http://192.168.92.142/
14.使用system命令,正常管理nginx
[root@localhost ~]# 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 Mon 2024-04-22 12:32:23 CST; 2min 27s ago
Docs: http://nginx.org/en/docs/
Process: 979 ExecStart=/apps/nginx/sbin/nginx -c /apps/nginx/conf/nginx.conf (code=exited, status=0/SUCCESS)
Main PID: 987 (nginx)
CGroup: /system.slice/nginx.service
├─987 nginx: master process /apps/nginx/sbin/nginx -c /apps/nginx/conf/nginx.conf
└─991 nginx: worker process
Apr 22 12:32:23 142 systemd[1]: Starting nginx - high performance web server...
Apr 22 12:32:23 142 systemd[1]: Can't open PID file /apps/nginx/run/nginx.pid (yet?) after start: No such file or directory
Apr 22 12:32:23 142 systemd[1]: Started nginx - high performance web server.
[root@localhost ~]# systemctl stop nginx
[root@localhost ~]# systemctl start nginx
[root@localhost ~]# systemctl enable --now nginx
15.完成。