使用三台服务器搭建lnmp架构

  • 在工作环境中,lnmp架构通常不会再单独的一台服务器上搭建,一般都是分离的,上一篇文章演示的是在一台服务器上搭建的方法,这里演示下多台服务器组合搭建lnmp架构。
环境说明
系统 IP 服务
redhat7 192.168.225.128 nginx
redhat7 192.168.225.129 mysql
redhat7 192.168.225.130 php

lnmp部署流程

nginx --> mysql --> php
  • 在192.168.225.128上安装nginx
//创建系统用户nginx
[root@hxdserver ~]# groupadd -r nginx
[root@hxdserver ~]# useradd -r -M -s /sbin/nologin nginx -g nginx
//安装依赖环境
[root@hxdserver ~]# yum -y install pcre-devel openssl openssl-devel gd-devel gcc gcc-c++
[root@hxdserver ~]# yum -y groups install 'Development Tools'

//创建日志存放目录
[root@hxdserver ~]# mkdir -p /var/log/nginx
[root@hxdserver ~]# chown -R nginx.nginx /var/log/nginx/
//下载nginx
[root@hxdserver ~]# cd /usr/src/
[root@hxdserver src]# wget http://nginx.org/download/nginx-1.14.0.tar.gz --2018-08-20 11:19:09--  http://nginx.org/download/nginx-1.14.0.tar.gz
[root@hxdserver src]# ls
debug  kernels  nginx-1.14.0.tar.gz
//编译安装
[root@hxdserver src]# tar xf nginx-1.14.0.tar.gz 
[root@hxdserver nginx-1.14.0]# cd nginx-1.14.0/
[root@hxdserver nginx-1.14.0]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-debug --with-http_ssl_module --with-http_realip_module --with-http_image_filter_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_stub_status_module --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log
[root@hxdserver nginx-1.14.0]# make && make install

//nginx安装后配置
//设置环境变量
[root@hxdserver ~]# echo 'export PATH=/usr/local/nginx/sbin:$PATH' > /etc/profile.d/nginx.sh
[root@hxdserver ~]# . /etc/profile.d/nginx.sh
//启动nginx
[root@hxdserver ~]# nginx 
[root@hxdserver ~]# ss -antl
State      Recv-Q Send-Q Local Address:Port               Peer Address:Port              
LISTEN     0      128          *:80                       *:*                  
LISTEN     0      128          *:22                       *:*                  
LISTEN     0      100    127.0.0.1:25                       *:*                  
LISTEN     0      128         :::22                      :::*                  
LISTEN     0      100        ::1:25                      :::* 

//关闭防火墙
[root@hxdserver ~]# systemctl stop firewalld.service
//关闭selinux
[root@hxdserver ~]# setenforce 0

在192.168.225.129安装mysql

//安装依赖包
[root@hxdserver ~]# yum -y install ncurses-devel openssl-devel openssl cmake mariadb-devel
//创建mysql用户和组
[root@hxdserver ~]# groupadd -r -g 306 mysql
[root@hxdserver ~]# useradd -M -s /sbin/nologin -g 306 -u 306 mysql

//下载二进制格式的mysql软件包

[root@hxdserver local]# ls
apache    bin    include  libexec                                     share
apr       etc    lib      mysql-5.7.23-linux-glibc2.12-x86_64.tar.gz  src
apr-util  games  lib64    sbin

[root@hxdserver local]# tar xf mysql-5.7.23-linux-glibc2.12-x86_64.tar.gz
[root@hxdserver local]# ln -sv mysql-5.7.23-linux-glibc2.12-x86_64/ mysql
"mysql" -> "mysql-5.7.23-linux-glibc2.12-x86_64/"

//修改目录/usr/local/mysql的属主属组
[root@hxdserver local]# chown -R mysql.mysql /usr/local/mysql
[root@hxdserver local]# ll /usr/local/mysql -d
lrwxrwxrwx. 1 mysql mysql 36 9月  27 10:28 /usr/local/mysql -> mysql-5.7.23-linux-glibc2.12-x86_64/

//添加环境变量
[root@hxdserver local]# cd
[root@hxdserver ~]# ls /usr/local/mysql
bin  COPYING  docs  include  lib  man  README  share  support-files
[root@hxdserver ~]# echo 'export PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
[root@hxdserver ~]# . /etc/profile.d/mysql.sh 
[root@hxdserver ~]# echo $PATH
/usr/local/mysql/bin:/usr/local/apache/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin

//建立数据存放目录
[root@hxdserver ~]# mkdir /opt/data
[root@hxdserver ~]# chown -R mysql.mysql /opt/data/
[root@hxdserver ~]# ll /opt/
总用量 0
drwxr-xr-x. 2 mysql mysql 6 9月  27 10:36 data

