Lnmp-blog,zh及lnmp平台分离部署

搭建lnmp

L:linux系统
N:nginx
M:mysql
P:php

一、安装nginx

使用Nginx官方提供的rpm包

[root@lnmp ~]# vim /etc/yum.repos.d/nginx.repo
添加:
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=0
enabled=1

执行yum安装及启动并开机自启

[root@lnmp ~]# yum -y install nginx
[root@lnmp ~]# systemctl start nginx
[root@lnmp ~]# systemctl enable nginx
[root@lnmp ~]# nginx -V
nginx version: nginx/1.18.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC) 
built with OpenSSL 1.0.2k-fips  26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie'

二、 安装php

使用第三方扩展epel源安装PHP7.2:

rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm

安装 php72 版本

yum -y install php72w php72w-cli php72w-common php72w-devel \
php72w-embedded php72w-gd php72w-mbstring php72w-pdo php72w-xml php72w-fpm \
php72w-mysqlnd php72w-opcache

这里我用提前准备好的包

[root@lnmp php-7.2]# ls
autoconf-2.69-11.el7.noarch.rpm
automake-1.13.4-3.el7.noarch.rpm
libargon2-20161029-3.el7.x86_64.rpm
m4-1.4.16-10.el7.x86_64.rpm
mod_php72w-7.2.27-1.w7.x86_64.rpm
pcre-devel-8.32-17.el7.x86_64.rpm
perl-Data-Dumper-2.145-3.el7.x86_64.rpm
perl-Test-Harness-3.28-3.el7.noarch.rpm
perl-Thread-Queue-3.02-2.el7.noarch.rpm
php72w-cli-7.2.27-1.w7.x86_64.rpm
php72w-common-7.2.27-1.w7.x86_64.rpm
php72w-devel-7.2.27-1.w7.x86_64.rpm
php72w-embedded-7.2.27-1.w7.x86_64.rpm
php72w-fpm-7.2.27-1.w7.x86_64.rpm
php72w-gd-7.2.27-1.w7.x86_64.rpm
php72w-mbstring-7.2.27-1.w7.x86_64.rpm
php72w-mysqlnd-7.2.27-1.w7.x86_64.rpm
php72w-opcache-7.2.27-1.w7.x86_64.rpm
php72w-pdo-7.2.27-1.w7.x86_64.rpm
php72w-xml-7.2.27-1.w7.x86_64.rpm

安装,启动并开机自启

[root@lnmp php-7.2]# yum -y install localinstall *.rpm
[root@lnmp ~]# systemctl start php-fpm
[root@lnmp ~]# systemctl enable php-fpm

三、安装mysql

安装mysql(mariadb):

rpm -ivh http://repo.mysql.com/yum/mysql-5.6-community/el/7/x86_64/mysql-community-release-el7-5.noarch.rpm

yum install mysql-community-server -y

同样我这里使用提前准备好的包

[root@lnmp mysql5.6]# ls
mysql-community-client-5.6.47-2.el7.x86_64.rpm
mysql-community-common-5.6.47-2.el7.x86_64.rpm
mysql-community-libs-5.6.47-2.el7.x86_64.rpm
mysql-community-server-5.6.47-2.el7.x86_64.rpm
perl-Compress-Raw-Bzip2-2.061-3.el7.x86_64.rpm
perl-Compress-Raw-Zlib-2.061-4.el7.x86_64.rpm
perl-Data-Dumper-2.145-3.el7.x86_64.rpm
perl-DBI-1.627-4.el7.x86_64.rpm
perl-IO-Compress-2.061-2.el7.noarch.rpm
perl-Net-Daemon-0.48-5.el7.noarch.rpm
perl-PlRPC-0.2020-14.el7.noarch.rpm

安装,启动并开机自启

[root@lnmp mysql5.6]# yum -y install localinstall *.rpm
[root@lnmp ~]# systemctl start mysqld
[root@lnmp ~]# systemctl enable mysqld

