Simple building of LNMP environment

Posted by Toonster on Fri, 17 Jan 2020 14:16:52 +0100

1. The user initiates the request through http protocol, and the request will first arrive at Nginx in LNMP architecture
2.Nginx will match the Location rule according to the user's request
3.Location if the matching request is static, it will be read by Nginx and returned directly locally
4.Location if the matching request is dynamic, Nginx forwards the request to fastcgi protocol
5.fastgi will give the request to PHP FPM management process after receiving it, and PHP FPM management process will call the specific working process warrap after receiving it
6. The warrap process will call the php program to parse. If it just parses the code, php directly returns
7. If there is a query database operation, php will connect to the database (user password IP) to initiate the query operation
8. The final data is composed of MySQL - > PHP - > PHP FPM - > fastcgi - > nginx - > HTTP - > user

To configure Nginx Official yum Warehouse
[root@web01 ~]# vim /etc/yum.repos.d/nginx.repo
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=0
enabled=1        




//To install nginx, the official source nginx must be installed
[root@web01 ~]# yum install -y nginx
[root@web01 ~]# nginx -v
nginx version: nginx/1.14.0v
install php
        

[root@web01 ~]# yum install php71w php71w-cli php71w-common php71w-devel php71w-embedded php71w-gd php71w-mbstring php71w-pdo php71w-xml php71w-fpm php71w-mysqlnd php71w-opcache php71w-mcrypt php71w-pecl-memcached php71w-pecl-mongodb php71w-pecl-redis php71w-pecl-zip php71w-bcmath -y
Nginx And PHP Integration
//Write the configuration file in the configuration file directory of nginx
    

[root@web01 ~]# cat /etc/nginx/conf.d/php.oldxu.com.conf
    server {
        listen 80;
        server_name php.oldxu.com;
        root /code;
        location / {
            index index.php info.php;
        }
        location ~ \.php$ {
            #Send the php request to the local 9000 port, which is the default port of php FPM
            fastcgi_pass 127.0.0.1:9000;
         #Tell PHP FPM which file under which path to parse the local
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            #Variables that contain some other relevant information
            include fastcgi_params;
        }
    }

Heavy load service

    [root@web01 ~]# systemctl restart nginx
    [root@web01 ~]# systemctl start php-fpm
    [root@web01 ~]# systemctl enable php-fpm

Prepare the environment according to the configuration file

[root@web01 ~]# mkdir /code
[root@web01 ~]# vim /code/info.php
    <?php
        phpinfo();
    ?>

Visit through browser: http://php.oldxu.com/info.php test
6.PHP and MySQL integration
Install mariadb

[root@web01 ~]# yum install mariadb-server mariadb  -y

1. Start mariadb

[root@web01 ~]# systemctl start mariadb
[root@web01 ~]# systemctl enable mariadb

        

      $servername = "localhost";
        $username = "root";
        $password = "oldxu.com";
        // Create connection
        $conn = mysqli_connect($servername, $username, $password);
        // Detection connection
        if (!$conn) {
            die("Connection failed: " . mysqli_connect_error());
        }
        echo "php Connect MySQL Database success";
        ?>

4. Execute php command to test whether mysql can be connected normally

[root@web01 ~]# php /code/tt.php
            php Connect MySQL Database success

5. Test the whole lnmp through browser access
http://php.oldxu.com/tt.php

7.LNMP architecture deployment Wordpress, Wecenter
1. Unify the user identity of nginx php

    [root@web01 ~]# groupadd -g666 www
    [root@web01 ~]# useradd -u666 -g666 www
    [root@web01 ~]# sed  -i '/^user/c user www;' /etc/nginx/nginx.conf        #Modify nginx identity
    [root@web01 ~]# sed -i '/^user/c user = www' /etc/php-fpm.d/www.conf
    [root@web01 ~]# sed -i '/^group/c group = www' /etc/php-fpm.d/www.conf
    [root@web01 ~]# systemctl restart nginx php-fpm

2. Add wordpress blog site information

[root@web01 ~]# cat /etc/nginx/conf.d/blog.oldxu.com.conf
    server {
        listen 80;
        server_name blog.oldxu.com;
        root /code/wordpress;
        client_max_body_size 100m;
        location / {
            index index.php index.html;
        }
        location ~ \.php$ {
            fastcgi_pass 127.0.0.1:9000;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include fastcgi_params;
        }
    }

2. Upload code, modify owner and group

[root@web01 ~]# cd /code
[root@web01 ~]# rz wordpress.zip   #<---
[root@web01 ~]# chown -R www.www /code/wordpress
3.To configure wordpress Libraries needed by the project
[root@web01 code]# mysql -uroot -poldxu.com     #1. Log in to mysql database
MariaDB [(none)]> create database wordpress;    #2. Create a library with wordpress name
MariaDB [(none)]> show databases;               #3. View all databases in the data
    +--------------------+
    | Database           |
    +--------------------+
    | information_schema |
    | mysql              |
    | performance_schema |
    | test               |
    | wordpress          |
    +--------------------+
    5 rows in set (0.00 sec)
MariaDB [(none)]> use wordpress;            #4. Enter wordpress Library
    Database changed
