图解Linux/Nginx/MySQL/PHP 架构流程
安装软件包 版本信息:
Linux:CentOS 7.6
Nginx:nginx/1.24.0
PHP:PHP 7.1.33
MariaDB: 5.5.68-MariaDB
安装Nginx
参考 学习笔记——Nginx部署篇
,确保不同机器的Linux版本号一致,安装的Nginx版本一致。
安装PHP
安装PHP的组件yum源
1 2 3 4 wget https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm rpm -Uvh epel-release-latest-7.noarch.rpm wget https://mirror.webtatic.com/yum/el7/webtatic-release.rpm --no-check-certificate rpm -Uvh webtatic-release.rpm
安装PHP服务
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 rpm -e $(rpm -qa | grep php) yum install php71w php71w-cli php71w-common php71w-devel php71w-embedded php71w-gd -y yum install php71w-mcrypt php71w-mbstring php71w-pdo php71w-xml php71w-fpm php71w-mysqlnd -y yum install php71w-opcache php71w-pecl-memcached php71w-pecl-redis php71w-pecl-mongodb -y egrep '^user|^group' www.conf sed -i '/^user/c user = www' www.conf sed -i '/^group/c group = www' www.conf egrep '^user|^group' www.conf systemctl start php-fpm systemctl enable php-fpm lsof -i:9000
安装mariadb
1 yum install mariadb-server mariadb -y
nginx配置php虚拟主机
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 server { listen 80; server_name blog; root /data/blog; location / { index index.php default.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; } mkdir -p /data/blogcd /data/blogchown -R www.www /data/blog
测试
1 2 3 4 5 6 7 8 9 10 11 12 13 <?php $servername = "localhost" ; $username = "root" ; $password = "123456" ; $conn = mysqli_connect ($servername , $username , $password ); if (!$conn ){ die ("Connection failed: " . mysqli_connect_error ()); } echo "数据库连接成功" ; ?>
浏览器访问:http://blog/test_mysql.php
单机部署WordPress 1 2 3 4 5 6 7 8 9 10 11 12 13 14 cd /server/toolswget https://cn.wordpress.org/latest-zh_CN.tar.gz tar xf latest-zh_CN.tar.gz -C /data/blog/ mv /data/blog/wordpress/* /data/blog/ chown -R www.www /data/blog create database blog character set utf8mb4 collate utf8mb4_unicode_ci; show create database blog; grant all privileges on blog.* to 'blog' @'192.168.110.%' identified by '123456' ; flush privileges;
独立数据库服务器 数据库的瓶颈主要来源于CPU、磁盘I/O和内存,将数据库部署到独立的服务器,具有以下优点:
缓解web网站的压力
增强数据库的读写性能
提高用户访问速度
1 2 3 4 5 6 7 8 9 10 11 12 mysqldump -uroot -p123456 -B -A | gzip > /tmp/bak.sql.gz scp /tmp/bak.sql.gz root@192.168.110.143:/tmp/ yum install mariadb-server mariadb -y systemctl start mariadb systemctl enable mariadb cd /tmpgzip -d bak.sql.gz mysql < /tmp/bak.sql
扩展多台web服务器 按照之前的步骤,安装Nginx和PHP
1 2 3 4 5 6 scp -rp root@web01:/etc/nginx /etc/ scp -rp root@web01:/data/blog /data/ systemctl restart nginx systemctl restart php-fpm
拆分静态资源到独立服务器 NFS的搭建流程参考 传统网络存储——NAS
篇
WordPress的静态资源存放在/data/blog/wp-content/uploads
目录下
1 2 3 scp -rp /data/blog/wp-content/* root@nfs:/data/blog/ mount -t nfs nfs:/data/blog /data/blog/wp-content/uploads/ df -Th
集成备份服务 在web01服务器上编写脚本,定时备份到backup服务器上(因为web01和web02内容相同,所以备份一台即可)
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 28 #!/bin/bash backup="/backup" path="/backup/$(hostname -i) " [ -f $path ] || mkdir -p $path if [ $(date +%w -eq 2)];then date ="$(date +%F -d -1day) _week1" else date ="$(date +%F -d -1day) " fi cd $path tar -cvhf conf_${date} .tar.gz /var/spool/cron/root /server/scripts /etc 2>/dev/null tar -cvf www_${date} .tar.gz /data --exclude=/data/blog/wp-content 2>/dev/null tar -cvf logs_${date} .tar.gz /var/log/nginx 2>/dev/null find $path -type f -name "*_${date} .tar.gz" | xargs md5sum > $path /$(hostname -i)_fingerprint_${date} .log rsync -avz $backup / rsync@backup::backup --password-file=/etc/rsyncd.password find $backup / -type f -name "*.tar.gz" -mtime +7 -o -name "*.log" -mtime +7 | xargs rm -f