修改mysql密码:

扫描二维码关注公众号,回复: 12461955 查看本文章
[root@lnmp ~]# mysql_secure_installation    //除了新密码一律下一个

应用1:搭建wordpress

(1)下载wordpress源码包

(2)复制wordpress安装包,到虚拟机/

[root@lnmp ~]# cd /
[root@lnmp /]# ls
bin   etc   lib64  opt   run   sys  var
boot  home  media  proc  sbin  tmp  wordpress-4.9.4-zh_CN.zip
dev   lib   mnt    root  srv   usr

解压

[root@lnmp /]# yum -y install unzip
[root@lnmp /]# unzip wordpress-4.9.4-zh_CN.zip

赋权

[root@lnmp /]# chmod -R 777 /wordpress

备份

[root@lnmp /]# cd /etc/nginx/conf.d/
[root@lnmp conf.d]# ls
default.conf
[root@lnmp conf.d]# mv default.conf default.conf.bak

(3)创建虚拟主机配置文件

[root@lnmp ~]# vim /etc/nginx/conf.d/blog.conf

server {
    
    
        listen 80;
        server_name blog.benet.com;
        root /wordpress;
        index index.php index.html;

        location ~ \.php$ {
    
    
                root /wordpress;
                fastcgi_pass 127.0.0.1:9000;
                fastcgi_index index.php;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                include fastcgi_params;
        }
} 

重新加载配置文件

[root@lnmp ~]# systemctl reload nginx

(4)创建blog数据库和管理用户

登录数据库:

[root@lnmp ~]# mysql -uroot -p123.com
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 14
Server version: 5.6.47 MySQL Community Server (GPL)

Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> 

创建数据库:

mysql> create database blog;
Query OK, 1 row affected (0.01 sec)

设置管理用户及密码:

mysql> grant all on blog.* to lisi@localhost identified by '123.com';
Query OK, 0 rows affected (0.01 sec)

(5)客户端通过域名访问blog,安装并配置
我的电脑 > 本地磁盘 > Windows > system32 > drivers > etc > hosts >添加:192.168.1.20 blog.benet.com

http://blog.benet.com
登录后台
http://blog.benet.com/wp-admin
在这里插入图片描述
在这里插入图片描述

应用2:搭建知乎wecenter

(1)下载wecenter源码包

(2)复制wecenter安装包,到虚拟机/zh

[root@lnmp ~]# mkdir /zh
[root@lnmp zh]# ls
WeCenter_3-3-4.zip

解压

[root@lnmp zh]# unzip WeCenter_3-3-4.zip

赋权

[root@lnmp zh]# chmod -R 777 /zh

(3) 创建虚拟主机配置文件

[root@lnmp ~]# vim /etc/nginx/conf.d/zh.conf
server {
    
    
        listen 80;
        server_name zh.benet.com;
        root /zh;
        index index.php index.html;

        location ~ \.php$ {
    
    
                root /zh;
                fastcgi_pass 127.0.0.1:9000;
                fastcgi_index index.php;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                include fastcgi_params;
        }
}

重新加载配置文件

[root@lnmp ~]# systemctl reload nginx

(4)创建blog数据库和管理用户
登录数据库:

[root@lnmp ~]# mysql -uroot -p123.com

创建数据库:

mysql> create database zh;
Query OK, 1 row affected (0.00 sec)

设置管理用户及密码:

mysql> grant all on zh.* to wangwu@localhost identified by '123.com';
Query OK, 0 rows affected (0.00 sec)

(5)客户端通过域名访问zh,安装并配置

http://zh.benet.com
登录后台
http://zh.benet.com/?/admin

在这里插入图片描述
在这里插入图片描述

lnmp平台的分离部署

环境:
web1:192.168.1.20 //lnmp
web2:192.168.1.21
mysql:192.168.1.22
php:192.168.1.23
nfs:192.168.1.24
lb:192.168.1.25

一、lnmp实现mysql数据库迁移