MariaDB [wordpress]> show tables;        #5. Check how many tables are in the wordpress Library
    Empty set (0.00 sec)    <--empty

4. Configure Windows Host resolution

5. Access through browser to complete initialization operation and finish

Know:
1. Prepare nginx configuration file

[root@web01 ~]# cat  /etc/nginx/conf.d/zh.oldxu.com.conf
    server {
        listen 80;
        server_name zh.oldxu.com;
        root /code/zh;
        client_max_body_size 100m;
        location / {
            index index.php;
        }
        location ~ \.php$ {
            fastcgi_pass 127.0.0.1:9000;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include fastcgi_params;
        }
    }

2. Upload code

    [root@web01 ~]# mkdir /code/zh -p
    [root@web01 ~]# cd /code/zh
    [root@web01 ~]# rz
    [root@web01 zh]# unzip WeCenter_3-3-2.zip

3. Modify authority

root@web01 zh]# chown -R www.www /code/zh/

4. create Library

[root@web01 zh]# mysql -uroot -poldxu.com
    MariaDB [(none)]> create database zh;
    MariaDB [(none)]> show databases;
    +--------------------+
    | Database           |
    +--------------------+
    | information_schema |
    | mysql              |
    | performance_schema |
    | test               |
    | wordpress          |
    | zh                 |
    +--------------------+
    6 rows in set (0.00 sec)

5. Configure WIndows Host resolution

1. Split database
Host name application environment external network address internal network address
web01 nginx+php 10.0.0.7 172.16.1.7
db01 mysql 172.16.1.51

Note: delete the IP related to resolution record 172 in / etc/hosts

1. Prepare a 172.16.1.51 database and install MariaDB server

[root@db01 ~]# yum install mariadb-server -y
    [root@db01 ~]# systemctl start mariadb
    [root@db01 ~]# systemctl enable mariadb.service
    [root@db01 ~]# netstat -lntp
    Active Internet connections (only servers)
    Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
    tcp        0      0 0.0.0.0:3306            0.0.0.0:*               LISTEN      8296/mysqld         

2. Backup 172.16.1.7 database and push it to 172.16.1.51

[root@web01 ~]#  mysqldump -uroot -p'oldxu.com' --all-databases > mysql-all.sql

3.172.16.1.51 restore database and create remote connection users
1. push

[root@web01 ~]# scp mysql-all.sql root@172.16.1.51:~

2. Log in to 51 server to recover data

  [root@db01 ~]# mysql -uroot < mysql-all.sql
    [root@db01 ~]# mysql
    MariaDB [(none)]> show databases;
    +--------------------+
    | Database           |
    +--------------------+
    | information_schema |
    | mysql              |
    | performance_schema |
    | test               |
    | wordpress          |
    | zh                 |
    +--------------------+
    6 rows in set (0.00 sec)

3. Restart the database

[root@db01 ~]# systemctl restart mariadb
    [root@db01 ~]# mysql -uroot -poldxu.com            #Password will change after restart

4. Create a remote connection user

MariaDB [(none)]> grant all privileges on *.* to 'all'@'%' identified by 'oldxu.com';

5. Remote connection test login 172.16.1.7

[root@web01 ~]# mysql -h172.16.1.51 -uall -poldxu.com
    Welcome to the MariaDB monitor.  Commands end with ; or \g.
    
    MariaDB [(none)]>

4. Apply cutover wordpress - > connect local database to connect remote database

1. stop the local database

        [root@web01 ~]# systemctl stop mariadb
        [root@web01 ~]# systemctl disable mariadb

2. Modify the information of wordpress connecting to the database

  [root@web01 wordpress]# vim /code/wordpress/wp-config.php
    /** WordPress Name of the database */
    define( 'DB_NAME', 'wordpress' );
    /** MySQL Database user name */
    define( 'DB_USER', 'all' );
    /** MySQL Database password */
    define( 'DB_PASSWORD', 'oldxu.com' );
    /** MySQL Host */
    define( 'DB_HOST', '172.16.1.51' );

3. Modify the information of wecenter connecting to the database

  [root@web01 ~]# find ./ -type f | xargs grep -Ri "oldxu.com"
    [root@web01 ~]# cat  /code/zh/system/config/database.php
    <?php
    $config['charset'] = 'utf8mb4';
    $config['prefix'] = 'aws_';
    $config['driver'] = 'MySQLi';
    $config['master'] = array (
      'charset' => 'utf8mb4',
      'host' => '172.16.1.51',
      'username' => 'all',
      'password' => 'oldxu.com',
      'dbname' => 'zh',
    );
    $config['slave'] = false;

2. Extend application node extend web node multiple web organizations together > Web Cluster

Host name application environment external network address internal network address
web01 nginx+php 10.0.0.7 172.16.1.7
web02 nginx+php 10.0.0.8 172.16.1.8
db01 mysql 172.16.1.51
1. Initialize web02

        [root@web02 ~]# groupadd -g666 www
        [root@web02 ~]# useradd -u666 -g666 www
        [root@web02 ~]# scp  172.16.1.7:/etc/yum.repos.d/* /etc/yum.repos.d/

