创建nginx+php+mysql分布式部署
流程图:
1.nginx安装:
安装nginx
安装两台
2.php安装:
安装php
安装两台
3.mysql安装:
安装mysql
安装两台(一台也可以)
4.修改php配置文件
vi /srv/php/etc/php-fpm.d/www.conf
36行 127.0.0.1改成自己本机ip 后重启php
5.修改nginx配置文件
vi /srv/nginx/conf/nginx.conf
配置文件如下
#user nobody;
worker_processes 1;
error_log /data/log/nginx/error.log;
error_log /data/log/nginx/error.log notice;
error_log /data/log/nginx/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 /data/log/nginx/access.log;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
upstream fastcgiserver {
server 192.168.32.132:9000;
server 192.168.32.151:9000;
}
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root /data/web/www;
index index.html index.php index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /data/web/wwww;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
root /data/web/www;
fastcgi_pass fastcgiserver;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
6.mysql配置root用户远程登录
/srv/mysql/bin/myqsld & //启动mysql
/srv/mysql/bin/mysql -uroot -p123456 //链接数据库
数据库命令↓
create user root@'%' identified by '123456';
grant all on *.* to root@'%'
7.在php服务器上传源码包如wordpress
yum -y install wget
mkdir -p /data/web/www
cd /data/web/www
wget https://cn.wordpress.org/latest-zh_CN.tar.gz
tar xzf latest-zh_CN.tar.gz
mv wordpress/* ./
访问nginx的ip开始安装wordpress
数据库用户为 root
数据库密码为 123456
数据库地址填写mysql的ip:3306
安装完后打包www目录 然后放到nginx的/data/web/www下解压 不然会不显示js css之类的东西
以上操作两台一样配置
配置LVS+keepalived
lvs+keepalived1(主)
yum -y install keepalived ipvsadm
vi /etc/keepalived/keepalived.conf
配置文件如下
! Configuration File for keepalived
global_defs {
notification_email {
acassen@firewall.loc
failover@firewall.loc
sysadmin@firewall.loc
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id LVS_DEVEL
vrrp_skip_check_adv_addr
vrrp_garp_interval 0
vrrp_gna_interval 0
}
vrrp_instance VI_1 {
state MASTER
interface ens33
virtual_router_id 55
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.32.201
}
}
virtual_server 192.168.32.201 80 {
delay_loop 6
lb_algo rr
lb_kind DR
persistence 60
protocol TCP
real_server 192.168.32.150 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 4
}
}
real_server 192.168.32.129 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 4
}
}
}
systemctl start keepalived
lvs+keepalived1(从)
yum -y install keepalived ipvsadm
vi /etc/keepalived/keepalived.conf
配置文件如下
! Configuration File for keepalived
global_defs {
notification_email {
acassen@firewall.loc
failover@firewall.loc
sysadmin@firewall.loc
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id LVS_DEVEL
vrrp_skip_check_adv_addr
vrrp_garp_interval 0
vrrp_gna_interval 0
}
vrrp_instance VI_1 {
state BACKUP
interface ens33
virtual_router_id 55
priority 50
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.32.201
}
}
virtual_server 192.168.32.201 80 {
delay_loop 6
lb_algo rr
lb_kind DR
persistence 60
protocol TCP
real_server 192.168.32.150 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 4
}
}
real_server 192.168.32.129 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 4
}
}
}
systemctl start keepalived
nginx配置
echo 1 > /proc/sys/net/ipv4/conf/ens32/arp_ignore
echo 2 > /proc/sys/net/ipv4/conf/ens32/arp_announce
echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore
echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce
ifconfig lo:0 192.168.32.201 netmask 255.255.255.255 //填写自己的虚拟ip