分布式构建lnmp,小白都能学会

前言:

之前的博文写过分布式安装LAMP搭建Discuz https://blog.csdn.net/weixin_43815140/article/details/105031305
rpm搭建nginx,mysql,php于一身的lnmp
https://blog.csdn.net/weixin_43815140/article/details/105163108
今天搞个分开部署的lnmp

所需环境:
再做之前,先开放端口或者关闭防火墙和selinux,以免无法访问。

名称 IP
nginx-1.16.1(centos7.2) 192.168.10.2
mysql-5.6(centos7.2) 192.168.10.4
php7.2(centos7.2) 192.168.10.6
测试机 192.168.10.3

在192.168.10.2安装nginx

1>// 编辑官方yum源

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

2>//yum安装nginx

[root@nginx~]# yum -y install nginx
[root@nginx~]# systemctl start nginx
[root@nginx~]# systemctl enable nginx 

3>//测试nginx是否可以访问

[root@localhost ~]# curl http://192.168.10.2
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

ok,nginx已经安装好,现在安装mysql

在192.168.10.4安装mysql

1>//使用本地rpm包安装mysql,拷贝到/my目录

[root@mysql ~]# cd /my
[root@mysql my]# 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@mysql my]# 

2>//安装mysql

[root@mysql my]# yum -y localinstall /my/*
[root@mysql my]# systemcal start mysqld

3>//监听端口,确认是否开启

[root@lnmp ~]# netstat -anpt | grep 3306
tcp6       0      0 :::3306                 :::*                    LISTEN      3634/mysqld         
[root@localhost ~]# 

4>//给MySQL设置密码

[root@lnmp ~]# mysql_secure_installation #根据提示修改密码为123.com
[root@lnmp ~]# mysql -uroot -p'123.com'  #登录mysql

在192.168.10.6安装PHP

1>//准备第三方epel扩展源

[root@phpserver ~]# rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
[root@phpserver ~]# rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm

2>//安装PHP7.2

[root@lnmp ~]# 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

3>//安装完成后,先不着急开启,修改/etc/php-fpm.d/www.conf文件

[root@phpserver ~]# cat /etc/php-fpm.d/www.conf | grep -v '^;' | grep -v '^$'
[www]
user = php
group = php
listen = 192.168.10.6:9000
listen.allowed_clients = 192.168.10.2
pm = dynamic
pm.max_children = 50
pm.start_servers = 5
pm.min_spare_servers = 5
pm.max_spare_servers = 35
slowlog = /var/log/php-fpm/www-slow.log
php_admin_value[error_log] = /var/log/php-fpm/www-error.log
php_admin_flag[log_errors] = on
php_value[session.save_handler] = files
php_value[session.save_path]    = /var/lib/php/session
php_value[soap.wsdl_cache_dir]  = /var/lib/php/wsdlcache

注意:listen = 192.168.10.6:9000 指向本机监听php-fpm;
listen.allowed_clients = 192.168.10.2 指向nginx主机

4>//开启php-fpm,监听端口

[root@phpserver ~]# systemctl start php-fpm
[root@phpserver ~]# netstat -anpt | grep php-fpm
tcp        0      0 192.168.10.6:9000       0.0.0.0:*               LISTEN      38364/php-fpm: mast 

配置nginx配置文件,使其能与php协同工作,nginx
处理一些静态页面请求,php处理动态请求

编辑nginx配置文件

修改/etc/nginx/conf.d/blog.conf虚拟主机文件(conf.d下存放的是虚拟主机文件,以这个为例。)

[root@nginx ~]# vim /etc/nginx/conf.d/blog.conf
server {
        listen 80;
        server_name blog.benet.com; #网站域名
        root    /www; #网站根目录
        index index.php index.html; #索引页

        location ~ \.php$ {
                root    /www;
                fastcgi_pass 192.168.10.6:9000; #写php-fpm的地址,转发给它工作
                fastcgi_index index.php;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                include fastcgi_params;
              }
        }
~          

注意:$document_root和root 属性一样,也可以用/wordpress代替。

客户端修改/etc/hosts文件,使其能域名解析到192.168.10.2

php创建info.php测试页,客户端访问测试

[root@phpserver ~]# cat /wordpress/info.php 
<?php
phpinfo();
?>
[root@localhost ~]# 

在这里插入图片描述
和之前一样,搭建个WordPress小博客网站,需要把源码包拷贝到nginx和php里,解压授权

创建数据库并给用户授权

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

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

创建网站根目录/www,源码包解压后移动到这里(下面步骤nginx,php都要做),因为/www目录为网站根目录,不知道里面会有什么文件,静态就交给nginx处理,动态交给php。

mkdir /www
unzip wordpress-4.9.4-zh_CN.zip
mv /wordpress/* /www
chmod -R 777 /www

客户端测试,访问blog.benet.com
在这里插入图片描述
根据提示安装就好

发布了35 篇原创文章 · 获赞 3 · 访问量 6346

猜你喜欢

转载自blog.csdn.net/weixin_43815140/article/details/105249773