2. Install the corresponding environment on the node to be extended: nginx+php

        [root@web02 ~]# yum install nginx -y
        
        [root@web02 ~]# rpm -e $(rpm -qa |grep php)
        [root@web02 ~]# yum install php71w php71w-cli php71w-common php71w-devel php71w-embedded php71w-gd php71w-mbstring php71w-pdo php71w-xml php71w-fpm php71w-mysqlnd php71w-opcache php71w-mcrypt php71w-pecl-memcached php71w-pecl-mongodb php71w-pecl-redis php71w-pecl-zip php71w-bcmath -y

3. Copy the nginx php-fpm php.ini configuration and code on web01 to web02
1.nginx.conf nginx virtualHost

[root@web02 ~]# rsync -avz --delete 172.16.1.7:/etc/nginx/ /etc/nginx/

2.php-fpm.d/www.conf php.ini

[root@web02 ~]# rsync -avz 172.16.1.7:/etc/php.ini /etc/php.ini
            [root@web02 ~]# rsync -avz 172.16.1.7:/etc/php-fpm.d/www.conf /etc/php-fpm.d/www.conf

3. Code directory / code

[root@web02 ~]# rsync -avz --delete 172.16.1.7:/code /
            [root@web02 ~]# ll /code/
            drwxr-xr-x  5 www www     4096 12 Month 612:41 wordpress
            drwxr-xr-x 15 www www     4096 12 Month 611:44 zh

4. Reload nginx PHP FPM service
1. Check the syntax of nginx and php for errors

[root@web02 ~]# nginx -t
            [root@web02 ~]# php-fpm  -t

2. Reload nginx PHP FPM program

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

3. Add nginx PHP FPM to boot

  [root@web02 ~]# systemctl enable nginx php-fpm

5. Test tail -f

Multiple application nodes will bring new problems:
    1. Are static resources inconsistent?
    2. Multiple nodes cannot work at the same time?
        0. Manual DNS switching
        1.DNS polling (all web nodes need to have public IP)
            Unsafe
            High cost
            No monitoring checks
        2.nginx agent - > Load Balancing

3. Solve the inconsistency of static resources of multiple web nodes?
4. Rapid expansion of a web node environment planning

Host name application environment external network address internal network address
web01 nginx+php 10.0.0.7 172.16.1.7
web02 nginx+php 10.0.0.8 172.16.1.8
nfs nfs 172.16.1.31
db01 mysql 172.16.1.51

1. Prepare an NFS shared directory?
#Initialize environment

    [root@nfs01 ~]# groupadd -g666 www
    [root@nfs01 ~]# useradd -u666 -g666 www

Install configure start nfs

[root@nfs01 ~]# yum install nfs-utils -y
    [root@nfs01 ~]# cat /etc/exports
    /data/blog 172.16.1.0/24(rw,sync,all_squash,anonuid=666,anongid=666)
    /data/zh 172.16.1.0/24(rw,sync,all_squash,anonuid=666,anongid=666)
    [root@nfs01 ~]# rm -rf /data
    [root@nfs01 ~]# mkdir /data/blog /data/zh -p

Restart nfs service

[root@nfs01 ~]# systemctl restart nfs

2. Find the location where the web stores static resources?

#Find the path of picture storage > browser > F12 > Select > select Picture
http://blog.oldxu.com/wp-content/uploads/2019/12/ks.jpeg
/code/wordpress/wp-content/uploads/2019/12/ks.jpeg

3. Copy the pictures of all nodes to the nfs storage?
On a web node with pictures

  [root@web02 ~]# scp -rp /code/wordpress/wp-content/uploads/* 172.16.1.31:/data/blog/

Go back to nfs storage and reauthorize

    [root@nfs01 ~]# chown -R www.www /data/blog/

4. Mount all web nodes

wordpress

   [root@web02 ~]# mount -t nfs 172.16.1.31:/data/blog /code/wordpress/wp-content/uploads/

After switching to web01 test, mount the test

   [root@web01 ~]# mount -t nfs 172.16.1.31:/data/blog /code/wordpress/wp-content/uploads/

wecenter

    http://zh.oldxu.com/uploads/article/20191206/72fb9f9956ef93b8a67498a95da3b3f7.jpg?7500
    /code/zh/uploads/

    [root@web02 ~]# mount -t nfs 172.16.1.31:/data/zh /code/zh/uploads/
    [root@web01 ~]# mount -t nfs 172.16.1.31:/data/zh /code/zh/uploads/

Remember to authorize

[root@nfs01 ~]# chown -R www.www /data/zh

Remember: be sure to write the attached information to the boot, or the next boot will be lost

5. Upload a new picture to verify whether it has been uploaded to the storage server
2. Configure mariadb password

[root@web01 ~]# mysqladmin password 'oldxu.com'
[root@web01 ~]# mysql -uroot -poldxu.com
        MariaDB [(none)]> quit

3. Prepare a script file for php to connect mysql

[root@web01 ~]# vim /code/tt.php
        <?php
Published 34 original articles, won praise and visited 454
Private letter follow

Topics: PHP Nginx MySQL Database