为了实现mysql独立运行,拆分lnmp

(1)另起一台centos7,安装mysql

[root@mysql ~]# rpm -ivh http://repo.mysql.com/yum/mysql-5.6-community/el/7/x86_64/mysql-community-release-el7-5.noarch.rpm
[root@mysql ~]# yum -y install mysql-community-server
启动并开机自启
[root@mysql ~]# systemctl start mysqld
[root@mysql ~]# systemctl enable mysqld

修改mysql密码:

[root@mysql ~]# mysql_secure_installation

(2)把原lnmp服务器上的数据库文件导出

[root@web1 ~]# mysqldump -uroot -p123.com --all-databases > `date +%F%H`-mysql-all.sql
[root@web1 ~]# scp 2021-01-1720-mysql-all.sql [email protected]:/

(3)在新的mysql服务器上导入数据库文件

[root@mysql ~]# mysql -uroot -p123.com < 2021-01-1720-mysql-all.sql

重启

[root@mysql ~]# systemctl restart mysqld

(4)在新mysql服务器上创建同名管理用户和密码

[root@mysql ~]# mysql -uroot -p123.com         //登录 

mysql> grant all on blog.* to lisi@'%' identified by '123.com';
Query OK, 0 rows affected (0.00 sec)

mysql> grant all on zh.* to wangwu@'%' identified by '123.com';
Query OK, 0 rows affected (0.01 sec)

(5)在原服务器上修改blog、zh的配置文件,重新指定数据库服务器ip

查看blog的配置位置:

[root@web1 ~]# cd /wordpress/       //切换到blog网页根目录
[root@web1 ~]# grep -R 123.com      //搜索保存密码的配置文件位置
[root@web1 ~]# vim /wordpress/wp-config.php
修改:
define('DB_NAME', 'blog');       //WordPress数据库的名称

/** MySQL数据库用户名 */
define('DB_USER', 'lisi');      //MySQL数据库用户名  

/** MySQL数据库密码 */
define('DB_PASSWORD', '123.com');      //MySQL数据库密码

/** MySQL主机 */
define('DB_HOST', '192.168.1.22');     //新MySQL主机ip

查看zh的配置文件位置:

[root@web1 ~]# cd /zh/
[root@web1 ~]# grep -R 123.com
[root@web1 ~]# vim /zh/system/config/database.phpcd /zh/
修改:
'host' => '192.168.1.22',      //新MySQL主机ip
  'username' => 'wangwu',      //MySQL数据库用户名
  'password' => '123.com',     //MySQL数据库密码
  'dbname' => 'zh',            //zh数据库的名称

关闭原服务器数据库

[root@web1 ~]# systemctl stop mysqld

二、拆分php

(1)启动一台centos7,安装php

[root@php ~]# rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm

[root@php ~]# rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm

[root@php ~]#  yum -y install php72w php72w-cli php72w-common php72w-devel php72w-embedded php72w-gd php72w-mbstring php72w-pdo php72w-xml php72w-fpm php72w-mysqlnd php72w-opcache

(2)修改nginx服务器上的配置文件,重新指向新的php服务器
修改blog

[root@web1 ~]# vim /etc/nginx/conf.d/blog.conf
修改:
server {
    
    
        listen 80;
        server_name blog.benet.com;
        root /wordpress;
        index index.php index.html;

        location ~ \.php$ {
    
    
                root /wordpress;
                fastcgi_pass 192.168.1.23:9000;
                fastcgi_index index.php;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                include fastcgi_params;
        }
}

修改zh

[root@web1 ~]# vim /etc/nginx/conf.d/zh.conf
server {
    
    
        listen 80;
        server_name zh.benet.com;
        root /zh;
        index index.php index.html;

        location ~ \.php$ {
    
    
                root /zh;
                fastcgi_pass 192.168.1.23:9000;
                fastcgi_index index.php;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                include fastcgi_params;
        }
}

(3)修改php服务器的配置文件

