网站友情链接作用,一个人免费看直播,wordpress广告链接不跳转,wordpress通过编辑器修改页面宽度文章目录前言备份与停止服务nginx安装与配置nginx 编译安装配置服务php-fpm多站点配置phf-fpm介绍多站点配置nginx 多站点配置nginx ssl 配置参考前言
之前服务器使用的是 LAMP环境#xff0c;想充分利用服务器资源#xff0c;再运行另外一个站点
在LAMP环境下应该是也可以…
文章目录前言备份与停止服务nginx安装与配置nginx 编译安装配置服务php-fpm多站点配置phf-fpm介绍多站点配置nginx 多站点配置nginx ssl 配置参考前言
之前服务器使用的是 LAMP环境想充分利用服务器资源再运行另外一个站点
在LAMP环境下应该是也可以实现多站点
但考虑到nginx轻量性以及当前主要开发生产用到nginx比较多配置好后也方便学习
于是决定进行更换
这里面有一个多用户权限的问题最好统一基于已有的用户进行设置如www用户目前下面的方案能够实现想要的效果但可能在规范和安全上做的并不好
备份与停止服务
备份备份网站备份配置文件
# 备份网站目录假设默认路径是 /var/www/html
sudo cp -r /var/www/html /var/www/html_backup# 备份 Apache 配置文件
sudo cp -r /etc/apache2 /etc/apache2_backup # Debian/Ubuntu
sudo cp -r /etc/httpd /etc/httpd_backup # CentOS/RHEL停止 apache 服务
# 停止 apache服务
sudo systemctl stop apache2 # Ubuntu/Debian
sudo systemctl stop httpd # CentOS# 禁止开机自启
sudo systemctl disable apache2 # Ubuntu/Debian
sudo systemctl disable httpd # CentOS# 更老版本centos 会提示 httpd,service is not a native service, redirecting to sbin/chkconfid Executing /sbin/chkconfid httpd off
# 也是执行成功了# 检查是否禁用
/sbin/chkconfig --list httpd# httpd 0:off 1:off 2:off 3:off 4:off 5:off 6:off 全是off 禁止开机自启动成功# 如需卸载 可执行
# ubuntu/debian
sudo apt remove --purge apache2
sudo apt autoremove# centos
sudo yum remove httpdnginx安装与配置
nginx 编译安装
下载源代码 nginx: download 然后解压
geoip 安装 参考
configure 配置很多 自行搜索添加 ./configure --prefix/www/server/nginx --conf-path/www/server/nginx/conf/nginx.conf --error-log-path/var/log/nginx/error.log --http-log-path/var/log/nginx/access.log --with-select_module --with-poll_module --with-threads --with-file-aio --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_addition_module --with-http_xslt_module --with-http_image_filter_module --with-http_geoip_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_auth_request_module --with-http_random_index_module --with-http_secure_link_module --with-http_degradation_module --with-http_slice_module --with-http_stub_status_module --with-http_sub_module --with-pcre --with-pcre-jit --with-stream --with-stream_ssl_module --with-stream_realip_module --with-cc-opt-O2 -pipe -fPIC --with-ld-opt-Wl,-Bsymbolic-functions -Wl,--as-neededconfiguring 完成后 会输出 configuration summary 然后 构建安装
make
make install配置服务
配置服务 nginx.service 放到 /etc/systemd/system 下
[Unit]
Descriptionnginx
Afternetwork.target[Service]
Typeforking
PIDFile/www/server/nginx/logs/nginx.pid
ExecStartPre/www/server/nginx/sbin/nginx -t
ExecStart/www/server/nginx/sbin/nginx
ExecReload/www/server/nginx/sbin/nginx -s reload
ExecStop/www/server/nginx/sbin/nginx -s stop
AmbientCapabilitiesCAP_NET_BIND_SERVICE
PrivateTmptrue
Userroot
Grouproot[Install]
WantedBymulti-user.target注意修改路径 根据你的安装路径进行调整 注意这里的 user group 都使用的 root后面的 nginx 主配置中 设置 user www; 服务以root用户启动后面设置 user www; 才能生效 注意 AmbientCapabilitiesCAP_NET_BIND_SERVICE 没添加会提示 nginx 绑定端口失败更多处理方法见 下面参考博客 启动服务
systemctl daemon-reloadsystemctl start nginxps -aux | grep nginx下面展示是基于后面的 nginx 配置【设置了 user www;】启动的效果
一个主进程 root用户一个工作线程 www 用户 后续过程涉及多次 nginx 配置修改需要重启验证下面的命令会多次用到
nginx -tsystemctl status nginxsystemctl reload nginxsystemctl stop nginxsystemctl start nginxps -aux | grep nginxphp-fpm多站点配置
phf-fpm介绍
php-fpm 是 PHP FastCGI Process Manager 的缩写是 PHP 提供的一种高性能的 FastCGI 实现 专门用于处理 PHP 请求。
在 Web 开发中Web 服务器如 Nginx、Apache本身无法直接解析和执行 PHP 文件。为了让 Web 服务器能够运行 PHP 脚本就需要一个中间程序来处理这些请求 —— 这就是 FastCGI 的作用。 FastCGI 是一种协议标准用于 Web 服务器与后端应用服务器之间的通信。 php-fpm 是实现这个协议的工具主要功能有
功能描述接收 PHP 请求从 Nginx 或 Apache 接收 PHP 脚本请求解析并执行 PHP 脚本把 PHP 代码转换为 HTML 页面或 JSON 数据管理进程池控制 PHP 进程数量提高并发性能支持多站点配置每个网站可以使用不同的用户、权限和配置支持 Unix Socket 和 TCP更灵活地与 Web 服务器通信日志记录与错误监控记录慢脚本、错误日志等信息
[浏览器] → [Nginx] → [PHP-FPM] → [MySQL / Redis / 其他服务]用户发出访问nginx接收请求nginx通过fastcgi_pass 转发给 php-fpmphp-fpm执行php脚本连接数据库生成html内容php-fpm 返回结果给nginx由nginx返回给浏览器。
多站点配置
php-fpm.conf.default
全局配置 php-fpm.conf
[global]
pid /www/server/php/74/var/run/php-fpm.pid
error_log /www/server/php/74/var/log/php-fpm.log
log_level notice
include /www/server/php/74/etc/php-fpm.d/*.conf[www]
listen /tmp/php-cgi-74.sock
listen.backlog 8192
listen.allowed_clients 127.0.0.1
listen.owner www
listen.group www
listen.mode 0666
user www
group www
pm dynamic
pm.status_path /phpfpm_74_status
pm.max_children 50
pm.start_servers 5
pm.min_spare_servers 5
pm.max_spare_servers 10
request_terminate_timeout 100
request_slowlog_timeout 30
slowlog var/log/slow.log在这里加入了一条 include
include /www/server/php/74/etc/php-fpm.d/*.conf用于加载各个站点配置
A站点 php-fpm.d/a.conf
[a]
listen /var/run/php/a.sock
listen.owner www
listen.group www
listen.mode 0666
user www
group wwwB站点 php-fpm.d/b.conf
[b]
listen /var/run/php/b.sock
listen.owner www
listen.group www
listen.mode 0666
user www
group www上面给出的是主要配置其中最重要的是 listen 对应一个站点应用建立的 sock 通信文件
这里的 listen 也支持 ; ‘ip.add.re.ss:port’ - to listen on a TCP socket to a specific IPv4 address on ; a specific port; ; ‘[ip:6:addr:ess]:port’ - to listen on a TCP socket to a specific IPv6 address on ; a specific port; ; ‘port’ - to listen on a TCP socket to all addresses ; (IPv6 and IPv4-mapped) on a specific port; ; ‘/path/to/unix/socket’ - to listen on a unix socket. 其他完整的配置 可以参考提供的 xxx.conf.default 然后进行设置建议直接拷贝后重命名为站点配置 在里面修改就行了
错误排查 主要通过查看日志 php-fpm.log 、slow.log
php-fpm 执行
pkill php-fpm/www/server/php/74/sbin/php-fpm -y /www/server/php/74/etc/php-fpm.conf -c /www/server/php/74/etc/php.ini 创建的 sock 主进程 master process
加载全局配置监听sock文件或端口管理子进程池 pool
子进程池 pool
每个 pool 对应一个 PHP 应用这里也就是 WordPress每个 pool 可以设置不同用户、监听地址、资源限制等子进程负责实际执行 PHP 脚本
nginx 多站点配置
nginx 安装路径 /www/server/nginx
conf/nginx.conf #user nobody;
user www;
worker_processes 1;#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;#pid logs/nginx.pid;events {worker_connections 1024;
}http {include 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 logs/access.log main;sendfile on;#tcp_nopush on;#keepalive_timeout 0;keepalive_timeout 65;#gzip on;include /www/server/nginx/conf/site-enabled/*.conf;} 原有的配置基本被删除掉 通过
include /www/server/nginx/conf/site-enabled/*.conf;将启用的 站点引入起来
site-available\
A站点配置 a.conf
server {listen 443 ssl;server_name a.top www.a.top;root /www/wwwroot/a.top;index index.php index.html;access_log /var/log/nginx/a.top.access.log;error_log /var/log/nginx/a.top.error.log;ssl_certificate /www/server/panel/vhost/cert/a.top/fullchain.pem;ssl_certificate_key /www/server/panel/vhost/cert/a.top/privkey.pem;ssl_protocols TLSv1.2 TLSv1.3;ssl_ciphers HIGH:!aNULL:!MD5;ssl_prefer_server_ciphers on;ssl_session_cache shared:SSL:10m;ssl_session_timeout 10m;location / {try_files $uri $uri/ /index.php?$query_string;}location ~ \.php$ {fastcgi_pass unix:/var/run/php/a.sock;fastcgi_index index.php;include fastcgi_params;fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;include fastcgi.conf;}location ~ /\.ht {deny all;}location ~ ^/wp-config\.php$ {deny all;}# 防止上传目录执行php文件location ~ ^/wp-content/uploads/.*\.php$ {deny all;}
}server {listen 80;server_name a.top www.a.top;return 301 https://$host$request_uri;
}B站点配置 b.conf
server {listen 80;server_name b.top;root /www/wwwroot/b.top;index index.php index.html;access_log /var/log/nginx/b.access.log;error_log /var/log/nginx/b.error.log;location / {try_files $uri $uri/ /index.php?$query_string;}location ~ \.php$ {fastcgi_pass unix:/var/run/php/b.sock;fastcgi_index index.php;include fastcgi_params;fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;include fastcgi.conf;}location ~ /\.ht {deny all;}location ~ ^/(uploads|files)/.*\.php$ {deny all;}
}注意配置文件中 fastcgi_pass 使用的 sock 要 和 php-fpm 中站点使用的sock文件 一致 conf/site-enabled
ln /www/server/nginx/site-avaiable/a.conf /www/server/nginx/conf/site-enabled/ln /www/server/nginx/site-avaiable/b.conf /www/server/nginx/conf/site-enabled/会在 conf/site-enabled 建立两个软链接 指向 我们 需要管理的网站 nginx 配置在nginx.conf 主配置那里通过 include 进行了引入
这种分离式配置可以分别设置不同网站的配置通过软链接进行启用 方便管理无需全都放在nginx.conf 主配置中 变得很长难以查询管理
nginx ssl 配置
具体参考 A站点 a.conf
ssl_certificate /www/server/panel/vhost/cert/a.top/fullchain.pem;
ssl_certificate_key /www/server/panel/vhost/cert/a.top/privkey.pem;ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 10m;ssl_certificate
ssl_certificate_key设置 ssl 证书和私钥 文件路径
这里要确保 nginx 能够对 证书和私钥有读取权限
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 10m;是 ssl 相关配置 协议版本 套件缓存超时等
然后
listen 443 ssl;启用 ssl 如果通过浏览器测试 提示 502 或 curl 测试 提示 curl: (35) SSL received a record that exceeded the maximum permissible length 检查是不是listen忘记添加了 ssl; nginx 安全策略
针对 wordpress 添加的规则
# 禁止 /wp-content/uploads/ *.php
location ~ ^/wp-content/uploads/.*\.php$ {deny all;
}AI提供的更细致的规则 可控参考设置
# 1. 禁止访问 .php 文件
location ~ ^/wp-content/uploads/.*\.php$ {deny all;
}# 2. 禁止访问敏感扩展名
location ~ ^/wp-content/uploads/.*\.(sh|pl|exe|bat|cgi|phps|phar)$ {deny all;
}# 3. 图片等资源正常访问
location ~ ^/wp-content/uploads/.*\.(jpg|jpeg|png|gif|webp|mp4|ogg|ogv|webm)$ {expires 30d;add_header Cache-Control public;
}# 4. 禁止访问 wp-config.php
location ~ ^/wp-config\.php$ {deny all;
}# 5. 禁止访问 .htaccess 等隐藏文件
location ~ /\. {deny all;
}参考 CentOS下为Nginx安装GeoIP扩展 nginx常见问题(四)端口无权限_nginx: emerg bind() to 0.0.0.0:80 failed (13: pe nginx多站点独立配置 Include引入 www.sock failed (13: Permission denied) 从零手写实现 nginx-16-nginx.conf 支持配置多个 server