nginx部署wordpress架構
實驗環境
(nginx)
10.20.20.3
(WordPress nginx PHP)
10.20.20.4
10.20.20.5
(mysql 主從)
10.20.20.6 10.20.20.7
查看Word
Press支持的mysql版本
數據庫主從設置
安裝依賴包
yum install libaio* -y
官網下載包 二進制安裝
腳本安裝 #腳本練習我QQ 790827253
傳入包 安裝mysql
10.20.20.6 (主) 10.20.20.7(從)
主 配置
vim /etc/my.cof
#master
[mysqld]
socket=/var/lib/mysql/mysql.sock
user=mysql
symbolic-links=0
datadir=/data/mysql
innodb_file_per_table=1
server-id=1
log-error=/data/mysql/mysql_error.txt
log-bin=/data/mysql/master-log
[client]
port=3306
socket=/var/lib/mysql/mysql.sock
[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/tmp/mysql.sock
建立庫用戶
create database test1;
GRANT REPLICATION SLAVE,REPLICATION CLIENT ON *.* TO 'dbb'@'10.20.20.%' IDENTIFIED BY '123456';
grant replication slave on *.* to "dbb"@"10.20.20.%" identified by '123456';
若是主數據庫以前有數據 要把數據導出來而後讓從數據庫同步
要把日誌改爲數據大的時候
導出文件
/usr/local/mysql/bin/
mysqldump --all-databases --single_transaction --flush-logs --master-data=2 --lock-tables > /opt/backup.sql
傳入從
scp /opt/backup.sql 10.20.20.7:/opt/
/etc/init.d/mysqld restart
查看二進制日誌
從配置
vim /etc/my.cof
#slave
[mysqld]
socket=/var/lib/mysql/mysql.sock
user=mysql
symbolic-links=0
datadir=/data/mysql
innodb_file_per_table=1
server-id=10
#log-bin=/data/mysql/master-log
relay-log = /data/mysql
[client]
port=3306
socket=/var/lib/mysql/mysql.sock
[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/tmp/mysql.sock
#
mysql < /opt/backup.sql #slave 導入sql文件數據
CHANGE MASTER TO MASTER_HOST='10.20.20.6',MASTER_USER='dbb',MASTER_PASSWORD='123456',MASTER_LOG_FILE='master-log.000002',MASTER_LOG_POS=950;
驗證
show databases;
start slave ;
show slave status\G
主從複製的問題
1 防火牆是否關閉
2 日誌必定要同步到正確的時候
3 使用reset slave 清除同步信息 reset maser
(WordPress nginx)10.20.20.4 10.20.20.5
php安裝
安裝依賴包
yum install -y gcc gcc-c++ make zlib zlib-devel pcre pcre-devel libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel krb5 krb5-devel openssl openssl-devel openldap openldap-devel nss_ldap openldap-clients openldap-servers
yum -y install wget vim pcre pcre-devel openssl openssl-devel libicu-devel gcc gcc-c++ autoconf libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel ncurses ncurses-devel curl curl-devel krb5-devel libidn libidn-devel openldap openldap-devel nss_ldap jemalloc-devel cmake boost-devel bison automake libevent libevent-devel gd gd-devel libtool* libmcrypt libmcrypt-devel mcrypt mhash libxslt libxslt-devel readline readline-devel gmp gmp-devel libcurl libcurl-devel openjpeg-devel
下載包
wget
增長用戶
useradd www
編譯
./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --with-config-file-scan-dir=/usr/local/php/etc/conf.d --enable-fpm --with-fpm-user=www --with-fpm-group=www --with-pear --with-curl --with-png-dir --with-freetype-dir --with-iconv --with-mhash --with-zlib --with-xmlrpc --with-xsl --with-openssl --with-mysqli --with-pdo-mysql --disable-debug --enable-zip --enable-sockets --enable-soap --enable-inline-optimization --enable-xml --enable-ftp --enable-exif --enable-wddx --enable-bcmath --enable-calendar --enable-shmop --enable-dba --enable-sysvsem --enable-sysvshm --enable-sysvmsg
make -j 2 && make install
cd /usr/local/php/etc/
[root@redis-s1 etc]# pwd
/usr/local/php/etc
[root@redis-s1 etc]# cp php-fpm.conf.default php-fpm.conf
cd /usr/local/php/etc/php-fpm.d/
root@redis-s1 php-fpm.d]# pwd
/usr/local/php/etc/php-fpm.d
[root@redis-s1 php-fpm.d]# cp www.conf.default www.conf
vim php-fpm.conf
vim www.conf
修改參數
8 4 4 6
pm.max_children =8
pm.start_servers =4
pm.min_spare_servers =4
pm.max_spare_servers =6
配置說明:
pm.max_children #,子進程最大數
pm.start_servers #,啓動時的進程數
pm.min_spare_servers #,保證空閒進程數最小值,若是空閒進程小於此值,則建立新的子進程
pm.max_spare_servers #,保證空閒進程數最大值,若是空閒進程大於此值,此進行清理
啓動
/usr/local/php/sbin/php-fpm
(WordPress nginx)10.20.20.4 10.20.20.5
安裝nginx
配置官方yum源
或者編譯安裝
倆個服務器都是同樣的配置
編譯安裝過程
cd /usr/local/scr/
傳入安裝包 1.12
解壓縮
tar xvf
cd 進去 編譯
./configure --prefix=/usr/local/nginx --with-pcre --with-http_stub_status_module --with-http_ssl_module
make && make install
配置文件在
/usr/local/nginx/conf/nginx.conf
#mkdir /data/nginx/wordpress –p
#chown www.www /usr/local/nginx/ /data/nginx/ -R
location / {
root /code/wordpress;
index index.php index.html index.htm;
if ($http_user_agent ~ "ApacheBench|WebBench|TurnitinBot|Sogou web spider|Grid Service") {
proxy_pass http://www.baidu.com;
return 403;
}
}
location ~ \.php$ {
root /code/wordpress;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
1.4:建立php測試頁:
# cat /data/nginx/wordpress/index.php
<?php
phpinfo();
?>
1.6:準備數據庫:
mysql> CREATE DATABASE wordpress;
建立的用戶是負載均衡的ip地址 這裏是0.20.20.3
mysql> GRANT ALL PRIVILEGES ON *.* TO "dbb"@"10.20.20.%" IDENTIFIED BY "123456";
mysql> FLUSH PRIVILEGES;
部署WordPress:
[root@redis-s4 wordpress]# pwd
/data/nginx/wordpress
# tar xvf wordpress-5.0.1-zh_CN.tar.gz
# mv wordpress-5.0.1-zh_CN.tar.gz /opt
# cp wp-config-sample.php wp-config.php
#vim wp-config.php #修改數據庫配置
# grep "^[a-Z]" wp-config.php
define('DB_NAME', 'wordpress');
define('DB_USER', 'wordpress');
define('DB_PASSWORD', '123456');
define('DB_HOST', '192.168.7.103');
define('DB_CHARSET', 'utf8');
define('DB_COLLATE', '');
define('AUTH_KEY', 'zY%A)<+`Qf^68kzMC-)0+bL3m!LADP-PgJ9a~-CLtk*JnJ%-OGDEi/-v*zoJx1gP');
define('SECURE_AUTH_KEY', '1muyi_3wR[/<}1-[2/F=Lb3`1c-+o5*!uu4-un-4!iMb!Nl|X=}Ud-<PGxkPpuse');
define('LOGGED_IN_KEY', '*%/!SxN}FllpDe4+_Phylp>_^jD]N$Tc9/CstL )yRvLg!+9y #l}1?Da 8}sat{');
define('NONCE_KEY', 'mTY2Bio ;v4j^%#O>J&@Py[6TaYy+mLmbp7$[=c,l|qY9TPwSH+Kf(!PUmTE9J`c');
define('AUTH_SALT', '~:wkxx83`q_+bm|{MH-|CA<`>#JJGitCsed#?a^BSsm{rVC1sg;b:&/:h$f>9D.z');
define('SECURE_AUTH_SALT', 'u%_}v`0L~#<kZ|U +st.]iNTbGq-%Bb)Ti&E<$-LMU6jtEz}X)T$MZ{@S(#DJOQp');
define('LOGGED_IN_SALT', 'QxUBuWv[GjaGx7=VR|&.i?wTsS-Jr($}p#?y})O-N1*h$gh d cj%H-g2~HV*&ki');
define('NONCE_SALT', '>Lx7Vu=>R+O||Ks)cCDGO=+st|Ja&|+5GlR?F{1Vdng-q:f#:{[TIfY#EtP|1k8a');
wp-config.php 文件中的主機不是mysql的地址而是 負載均衡的nginxip 10.20.20.3 獨立的
訪問網站 獲得的替換在下面
測試並啓動Nginx:
[root@redis-s4 wordpress]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@redis-s4 wordpress]# /usr/local/nginx/sbin/nginx
推薦yum安裝
yum 安裝過程
配置yum 源 官網
[root@wordpress yum.repos.d]#cat nginx.repo
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=0
enabled=1
修改配置文件 改用戶
vim /etc/nginx/nginx.conf
user www;
worker_processes auto;
vim /etc/nginx/nginx.conf
server {
server_name www.web.com;
listen 80;
root /code;
index index.php index.html;
location ~ \.php$ {
root /code;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
更改目錄的權限 和用戶
chown www.www /code/ -R
測試頁
[root@nginx ~]# cat /code/info.php
<?php
phpinfo();
?>
部署博客產品Wordpress
#1.nginx具體配置信息
[root@nginx ~]# cat /etc/nginx/conf.d/wordpress.conf
server {
listen 80;
server_name www.dbb.com;
root /code/wordpress;
index index.php index.html;
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
2) 重啓nginx服務
[root@nginx ~]# systemctl restart nginx
3) 獲取wordpress產品,解壓並部署wordress
[root@nginx ~]# mkdir /code
[root@nginx ~]# cd /code
[root@nginx code]# wget https://cn.wordpress.org/wordpress-4.9.4-zh_CN.tar.gz
[root@nginx ~]# tar xf wordpress-4.9.4-zh_CN.tar.gz
[root@nginx ~]# chown -R www.www /code/wordpress/
tar xf wordpress-4.9.4-zh_CN.tar.gz -C /code/
cp wp-config-sample.php wp-config.php
cd /cxldata/www/wordpress
vi wp-config.php
#添加三行
define("FS_METHOD", "direct");
define("FS_CHMOD_DIR", 0777);
define("FS_CHMOD_FILE", 0777);
wp-config.php 文件中的主機不是mysql的地址而是 負載均衡的nginxip 10.20.20.3 獨立的
4) 因爲wordpress產品須要依賴數據庫, 因此須要手動創建數據庫
[root@nginx ~]# mysql -uroot -pBgx123.com
mysql> create database wordpress;
mysql> exit
create database wordpress;
grant all on *.* to dbb@'10.20.20.3' identified by '123456';
(nginx) 10.20.20.3
使用nginx負載均衡
一樣安裝nginx
安裝mysql
鏈接主mysql的服務器
mysql -udeng -h10.20.20.6 -p123456
配置負載均衡
vim /etc/nginx/nginx.cof
stream {
upstream mysql {
server 10.20.20.6:3306;
}
server {
listen 10.20.20.3 :3306;
proxy_pass mysql;
}
}
啓動nginx
nginx -t
nginx -s reload
端口是否打開
若是nginx啓動錯誤
nginx: [error] open() "/Var/run/Nginx.pid" failed (2: No such file or directory)
執行
nginx -C /etc/nginx/nginx.cof
測試在10.20.20.8 上 鏈接mysql 經過鏈接10.20.20.3來鏈接到主數據庫 06上
mysql -udeng -h10.20.20.3 -p123456
修改8 9 上面nginx 配置文件訪問網頁的目錄 改成PHp路徑
訪問ip 測試數據庫鏈接 10.20.20.8
wordpress目錄下放置圖片的位置
/wp-content/uploads
(nginx) 10.20.20.3
修改配置文件
實現負載均衡
把wordpress1 上面web目錄下的文件拷貝到wordpress2 下面
或者從新在2的上面配置wordpress 和1 的上面要同樣
server {
server_name www.dbbdbb.net;
listen 80;
location / {
root /code/wordpress;
index index.php index.html;
}
location ~ \.php$ {
root /code/wordpress;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
在wordpress2 上面要和wordpress1 上面nginx配置文件同樣
的代碼
server {
server_name www.dbbdbb.net;
listen 80;
location / {
root /code/wordpress;
index index.php index.html;
}
location ~ \.php$ {
root /code/wordpress;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
訪問wordpress2 的ip 查看wordpress 網站 是否和1 的同樣
把監聽ip改爲域名
1的
2 的
作nfs
在3 上安裝nfs
yum install nfs-utils -y
作目錄共享
mkdir /nfsdata/images -pv
vim /etc/exports
systemctl restart nfs
在wordpress1 和2 上安裝nfs-utils
yum install nfs-utils -y
wordoress1 上 查看掛載的目錄
showmount -e 10.20.20.3
把目錄掛載到圖片的目錄
若是目錄下有數據就拷走 等掛載完再考回來
mount -t nfs10.20.20.3:/nfsdata/images /data/nginx/wordpress/wp-content/iploads
2 的上面也掛載一下
而且寫入文件中
vim /etc/fstab
或者寫入
vim /etc/rc.d/rc.local
在3 上 加負載
yum install haproxy
/etc/nginx/conf/conf.d/*.cong:
vim conf.d/blog.conf
加人 ip_hash: 服務請求只在一個服務器上
nfs的主從同步
3 上 的主 4上的 從
安裝
yum install nfs-utils -y
從上要有和主上同樣的目錄結構
mkdir /nfsdata/images -pv
vim /etc/exports
systemctl start nfs
將主的數據拷貝到從上面
scp 10.20.20.3:/nfsdata/images/* 10.20.20.8:/nfsdata/images/
能夠在8上執行拷貝命令 也能夠在別的主機上執行 跨主機執行
作好祕鑰認證
可使用rsync 比scp好 效驗功能 已有的文件不會拷貝
yum install rsync
rsync -avrlopg /nfsdata/images/* 10.20.30.8:/nfsdata/images/
祕鑰認證
ssh-keygen
ssh-copy-id 10.20.20.4
yum install httpd-tools -y
攻擊網站 發起請求
查看本機的 ulimit -n
wordpress 更新
3 上面
把新版本的傳進來
解包 tar xvf
cd wordpress
把wordpress1(10.20.20.3) 上面的配置文件拷貝到 3 上面
目錄下
scp wp-config-php 10.20.20.3:/usr/local/src/wordpress
3 上
在wordpress目錄下
配置文件
wp-config-php
修改配置
拷貝到wordpress2 下面
scp -r ./* 10.20.20.9:/data/nginx/wordpress
修改權限
chown www.www -R /data/nginx/wordpress/
過程
1 中止服務nginx
2備份原數據或刪除
3 升級版本
4 啓動服務nginx
解完包 進入目錄wordpress
打包 zip -r wordpress.zip wordpress/*
腳本
vim updata.sh
#!/bin/bash
ssh 10.20.20.8 "/usr/local/nginx/sbin/nginx -s stop"
ssh 10.20.20.8 "rm -rf /data/nginx/wordpress/*"
scp wordpress.zip 10.20.20.8:/data
ssh 10.20.20.8 "unzip /data/wordpress.zip -d /data/nginx/wordpress/ && /usr/local/nginx/sbin/nginx && chown www.www -R /data/nginx/wordpress/"
學會查看網頁的調試器 右鍵 查看元素
總結配置文件
wordpress1 的配置文件
wordpress2 的同樣
server {
server_name www.dbbdbb.net;
listen 80;
location / {
root /code/wordpress;
index index.php index.html;
}
location ~ \.php$ {
root /code/wordpress;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
nginx配置文件
stream {
upstream mysql {
server 10.20.20.6:3306;
}
server {
listen 10.20.20.16:3306;
proxy_pass mysql;
}
}
server {
listen 80;
server_name blogs;
index index.html index.php;
location / {
proxy_pass http://blogs;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_next_upstream http_502 http_504 error timeout invalid_header;
}
}
# vim /etc/nginx/conf.d/blog.conf
upstream blogs {
server 172.20.101.204:80 weight=1 max_fails=3 fail_timeout=100s;
server 172.20.101.205:80 weight=1 max_fails=3 fail_timeout=100s;
ip_hash;
}
server {
listen 80;
server_name www.dengbingbings.com;
index index.html index.php;
location / {
proxy_pass http://blogs;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_next_upstream http_502 http_504 error timeout invalid_header;
}
}
server_name www.dbbdbb.com 3臺機必須同樣
10.20.20.3 配置文件 終極版
只須要這一個配置
#調配mysql數據庫的
stream {
upstream mysql {
server 10.20.20.5:3306;
}
server {
listen 10.20.20.3:3306;
proxy_pass mysql;
}
}
#負載均衡wordpress
註釋掉哈希是爲了體現出輪詢
upstream blogs {
server 172.20.101.204;
server 172.20.101.205;
# ip_hash;
# hash $request_uri consistent;
server {
listen 80;
location / {
proxy_pass http://blogs;
}
}
補充 實驗過程能夠切換綁定域名的ip地址來查看實驗的效果
C:\Windows\System32\Drivers\etc
wordpress1和2 的配置文件
[root@wordpress2 etc]#cat /etc/nginx/nginx.conf
user www;
worker_processes auto;
error_log /var/log/nginx/error.log warn;
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"';
server {
server_name www.dengbingbing.com;
listen 80;
location / {
root /code/wordpress;
index index.php index.html;
}
location ~ \.php$ {
root /code/wordpress;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
}
nginx 負載均衡的配置文件
[root@nginx ~]#cat /etc/nginx/nginx.conf
user www;
worker_processes auto;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
stream {
upstream mysql {
server 10.20.20.6:3306;
}
server {
listen 10.20.20.16:3306;
proxy_pass mysql;
}
}
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"';
upstream blogs {
server 172.20.101.204;
server 172.20.101.205;
# ip_hash;
# hash $request_uri consistent;
}
server {
listen 80;
location / {
proxy_pass http://blogs;
}
}
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
}
若是要實如今一個wordpress上面添加圖片 另外一個沒有添加圖片
訪問能夠看到圖片和看不到圖片 ,要修改wordpress後臺的
WordPress地址(URL)和 站點地址(URL)
修改成nginx的調度服務器的地址 而後這個地址綁定到域名上
可能會出現的問題
修改完之後會由於密碼不對進不去wordpress頁面
解決辦法
1 從新安裝wordpress使倆個上面的配置都同樣
複製拷貝過去
實驗過程
在安裝完一個wordpress之後在頁面建立了之後 再把wordpress的配置文件傳到第二個上面