//初始化数据库
[root@hxdserver ~]# /usr/local/mysql/bin/mysqld --initialize --user=mysql --datadir=/opt/data/
2018-10-18T10:06:27.093840Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2018-10-18T10:06:27.742780Z 0 [Warning] InnoDB: New log files created, LSN=45790
2018-10-18T10:06:27.821871Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2018-10-18T10:06:29.215545Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 7b2f63b5-d2bd-11e8-9110-000c294b7cb8.
2018-10-18T10:06:29.216758Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2018-10-18T10:06:29.580008Z 1 [Note] A temporary password is generated for root@localhost: ur1),jhW!Isp

//配置mysql
[root@hxdserver ~]# ln -sv /usr/local/mysql/include/ /usr/local/include/mysql
"/usr/local/include/mysql" -> "/usr/local/mysql/include/"
[root@hxdserver ~]# echo '/usr/local/mysql/lib' > /etc/ld.so.conf.d/mysql.conf
[root@hxdserver ~]# ldconfig -v
[root@hxdserver ~]# ldconfig -p |grep mysql
	libmysqlclient.so.20 (libc6,x86-64) => /usr/local/mysql/lib/libmysqlclient.so.20
	libmysqlclient.so.18 (libc6,x86-64) => /usr/lib64/mysql/libmysqlclient.so.18
	libmysqlclient.so (libc6,x86-64) => /usr/lib64/mysql/libmysqlclient.so
	libmysqlclient.so (libc6,x86-64) => /usr/local/mysql/lib/libmysqlclient.so

//生成配置文件
[root@hxdserver ~]# vim /etc/my.cnf
[root@hxdserver ~]# cat /etc/my.cnf
[mysqld]
basedir=/usr/local/mysql
datadir=/opt/data
socket=/tmp/mysql.sock
port=3306
pid-file=/opt/data/mysql.pid
user=mysql
skip-name-resolve

//配置服务启动脚本
[root@hxdserver ~]# cp -a /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
[root@hxdserver ~]# sed -ri 's#^(basedir=).*#\1/usr/local/mysql#g' /etc/init.d/mysqld 
[root@hxdserver ~]# sed -ri 's#^(datadir=).*#\1/opt/data/#g' /etc/init.d/mysqld 

//启动mysql
[root@hxdserver ~]# service mysqld start 
Starting MySQL.Logging to '/opt/data/hxdserver.err'.
 SUCCESS! 
[root@hxdserver ~]# ps -ef |grep mysql
root      57089      1  0 11:09 pts/1    00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/opt/data --pid-file=/opt/data/mysql.pid
mysql     57267  57089  3 11:09 pts/1    00:00:00 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/opt/data --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=hxdserver.err --pid-file=/opt/data/mysql.pid --socket=/tmp/mysql.sock --port=3306
root      57297  12647  0 11:10 pts/1    00:00:00 grep --color=auto mysql
[root@hxdserver ~]# ss -antl
State      Recv-Q Send-Q Local Address:Port               Peer Address:Port              
LISTEN     0      128              *:80                           *:*                  
LISTEN     0      128              *:22                           *:*                  
LISTEN     0      100      127.0.0.1:25                           *:*                  
LISTEN     0      128             :::22                          :::*                  
LISTEN     0      100            ::1:25                          :::*                  
LISTEN     0      80              :::3306                        :::* 
//修改密码
//使用初始密码登录
[root@hxdserver ~]# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.23

Copyright (c) 2000, 2018, 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> set password = password('hxd123456.');
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> exit
Bye


在192.168.225.130上安装php

//配置yum源
[root@hxdserver ~]# cd /etc/yum.repos.d/
[root@hxdserver yum.repos.d]#  wget http://mirrors.163.com/.help/CentOS7-Base-163.repo

[root@hxdserver yum.repos.d]#  sed -i 's/\$releasever/7/g' /etc/yum.repos.d/CentOS7-Base-163.repo 
[root@hxdserver yum.repos.d]#  sed -i 's/^enabled=.*/enabled=1/g' /etc/yum.repos.d/CentOS7-Base-163.repo 
[root@hxdserver yum.repos.d]# yum clean all 

[root@hxdserver yum.repos.d]# vim xx.repo 
//这里将本地源关闭了
[root@hxdserver yum.repos.d]# yum install -y  libxml2 libxml2-devel openssl openssl-devel bzip2 bzip2-devel libcurl libcurl-devel libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel gmp gmp-devel libmcrypt libmcrypt-devel readline readline-devel libxslt libxslt-devel mhash mhash-devel 