[root@php ~]# vim /etc/php-fpm.d/www.conf
定位并修改为:
listen = 192.168.1.23:9000
listen.allowed_clients = 192.168.1.20,192.168.1.21

重启php

[root@php ~]# systemctl restart php-fpm

(4)从nginx服务器复制wordpress和zh的安装目录到php服务器

[root@web1 ~]# scp -rp /wordpress [email protected]:/
[root@web1 ~]# scp -rp /zh [email protected]:/

关闭原服务器php:

[root@web1 ~]# systemctl stop php-fpm

(5)客户端验证访问

在做好的域名解析访问:
http://blog.benet.com
http://zh.benet.com

三、搭建nfs共享服务器,把网站静态元素通过挂载方式放在nfs上

(1)开启一台centos7,安装nfs-utils、rpcbind:

[root@nfs ~]# yum -y install nfs-utils  rpcbind

(2)创建挂载点

[root@nfs ~]# mkdir -p /nfs/{blog,zh}

(3)发布共享目录

[root@nfs ~]# vim /etc/exports
添加:
/nfs/blog       192.168.1.0/24(rw,sync,no_root_squash)
/nfs/zh         192.168.1.0/24(rw,sync,no_root_squash)

(4)重启nfs服务

[root@nfs ~]# systemctl restart rpcbind
[root@nfs ~]# systemctl restart nfs

(5)在nginx服务器上查看nfs共享目录

[root@web1 ~]# showmount -e 192.168.1.24
Export list for 192.168.1.24:
/nfs/zh   192.168.1.0/24
/nfs/blog 192.168.1.0/24

(6)把wordpress的内容目录挂载到nfs

[root@web1 ~]# cd /wordpress/
[root@web1 wordpress]# cp -rp wp-content/ wp-contentbak
[root@web1 wordpress]# mount -t nfs 192.168.1.24:/nfs/blog wp-content
[root@web1 wordpress]# cp -rp wp-contentbak/* wp-content/

(7)设置永久挂载

[root@web1 ~]# vim /etc/fstab
添加:
192.168.1.24:/nfs/log  /wordpress/wp-content   nfs     defaults        0 0

四、搭建第二台web服务器

(1)安装ninx

[root@web2 ~]# vim /etc/yum.repos.d/nginx.repo
添加:
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=0
enabled=1
保存退出
[root@web2 ~]# yum -y install nginx

(2)把web1(192.168.1.20)上的nginx的配置复制到web2(192.168.1.21)

[root@web1 ~]# scp -rp /etc/nginx/* [email protected]:/etc/nginx/

(3)把web1上网页源码复制到web2

[root@web1 ~]# scp -rp /wordpress [email protected]:/
[root@web1 ~]# scp -rp /zh [email protected]:/

(4)启动服务

[root@web2 ~]# systemctl start nginx

五、搭建负载均衡

(1)安装nginx

[root@lb ~]# vim /etc/yum.repos.d/nginx.repo
添加:
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=0
enabled=1
保存退出
[root@lb ~]# yum -y insyall nginx

(2)配置负载均衡

[root@lb ~]# vim /etc/nginx/conf.d/lb.conf
添加:
server {
    
    
        listen 80;
        server_name blog.benet.com;

        location / {
    
    
                proxy_pass      http://webcluster;
                proxy_set_header Host $http_host;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }
}
server {
    
    
        listen 80;
        server_name zh.benet.com;

        location / {
    
    
                proxy_pass      http://webcluster;
                proxy_set_header Host $http_host;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }
}

重启nginx

[root@lb ~]# systemctl restart nginx

至此lnmp环境分离部署就做完了

域名解析:

我的电脑 > 本地磁盘 > Windows > system32 > drivers > etc > hosts >
添加:
zh.benet.com 192.168.1.20
blog.benet.com 192.168.1.21

浏览器访问:

http://blog.benet.com
在这里插入图片描述
http://zh.benet.com
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/hjyhjy0/article/details/112681943