戴尔电脑网站建设方案范文,网站做优化每天一定要更新,中国字体设计网,wordpress简介怎么改文章目录 1. Nginx 环境准备2. Nginx 安装和启动3. Nginx 常用命令4. Nginx 使用systemctl启动、停止、重新加载5. Nginx 配置文件6. Nginx 配置文件结构 1. Nginx 环境准备
虚拟机镜像#xff0c;基于Centos7#xff0c;网盘链接: https://pan.baidu.com/s/1NmCR-vdAcZLouR… 文章目录 1. Nginx 环境准备2. Nginx 安装和启动3. Nginx 常用命令4. Nginx 使用systemctl启动、停止、重新加载5. Nginx 配置文件6. Nginx 配置文件结构 1. Nginx 环境准备
虚拟机镜像基于Centos7网盘链接: https://pan.baidu.com/s/1NmCR-vdAcZLouRRn9V1yTA 密码: 1b60虚拟机的用户名/密码root/123456使用的环境和应用放置在/home目录下
目录简介说明AdminLTE-3.2.0静态web后台管理系统静态web的配置演示apps/ruoyi-admin.jar若依后台管理系统基于springboot方向代理演示apache-tomcat-8.5.81apps/ruoyi-admin.warjavaweb服务器动静分离演示sql/ry_20210924.sqlsql/quartz.sql数据库初始化脚本创建若依后台数据库mysql5.7数据库用户名/密码root/123456open-JDK 1.8java运行环境已安装
2. Nginx 安装和启动
① 关闭防火墙
sudo systemctl stop firewalld
sudo systemctl disable firewalld② 使用yum安装
sudo yum install yum-utils net-tools③ 在 CentOS 系统中创建一个名为 nginx.repo 的文件并将以下内容写入该文件
[nginx-stable]
namenginx stable repo
baseurlhttp://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck1
enabled1
gpgkeyhttps://nginx.org/keys/nginx_signing.key
module_hotfixestrue这个文件是一个 YUM 软件包管理器的仓库配置文件它指定了一个名为 nginx-stable 的仓库其中包含了稳定版本的 Nginx 软件包。这个仓库的 URL 是 http://nginx.org/packages/centos/ r e l e a s e v e r / releasever/ releasever/basearch/其中 $releasever 和 $basearch 是 CentOS 系统的变量会被替换为相应的值。此外这个仓库还启用了 GPG 检查并指定了 GPG 密钥的 URL。最后module_hotfixes 参数指定了是否启用模块热修复功能。
执行如下命令
cat /etc/yum.repos.d/nginx.repo EOF
[nginx-stable]
namenginx stable repo
baseurlhttp://nginx.org/packages/centos/\$releasever/\$basearch/
gpgcheck1
enabled1
gpgkeyhttps://nginx.org/keys/nginx_signing.key
module_hotfixestrue
EOFsudo yum install nginx3. Nginx 常用命令
nginx #立即停止
nginx -s stop#执行完当前请求再停止
nginx -s quit#重新加载配置文件相当于restart
nginx -s reload#将日志写入一个新的文件
nginx -s reopen#测试配置文件
nginx -tNginx 日志位于/var/log/nginx/
4. Nginx 使用systemctl启动、停止、重新加载
systemctl start nginxsystemctl status nginx#产看日志
journalctl -xesystemctl stop nginxsystemctl reload nginx#配置开机启动
systemctl enable nginx① 在centos 7中用systemctl启动nginx可能出现如下错误
[rootnginx-dev ~]# systemctl start nginx
Job for nginx.service failed because the control process exited with error code. See systemctl status nginx.service and journalctl -xe for details.[rootnginx-dev ~]# systemctl status nginx
● nginx.service - nginx - high performance web serverLoaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)Active: failed (Result: exit-code) since Sun 2023-08-06 17:51:44 CST; 16s agoDocs: http://nginx.org/en/docs/Process: 2365 ExecStart/usr/sbin/nginx -c /etc/nginx/nginx.conf (codeexited, status1/FAILURE)Aug 06 17:51:44 nginx-dev systemd[1]: Starting nginx - high performance web server...
Aug 06 17:51:44 nginx-dev nginx[2365]: nginx: [emerg] bind() to 0.0.0.0:8000 failed (13: Permission denied)
Aug 06 17:51:44 nginx-dev systemd[1]: nginx.service: control process exited, codeexited status1
Aug 06 17:51:44 nginx-dev systemd[1]: Failed to start nginx - high performance web server.
Aug 06 17:51:44 nginx-dev systemd[1]: Unit nginx.service entered failed state.
Aug 06 17:51:44 nginx-dev systemd[1]: nginx.service failed.这是由于selinux的安全策略引起的。解决方法如下
setenforce 0 临时修改/etc/selinux/config设置SELINUXdisabled 永久有效需重启
② 启动 nginx 报错
[rootnginx-dev ~]# systemctl start nginx
Job for nginx.service failed because the control process exited with error code. See systemctl status nginx.service and journalctl -xe for details.[rootnginx-dev ~]# systemctl status nginx.service
● nginx.service - nginx - high performance web serverLoaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)Active: failed (Result: exit-code) since Sun 2023-08-06 17:56:39 CST; 26s agoDocs: http://nginx.org/en/docs/Process: 2403 ExecStart/usr/sbin/nginx -c /etc/nginx/nginx.conf (codeexited, status1/FAILURE)Aug 06 17:56:38 nginx-dev nginx[2403]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
Aug 06 17:56:38 nginx-dev nginx[2403]: nginx: [emerg] bind() to 0.0.0.0:8000 failed (98: Address already in use)
Aug 06 17:56:39 nginx-dev nginx[2403]: nginx: [emerg] still could not bind()
Aug 06 17:56:39 nginx-dev systemd[1]: nginx.service: control process exited, codeexited status1
Aug 06 17:56:39 nginx-dev systemd[1]: Failed to start nginx - high performance web server.
Aug 06 17:56:39 nginx-dev systemd[1]: Unit nginx.service entered failed state.
Aug 06 17:56:39 nginx-dev systemd[1]: nginx.service failed.这是一个nginx启动错误错误信息显示端口80已经被占用。这通常是因为另一个进程已经在监听该端口。您可以使用以下命令检查哪个进程正在使用该端口
[rootnginx-dev ~]# sudo lsof -i :80
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
nginx 2355 root 7u IPv4 21150 0t0 TCP *:http (LISTEN)
nginx 2356 nginx 7u IPv4 21150 0t0 TCP *:http (LISTEN)
nginx 2357 nginx 7u IPv4 21150 0t0 TCP *:http (LISTEN)这将显示所有正在使用端口80的进程的详细信息包括进程IDPID和进程名称。您可以使用此信息来杀死该进程例如可以通过杀死该进程
[rootnginx-dev ~]# sudo kill -9 2355
[rootnginx-dev ~]# sudo kill -9 2356
[rootnginx-dev ~]# sudo kill -9 2357 另一种解决此问题的方法是更改nginx配置文件中的端口。可以编辑nginx配置文件通常位于/etc/nginx/nginx.conf并将端口更改为未被占用的端口。然后重新启动nginx服务以使更改生效
[rootnginx-dev ~]# sudo service nginx restart
Redirecting to /bin/systemctl restart nginx.service
[rootnginx-dev ~]# systemctl status nginx
● nginx.service - nginx - high performance web serverLoaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)Active: active (running) since Sun 2023-08-06 18:07:52 CST; 23s agoDocs: http://nginx.org/en/docs/Process: 2482 ExecStart/usr/sbin/nginx -c /etc/nginx/nginx.conf (codeexited, status0/SUCCESS)Main PID: 2483 (nginx)CGroup: /system.slice/nginx.service├─2483 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf├─2484 nginx: worker process└─2485 nginx: worker processAug 06 18:07:52 nginx-dev systemd[1]: Starting nginx - high performance web server...
Aug 06 18:07:52 nginx-dev systemd[1]: Started nginx - high performance web server.5. Nginx 配置文件
nginx 配置文件位于 /etc/nginx/nginx.conf
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log notice;
pid /var/run/nginx.pid;events {worker_connections 1024;
}http {include /etc/nginx/mime.types;default_type application/octet-stream;log_format main $remote_addr - $remote_user [$time_local] $request $status $body_bytes_sent $http_referer $http_user_agent $http_x_forwarded_for;access_log /var/log/nginx/access.log main;sendfile on;keepalive_timeout 65;include /etc/nginx/conf.d/*.conf;
}include 命令会引用 /etc/nginx/conf.d 目录下所有的.conf文件这样可以保持主配置文件的简洁同时配个多个.conf文件方便区分增加可读性。
[rootnginx-dev conf.d]# cd /etc/nginx/conf.d
[rootnginx-dev conf.d]# ls
admin-8000.conf default.conf默认配置 /etc/nginx/conf.d/default.conf
server {listen 80;server_name localhost;location / {root /usr/share/nginx/html;index index.html index.htm;}error_page 500 502 503 504 /50x.html;location /50x.html {root /usr/share/nginx/html;}
}① 该配置文件定义了一个监听本地 80 端口的服务器服务器名为 localhost。location / 表示匹配所有以 / 开头的请求 URL也就是所有请求。
② index 指定了默认的首页文件当客户端请求一个目录时Nginx 会尝试返回该目录下的默认文档则会返回目录列表或者 403 Forbidden 错误。
index 指令的语法如下
index file1 [file2 ...];其中file1、file2 等参数表示默认文档的文件名多个文件名之间用空格分隔。Nginx 会按照指定的顺序依次尝试返回这些文件直到找到一个存在的文件为止。
例如以下配置指定了默认文档为 index.html
location / {index index.html;
}当客户端请求 / 目录时Nginx 会尝试返回 index.html 文件。如果该文件不存在则会返回目录列表或者 403 Forbidden 错误。
③ root 指令用于指定服务器上的根目录即 Nginx 服务器将从该目录中提供文件
[rootnginx-dev conf.d]# cd /usr/share/nginx/html
[rootnginx-dev html]# ll
total 8
-rw-r--r--. 1 root root 497 Apr 12 01:22 50x.html
-rw-r--r--. 1 root root 615 Apr 12 01:22 index.html该服务器的根目录为 /usr/share/nginx/html当访问该服务器时如果请求的路径为 /则返回 index.html 或 index.htm 文件。如果服务器出现 500、502、503 或 504 错误则会返回 /usr/share/nginx/html/50x.html 页面。
④ 访问本机的80端口将返回index.html文件
[rootnginx-dev ~]# curl 127.0.0.1:80
!DOCTYPE html
html
head
titleWelcome 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
h1Welcome to nginx!/h1
pIf you see this page, the nginx web server is successfully installed and
working. Further configuration is required./ppFor online documentation and support please refer to
a hrefhttp://nginx.org/nginx.org/a.br/
Commercial support is available at
a hrefhttp://nginx.com/nginx.com/a./ppemThank you for using nginx./em/p
/body
/html
[rootnginx-dev ~]#6. Nginx 配置文件结构
http {server{#虚拟主机location {listen 80server_name localhost;}location {}}server{}}参考课程文档地址https://www.yuque.com/wukong-zorrm/cql6cz/ofesua