Lxn-Chan!

(~ ̄▽ ̄)→))* ̄▽ ̄*)o

在同一台服务器上同时部署 Nginx 、 Apache2 和 Wordpress,并使用 Nginx 反代基于 Apache2+PHP 的 Wordpress 站点。

这是一篇过时的文章,新写的版本在在CentOS中搭建由nginx反代和Apache2作为后端的Wordpress。部分内容可能已过时或者有所改变,请前往新版文章或谨慎参考本文内容。本文将不会再更新。

总述

Nginx 的PHP配置实在有些复杂,于是想出了这种曲线救国的方案,即使用 Apache2 监听一个内部端口,然后由 Nginx 代理该端口并在外部访问。

基础环境配置

安装必要组件

先安装Apache2和 PHP 组件。

1
apt install apache2 openssl php libapache2-mod-php

安装好后打开/etc/apache2/ports.conf文件,把全部行注释或删除,在最下面加入新行以监听新的端口号。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# 原来可能的内容,全部注释或删除掉
# If you just change the port or add more ports here, you will likely also
# have to change the VirtualHost statement in
# /etc/apache2/sites-enabled/000-default.conf

#Listen 80

#<IfModule ssl_module>
# Listen 443
#</IfModule>

#<IfModule mod_gnutls.c>
# Listen 443
#</IfModule>

# vim: syntax=apache ts=4 sw=4 sts=4 sr noet

# 添加新行,格式为 Listen 端口号
Listen 1111

保存退出后重启 Apache2 。

1
systemctl restart apache2

此时确认 Apache2 没有占用80和443两个端口后安装 Nginx :

1
apt install nginx

访问服务器IP的80端口出现的如果是 Nginx 的默认界面即为成功,在这一步暂时不需要对nginx进行任何设置。

配置数据库

这里仅针对全新安装,若并非全新安装的数据库请酌情绕过。

安装MySQL数据库:

1
apt install mysql-server

执行安全安装:

1
mysql_secure_installation

进入数据库:

1
mysql -uroot -p

输入密码后创建数据库:

1
CREATE DATABASE IF NOT EXISTS wordpress DEFAULT CHARACTER SET = utf8mb4 DEFAULT COLLATE = utf8mb4_general_ci;

创建用户,将命令行内{password}改为你的自定义密码,不要使用Root密码

1
CREATE USER 'wordpress'@'localhost' IDENTIFIED BY '{password}';

授予用户访问数据库的权限并强制刷新权限:

1
2
GRANT ALL PRIVILEGES ON wordpress.* TO 'wordpress'@'localhost' WITH GRANT OPTION;
FLUSH PRIVILEGES;

最后退出数据库即可。

配置 Apache2

因为 Apache2 不真实对外提供服务,所以不需要 Apache2 监听 80/443 端口,该端口将由 Nginx 代理出站。

创建后端

先准备一个SSL证书,最好是自建CA的证书,也可以是域名的证书(与前端证书无关)。

如果是自建CA的证书需要把根证书合到系统内,如果是域名证书需要在系统hosts中将域名解析到本机:

1
127.0.0.1 <YourDomain>

下面创建一个示例配置文件/etc/apache2/sites-available/wp.conf,注意修改端口号:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<VirtualHost *:1111>
ServerAdmin <Your Email>
ServerName <Domain Name>
ServerAlias <Domain Name>
DocumentRoot /var/www/wp
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined

#SSL
SSLEngine On
SSLCertificateFile /Path/to/your/cert/file
SSLCertificateKeyFile /Path/to/your/cert/Key
SSLCipherSuite EECDH+CHACHA20:EECDH+CHACHA20-draft:EECDH+AES128:RSA+AES128:EECDH+AES256:RSA+AES256:EECDH+3DES:RSA+3DES:!MD5
SSLProtocol All -SSLv2 -SSLv3 -TLSv1
SSLHonorCipherOrder On
</VirtualHost>

然后启用配置文件并重启 Apache2 :

1
2
3
sudo a2ensite wp.conf
sudo a2dissite 000-default.conf
sudo systemctl restart apache2

此时可以本地测试一下看是否有正确返回。

配置 Nginx

创建代理站点

/etc/nginx/conf.d新建配置文件,内容示例如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
server {
listen 80;
server_name <YourDomain>;
rewrite ^(.*)$ https://${server_name}$1 permanent;
}

server {
listen 443 ssl http2;
server_name <YourDomain>;

ssl_certificate /Path/to/your/cert/file;
ssl_certificate_key /Path/to/your/cert/Key;
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
ssl_protocols SSLv2 SSLv3 TLSv1.1 TLSv1.2;
ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;
ssl_prefer_server_ciphers on;

location / {
# 这里一定要填写https协议
proxy_pass https://127.0.0.1:1111;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header REMOTE-HOST $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}

后端验证

如果后端和前端在同一个服务器上是可以不必验证的。

在 Nginx 的Location段中添加如下参数:

1
2
3
4
5
6
7
location / {
...
proxy_pass https://127.0.0.1:1111;
proxy_ssl_certificate     /etc/nginx/client.pem;  
        proxy_ssl_certificate_key /etc/nginx/client.key;
...
}

如果是自建CA还需要添加PEM格式的根证书(proxy_ssl_trusted_certificate),另外还可以配置proxy_ssl_verifyproxy_ssl_verfiy_depth指令,用来验证安全证书:

1
2
3
4
5
6
7
location / {
...
     proxy_ssl_trusted_certificate /etc/nginx/trusted_ca_cert.crt;
     proxy_ssl_verify       on;
     proxy_ssl_verify_depth 2;
...
}

参考资料

排名不分先后

本站文章

非本站文章

 简单说两句



联系站长 | 服务状态 | 友情链接

备案号:辽ICP备19013963号

萌ICP备 20219421 号

中国互联网违法和不良信息举报中心

架构版本号:8.1.6 | 本站已全面支持IPv6

正在载入运行数据(1/2)请稍后...
正在载入运行数据(2/2)请稍后...

Copyright 2024 LingXuanNing, All rights reserved.