下载地址: https://nginx.org/en/download.html
选择稳定版本:Stable version中的 nginx-1.22.0(版本以后还会变,根据当前版本下载即可)
上传到/usr/local
目录下,进入到该目录下,解压压缩包
tar -zxvf nginx-1.22.0.tar.gz
[root@Captian local]# tar -zxvf nginx-1.22.0.tar.gz
nginx-1.22.0/
nginx-1.22.0/auto/
nginx-1.22.0/conf/
nginx-1.22.0/contrib/
nginx-1.22.0/src/
nginx-1.22.0/configure
进入到解压文件根目录,执行命令行,检查安装环境
./configure --prefix=/usr/local/nginx
演示:
[root@Captian nginx-1.22.0]# ./configure --prefix=/usr/local/nginx
checking for OS
+ Linux 4.18.0-348.7.1.el8_5.x86_64 x86_64
checking for C compiler ... not found
./configure: error: C compiler cc is not found
[root@Captian nginx-1.22.0]#
yum install -y gcc
演示:
[root@Captian /]# yum install -y gcc
Last metadata expiration check: 2:28:53 ago on Fri 12 Aug 2022 02:23:13 PM CST.
Dependencies resolved.
=============================================================================================================================================================
Package Architecture Version Repository Size
=============================================================================================================================================================
Installing:
......
yum install -y pcre pcre-devel
演示:
[root@Captian /]# yum install -y pcre pcre-devel
Last metadata expiration check: 2:35:09 ago on Fri 12 Aug 2022 02:23:13 PM CST.
Package pcre-8.42-4.el8.x86_64 is already installed.
Dependencies resolved.
......
yum install -y zlib zlib-devel
演示:
[root@Captian /]# yum install -y zlib zlib-devel
Last metadata expiration check: 2:48:44 ago on Fri 12 Aug 2022 02:23:13 PM CST.
Package zlib-1.2.11-13.el8.x86_64 is already installed.
Dependencies resolved.
yum install -y openssl openssl-devel
演示:
[root@Captian /]# yum install -y openssl openssl-devel
Last metadata expiration check: 2:49:43 ago on Fri 12 Aug 2022 02:23:13 PM CST.
Package openssl-1:1.1.1k-5.el8_5.x86_64 is already installed.
Dependencies resolved.
再执行./configure --prefix=/usr/local/nginx
将页面拉到最后几十行,如下所示即OK
Configuration summary
+ using system PCRE2 library
+ OpenSSL library is not used
+ using system zlib library
nginx path prefix: "/usr/local/nginx"
nginx binary file: "/usr/local/nginx/sbin/nginx"
nginx modules path: "/usr/local/nginx/modules"
nginx configuration prefix: "/usr/local/nginx/conf"
nginx configuration file: "/usr/local/nginx/conf/nginx.conf"
nginx pid file: "/usr/local/nginx/logs/nginx.pid"
nginx error log file: "/usr/local/nginx/logs/error.log"
nginx http access log file: "/usr/local/nginx/logs/access.log"
nginx http client request body temporary files: "client_body_temp"
nginx http proxy temporary files: "proxy_temp"
nginx http fastcgi temporary files: "fastcgi_temp"
nginx http uwsgi temporary files: "uwsgi_temp"
nginx http scgi temporary files: "scgi_temp"
[root@Captian nginx-1.22.0]#
make
演示:
[root@Captian nginx-1.22.0]# make
make -f objs/Makefile
make[1]: Entering directory '/usr/local/nginx-1.22.0'
cc -c -pipe -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g -I src/core -I src/event -I src/event/modules -I src/os/unix -I objs \
-o objs/src/core/nginx.o \
src/core/nginx.c
cc -c -pipe -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g -I src/core -I src/event -I src/event/modules -I src/os/unix -I objs \
-o objs/src/core/ngx_log.o \
src/core/ngx_log.c
cc -c -pipe -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g -I src/core -I src/event -I src/event/modules -I src/os/unix -I objs \
-o objs/src/core/ngx_palloc.o \
src/core/ngx_palloc.c
make install
演示:
[root@Captian nginx-1.22.0]# make
make -f objs/Makefile
make[1]: Entering directory '/usr/local/nginx-1.22.0'
cc -c -pipe -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g -I src/core -I src/event -I src/event/modules -I src/os/unix -I objs \
-o objs/src/core/nginx.o \
进入安装好的目录/usr/local/nginx/sbin
./nginx 启动
./nginx -s stop 快速停止
./nginx -s quit 优雅关闭,在退出前完成已经接受的连接请求
./nginx -s reload 重新加载配置
//查看nginx进程
ps -ef|grep nginx
查看虚拟机IP地址ip addr
,打开浏览器访问ip
若不能访问,请关掉防火墙
放行端口
firewall-cmd --zone=public --add-port=80/tcp --permanent
重启防火墙
firewall-cmd --reload
创建服务脚本
vim /usr/lib/systemd/system/nginx.service
服务内容
[Unit]
#必须加守护神不然会报错
Description=nginx -web server
# 指定启动nginx之前需要其他的其他服务,如network.target等
After=network.target remote-fs.target nss-lookup.target
[Service]
# Type为服务的类型,仅启动一个主进程的服务为simple,需要启动若干子进程的服务为forking
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
# 设置执行systemctl start nginx后需要启动的具体命令。
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
# 设置执行systemctl reload nginx后需要执行的具体命令。
ExecReload=/usr/local/nginx/sbin/nginx -s reload
# 设置执行systemctl stop nginx后需要执行的具体命令。
ExecStop=/usr/local/nginx/sbin/nginx -s stop
ExecQuit=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true
[Install]
# 设置在什么模式下被安装,设置开机启动的时候需要有这个。
WantedBy=multi-user.target
重新加载系统服务
systemctl daemon-reload
查看nginx是否在运行
ps -ef | grep nginx
关闭
./nginx -s stop
启动服务
systemctl start nginx.service
开机启动服务:
systemctl enable nginx
检查是否开机启动:
systemctl is-enabled nginx
[root@Captian ~]# systemctl is-enabled nginx
enabled
会发现命令有做一个软连接,这个就是为了开机启动:
Created symlink /etc/systemd/system/multi-user.target.wants/nginx.service → /usr/lib/systemd/system/nginx.service.
以后使用一下命令即可:
启动服务:systemctl start nginx
关闭服务: systemctl stop nginx
重载配置:systemctl reload nginx
重启服务:systemctl restart nginx
开机启动服务:systemctl enable nginx