//下载php
[root@hxdserver src]# wget  http://cn.php.net/distributions/php-7.2.8.tar.xz
[root@hxdserver src]# tar xf php-7.2.8.tar.xz
[root@hxdserver src]# cd php-7.2.8/
[root@hxdserver php-7.2.8]# ./configure --prefix=/usr/local/php7 --with-curl  --with-freetype-dir  --with-gd  --with-gettext  --with-iconv-dir  --with-kerberos  --with-libdir=lib64  --with-libxml-dir=/usr  --with-openssl  --with-pcre-regex  --with-pdo-mysql  --with-pdo-sqlite  --with-pear  --with-jpeg-dir  --with-png-dir  --with-xmlrpc  --with-xsl  --with-zlib  --with-config-file-path=/etc  --with-config-file-scan-dir=/etc/php.d  --with-bz2  --enable-fpm  --enable-bcmath  --enable-libxml  --enable-inline-optimization  --enable-mbregex  --enable-mbstring  --enable-opcache  --enable-pcntl  --enable-shmop  --enable-soap  --enable-sockets  --enable-sysvsem --enable-xml  --enable-zip

[root@hxdserver php-7.2.8]#  make 
当配置PHP时出现  make: *** [ext/fileinfo/libmagic/apprentice.lo] Error 1 时,是因为服务器内存不足1G。
只需要在配置命令中添加 --disable-fileinfo即可 。
[root@hxdserver php-7.2.8]# make install
//安装后配置
[root@hxdserver ~]# echo 'export PATH=/usr/local/php7/bin:$PATH' > /etc/profile.d/php7.sh
[root@hxdserver ~]# source /etc/profile.d/php7.sh 
[root@hxdserver ~]# which php 
/usr/local/php7/bin/php
[root@hxdserver ~]# php -v 
PHP 7.2.8 (cli) (built: Sep 27 2018 12:01:24) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies

//配置php-fpm
[root@hxdserver php-7.2.8]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm 
[root@hxdserver php-7.2.8]# chmod +x /etc/rc.d/init.d/php-fpm 
[root@hxdserver php-7.2.8]# cp /usr/local/php7/etc/php-fpm.conf.default /usr/local/php7/etc/php-fpm.conf
[root@hxdserver php-7.2.8]#  cp /usr/local/php7/etc/php-fpm.d/www.conf.default /usr/local/php7/etc/php-fpm.d/www.conf

//编辑php-fpm的配置文件/usr/local/php7/etc/php-fpm.conf 
//配置fpm的相关选项改为所需要的值
[root@hxdserver php-7.2.8]# vim /usr/local/php7/etc/php-fpm.conf
[root@hxdserver php-7.2.8]#  tail /usr/local/php7/etc/php-fpm.conf 
; files from a glob(3) pattern. This directive can be used everywhere in the
; file.
; Relative path can also be used. They will be prefixed by:
;  - the global prefix if it's been set (-p argument)
;  - /usr/local/php7 otherwise
include=/usr/local/php7/etc/php-fpm.d/*.conf
pm.max_children = 50
pm.start_servers = 5 
pm.min_spare_servers = 2 
pm.max_spare_servers = 8

//启动php-fpm
[root@hxdserver php-7.2.8]# service php-fpm start 
Starting php-fpm  done
[root@hxdserver php-7.2.8]#  ss -antl 
State      Recv-Q Send-Q Local Address:Port               Peer Address:Port              
LISTEN     0      128              *:80                           *:*                  
LISTEN     0      128              *:22                           *:*                  
LISTEN     0      100      127.0.0.1:25                           *:*                  
LISTEN     0      128      127.0.0.1:9000                         *:*                  
LISTEN     0      128             :::22                          :::*                  
LISTEN     0      100            ::1:25                          :::*                  
LISTEN     0      80              :::3306                        :::*

//fpm默认监听在127.0.0.1的9000端口

在192.168.225.128上修改nginx的配置文件

[root@hxdserver ~]# vim /usr/local/nginx/conf/nginx.conf
#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/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  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;
 #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html 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   html;
        }
}
server {
       listen  8000;
       server_name www.dubai666666.com;
       location / {
 root   /www/ddd;
            index index.php index.html index.htm ;
  }
  location ~ \.php$ {
  root        /www/ddd;
  fastcgi_pass  192.168.225.130:9000;
  fastcgi_index index.php;
  fastcgi_param SCRIPT_FILENAME  /www/ddd$fastcgi_script_name;
  include        fastcgi_params;
        # 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           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$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;
    #    }
    }

}

修改192.168.255.130的/usr/local/php7/etc/php-fpm.d/www.conf文件

[root@localhost ~]# vim /usr/local/php7/etc/php-fpm.d/www.conf
listen = 192.168.255.130:9000
;listen.allowed_clients = 192.168.225.128

创建网页目录和文件

[root@localhost ~]# mkdir /www/ddd/ -p
[root@localhost ~]# vim /www/ddd/index.php

[root@localhost ~]# service php-fpm restart

验证
使用三台服务器搭建lnmp架构_第1张图片

你可能感兴趣的:(使用三台服务器搭建lnmp架构)