注意:我使用的是Linux Rocky8.6版本,兼容部分CentOS版本,Ubuntu和其他OS版本,命令、文件路径、相关环境版本需要参考官方文档。
Nginx安装部署
-
安装Nginx软件的方式有很多种,分为如下几种
-
1.源码编译=>Nginx (1.版本随意 2.安装复杂 3.升级繁琐)
-
2.epel仓库=>Nginx (1.版本较低 2.安装简单 3.配置不易读)
-
3.官方仓库=>Nginx (1.版本较新 2.安装简单 3.配置易读,强烈推荐)
-
安装Nginx依赖
# yum install -y gcc gcc-c++ autoconf pcre pcre-devel make automake httpd-tools yum-utils
配置Nginx源
# vim /etc/yum.repos.d/nginx.repo [nginx-stable] name=nginx stable repo baseurl=http://nginx.org/packages/centos/$releasever/$basearch/ gpgcheck=1 enabled=1m gpgkey=https://nginx.org/keys/nginx_signing.key module_hotfixes=true
登录Nginx官网 nginx news
拉到底部,点击stable and mainline(稳定与开发版本)
选择安装系统
配置官网源信息
安装Nginx服务
# 清除yum缓存,为了防止从默认的epel源安装Nginx需要清除yum缓存 [root@rocky8 ~]#yum clean all # 重新缓存yum源仓库 [root@rocky8 ~]#yum makecache # 列出所有Nginx版本 [root@Web01-Nginx ~]#yum list nginx --showduplicates Last metadata expiration check: 0:06:04 ago on Fri 16 Sep 2022 08:22:14 PM CST. Available Packages nginx.x86_64 1.16.0-1.el8.ngx nginx-stable nginx.x86_64 1:1.14.1-9.module+el8.4.0+542+81547229 AppStream nginx.x86_64 1:1.16.1-1.el8.ngx nginx-stable nginx.x86_64 1:1.18.0-1.el8.ngx nginx-stable nginx.x86_64 1:1.18.0-2.el8.ngx nginx-stable nginx.x86_64 1:1.20.0-1.el8.ngx nginx-stable nginx.x86_64 1:1.20.1-1.el8.ngx nginx-stable nginx.x86_64 1:1.20.2-1.el8.ngx nginx-stable nginx.x86_64 1:1.22.0-1.el8.ngx nginx-stabl # 安装指定版本1.20,也可以直接yum install nginx -y(默认安装最新版本,这里为1.22.0版本) [root@Web01-Nginx ~]#yum -y install nginx-1.20.0 # 查看Nginx版本 [root@Web01-Nginx ~]#nginx -V nginx version: nginx/1.22.0 # 关闭apache服务httpd,禁止开机启动,否则Nginx会与apache冲突无法启动(如果没安装可不做) [root@rocky8 ~]#systemctl stop httpd [root@rocky8 ~]#systemctl disable httpd # 启动Nginx并设置开机自启动 [root@Web01-Nginx ~]#systemctl enable --now nginx # 查看Nginx状态,显示Active: active (running)表示已经启动 [root@Web01-Nginx ~]#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 Fri 2022-09-16 21:40:42 CST; 1s ago Docs: http://nginx.org/en/docs/ Process: 8259 ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf (code=exited, status=0/SUCCESS) Main PID: 8260 (nginx) Tasks: 3 (limit: 11175) Memory: 3.0M CGroup: /system.slice/nginx.service ├─8260 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf ├─8261 nginx: worker process └─8262 nginx: worker process Sep 16 21:40:42 rocky8.wang.org systemd[1]: Starting nginx - high performance web server... Sep 16 21:40:42 rocky8.wang.org systemd[1]: nginx.service: Can't open PID file /var/run/nginx.pid (yet?> Sep 16 21:40:42 rocky8.wang.org systemd[1]: Started nginx - high performance web server. # 查看进程端口,也可以用ss -nltp [root@rocky8 ~]#netstat -ltnp Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1051/sshd tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1505/master tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 8260/nginx: master tcp6 0 0 :::22 :::* LISTEN 1051/sshd tcp6 0 0 ::1:25 :::* LISTEN 1505/master # 查看master和worker进程,及user [root@rocky8 ~]#ps -ef | grep nginx root 8260 1 0 21:40 ? 00:00:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf nginx 8261 8260 0 21:40 ? 00:00:00 nginx: worker process nginx 8262 8260 0 21:40 ? 00:00:00 nginx: worker process root 8369 8272 0 22:09 pts/0 00:00:00 grep --color=auto nginx
访问Nginx服务
# 查看本机IP [root@rocky8 ~]#ip a 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000 link/ether 00:0c:29:d4:9c:21 brd ff:ff:ff:ff:ff:ff inet 10.0.0.204/24 brd 10.0.0.255 scope global noprefixroute eth0 valid_lft forever preferred_lft forever inet6 fe80::20c:29ff:fed4:9c21/64 scope link valid_lft forever preferred_lft forever # 输入Linux浏览器命令访问本机IP地址显示nginx页面信息 [root@rocky8 ~]#curl 10.0.0.204 <!DOCTYPE html> <html> <head> <title>Welcome to nginx!</title> <style> html { color-scheme: light dark; } body { width: 35em; margin: 0 auto; font-family: Tahoma, Verdana, Arial, sans-serif; } </style> </head> <body> <h1>Welcome to nginx!</h1> <p>If you see this page, the nginx web server is successfully installed and working. Further configuration is required.</p> <p>For online documentation and support please refer to <a href="http://nginx.org/">nginx.org</a>.<br/> Commercial support is available at <a href="http://nginx.com/">nginx.com</a>.</p> <p><em>Thank you for using nginx.</em></p> </body> </html>
windows浏览器输入本机IP地址显示Nginx页面
检查Nginx版本
# 检查版本 # nginx -v nginx version: nginx/1.20.1 # 检查编译参数 # nginx -V
查看Nginx目录结构
# rpm -ql nginx
Nginx主配置文件
路径 | 类型 | 作用 |
---|---|---|
/etc/nginx/nginx.conf | 配置 文件 | nginx主配 置文件 |
/etc/nginx/conf.d/default.conf | 配置 文件 | 默认网站配 置文件 |
Nginx代理配置文件
路径 | 类型 | 作用 |
---|---|---|
/etc/nginx/fastcgi_params | 配置 文件 | Fastcgi代理配 置文件 |
/etc/nginx/scgi_params | 配置 文件 | scgi代理配置 文件 |
/etc/nginx/uwsgi_params | 配置 文件 | uwsgi代理配 置文件 |
Nginx编码配置文件
路径 | 类型 | 作用 |
---|---|---|
/etc/nginx/win-utf | 配置 文件 | Nginx编码转换映射文件 |
/etc/nginx/koi-utf | 配置 文件 | Nginx编码转换映射文件 |
/etc/nginx/koi-win | 配置 文件 | Nginx编码转换映射文件 |
/etc/nginx/mime.types | 配置 文件 | Content-Type与扩展名 |
Nginx管理命令文件
路径 | 类 型 | 作用 |
---|---|---|
/usr/sbin/nginx | 命 令 | Nginx命令行管理终端工 具 |
/usr/sbin/nginx debug | 命 令 | Nginx命令行与终端调试 工具 |
Nginx日志相关文件
路径 | 类型 | 作用 |
---|---|---|
/var/log/nginx | 目录 | Nginx默认存放日志目录 |
/etc/logrotate.d/nginx | 配置 文件 | Nginx默认的日志切割 |