nginx搭建wordpress
环境:centos7,nginx,phpMyAdmin,maraidb-server
- 搭建任务
现在是打算搭两台服务器,两台都提供web服务,其中一台(serverA)再做nginx(proxy),一台(serverB)做mariadb
serverA:eth0:172.16.104.22 eth1:192.168.104.22
serverB:eth0:172.16.104.33 eth1:192.168.104.33
serverA的配置
- 环境安装
cd /etc/nginx/conf.d
vim nginx.conf
server {
listen 192.168.104.22:80;
server_name www.nginx.com;
root /data/web;
location / {
index index.html index.php;
}
location ~ \.php$ {
root /data/web;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
因为要连数据库,而且wordpress是动态网站,这里用fcgi,这样网站配置文件就写完了。
下面写nginx(proxy)代理,这里我做了ssl,和一个访问当后端主机均不可用时,使用127.0.0.1的80端口提供一个sorry
server响应客户端的请求。
vim proxy.conf
- 下面写默认网页
mkdir -p /data/web
vim /data/web/index.php
后端主机不可用时,响应客户端的默认页面
vim sorry.html
这里再做一个nfs共享,后面serverB的网页文件也不用写了
[root@serverA ~]# vim /etc/exports
/data/web 192.168.104.0/24(rw)
[root@serverA ~]# systemctl restart rpcbind
[root@serverA ~]# systemctl restart nfs-server
[root@serverA ~]# cd /data/web
[root@serverA web]# ls
index.php phpMyAdmin sorry.html wordpress ##解压phpMyAdmin和wordpress到网站根目录下
serverA基本做完了,我们来重启服务
systemctl restart nginx
systemctl restart php-fpm
serverB的配置
前面说serverB也做web,本人懒,就scp传一份serverA的nginx.conf,改改监听地址就好了。安装nginx和前面一样。
[root@serverB conf.d]# vim nginx.conf
server {
listen 192.168.104.33:80;
server_name www.nginx.com;
root /data/web;
location / {
index index.html index.php;
}
location ~ \.php$ {
root /data/web;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
[root@serverB conf.d]# mkdir /data/web
[root@serverB ~]# vim /etc/fstab
/dev/mapper/centos-root / xfs defaults 0 0
UUID=8366371f-47a4-4d5d-9e29-89bfba6b6b45 /boot xfs defaults 0 0
/dev/mapper/centos-swap swap swap defaults 0 0
192.168.104.22:/data/web /data/web nfs defaults,_netdev 0 0
[root@serverB ~]# mount -a
[root@serverB ~]# ll /data/web
总用量 16
-rw-r--r-- 1 root root 66 12月 15 18:02 index.php
drwxr-xr-x 10 root root 4096 12月 13 01:09 phpMyAdmin
-rw-r--r-- 1 root root 45 12月 13 01:37 sorry.html
drwxr-xr-x 5 1006 1006 4096 12月 15 18:49 wordpress
重启服务我们先验证搭好没有,会看到system内容一个serverA一个serverB(那是主机名)
页面多刷新几次就会看到他代理到不同的web主机上
然后停止两台的web服务
[root@serverB web]# systemctl stop nginx
[root@serverA conf.d]# pwd
/etc/nginx/conf.d
[root@serverA conf.d]# mv nginx.conf nginx.conf.bak
[root@serverA conf.d]# systemctl restart nginx
[root@serverA conf.d]#
这里要注意,因为serverA做了(proxy),直接停止web服务,代理也关闭了,所有我们给web配置文件换个名字(就当它冗机了)。然后再去访问验证
- 下面做mariadb配置
我们先改一下my.cnf的数据库路径
mkdir /data/database
chown mysql:mysql /data/database #如果不改属主,重启服务mariadb会没有权限写入
重启mariadb进去mariadb
systemctl restart mariadb
systemctl enable mariadb #加入开启启动项
进入mysql创建一个数据库,和一个用户,并允许该用户对数据库有相应的权限
[root@serverB web]# mysql -uroot -p
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 4
Server version: 5.5.44-MariaDB MariaDB Server
Copyright (c) 2000, 2015, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> CREATE DATABASE wordpress;
Query OK, 1 row affected (0.00 sec)
MariaDB [(none)]> CREATE USER 'wordpress'@'%' IDENTIFIED BY '123456';
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> GRANT ALL PRIVILEGES ON wordpress.* TO 'wordpress'@'%';
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> SHOW DATABASES;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
| wordpress |
+--------------------+
5 rows in set (0.00 sec)
MariaDB [(none)]> SELECT User,Host,Password FROM mysql.user;
+-----------+----------------+-------------------------------------------+
| User | Host | Password |
+-----------+----------------+-------------------------------------------+
| root | localhost | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
| root | serverb.rj.com | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
| root | 127.0.0.1 | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
| wordpress | % | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
+-----------+----------------+-------------------------------------------+
4 rows in set (0.00 sec)
MariaDB [(none)]> FLUSH PRIVILEGES; #刷新数据库使配置生效
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]>
然后登陆phpMyAdmin,这里遇到了个问题就是出现php登陆页面却登陆不了,找了好久发现是代理问题,把serverA的web服务关了就能登陆了,有可能登陆账号它会找serverA的mysql?因为这个数据库管理软件要装在数据库那台机子上才生效
[root@serverA web]# tar xzf /root/pkg/wordpress-5.0.3-zh_CN.tar.gz
[root@serverA web]# cd wordpress/
[root@serverA wordpress]# ls
index.php readme.html wp-admin wp-comments-post.php wp-content wp-includes wp-load.php wp-mail.php wp-signup.php xmlrpc.php
license.txt wp-activate.php wp-blog-header.php wp-config-sample.php wp-cron.php wp-links-opml.php wp-login.php wp-settings.php wp-trackback.php
[root@serverA wordpress]# cp -p wp-config-sample.php wp-config.php
[root@serverA wordpress]# vim wp-config.php
define('DB_NAME', 'wordpress'); #修改这几行就行了
/** MySQL数据库用户名 */
define('DB_USER', 'wordpress');
/** MySQL数据库密码 */
define('DB_PASSWORD', '123456');
/** MySQL主机 */
define('DB_HOST', '192.168.104.33');
我们访问安装wordpress去咯,这里会看到安装出现css样式加载不出来,不过没事后面解决,这里在网上看到别人的解决方法,这里好像因为代理的固定连接,需要到数据库后台修改
https://www.nginx/wordpress
这里安装又出现个问题就是它代理又会找serverB去了,又把serverB的nginx给关了,后面我又实验一遍,serverA使用nfs共享文件给serverB就不会出现这种情况(原因他们共用一个web文件夹就不会跑来跑去的不听话)
不是scc样式不加载吗,来,进入phpMyAdmin,记住关了serverA的web
[root@serverA conf.d]# mv nginx.conf nginx.conf.bak
[root@serverA conf.d]# systemctl restart nginx
找到wp_options表,修改home和siteurl值,把代理www改为域名,再刷新wordpress