LNMP deployment and application (Linux+Nginx+MySQL+PHP)------Deployment process (super detailed!! Quick look!!)


■LNMP refers to a set of acronyms for free software commonly used together to run a dynamic website or server. L refers to Linux, N refers to Nginx, M generally refers to MySQL, or MariaDB, and P generally refers to PHP, but also Perl or Python.

■LNMP stands for: Nginx+MySQL+PHP web server architecture under Linux system

■Linux is a general term for a class of Unix computer operating systems, and is currently the most popular free operating system. Representative versions are: debian, centos, ubuntu, fedora, gentoo, etc.

■Nginx is a high-performance HTTP and reverse proxy server, as well as an IMAP/POP3/SMTP proxy server

■Mysql is a small relational database management system

PHP is a scripting language that is executed on the server side to embed HTML documents

■These four kinds of software are all free and open source software, which can be combined together to become a free, efficient and highly scalable website service system

One: Deployment of LNMP Architecture (Theory)

1.1: Overview of LNMP Architecture

The LNMP platform is a combined architecture of Linux, Ngnix, My SQL, and PHP, and requires a Linux server, MysαL database, and a PHP parsing environment
■Ngnix features: high concurrency, low resources, very strong in handling static network access requests
Apache: Both static processing and dynamic processing can be done, and it is more suitable for dynamic processing
■Nginx passes dynamic resource requests to fpm in PHP to process dynamic requests
■PHP main configuration file: php.ini
■ Nginx allocates dynamic resources to FPM or Apache

1.2: MySQL installation configuration

■In order to be consistent with the Nginx and PHP environments, choose to install the MySQL component by compiling the source code here
■How to deploy MySQL
■Compile and install MySQL
■Optimize and adjust the initialization database
■Start the mysq service and set the password of the root database account

1.3: Installation of PHP parsing environment

■Configure web page dynamic and static separation, parse PHP, there are two ways to choose
● FPM module using PHP
●Transfer the Web request for accessing the PHP page to the Apache server for processing

■The newer version of PHP has its own FPM module, which is used to manage PHP parsing instances and optimize parsing efficiency
●FastCG separates Http Server and dynamic scripting language
●Nginx specializes in processing static requests and forwarding dynamic requests
●PHP FPM specializes in parsing PHP dynamic requests
●Single-server LNMP architecture usually uses FPM to parse PHP

■PHP compilation and installation steps

●Compile and install PHP
●Add "--enable-fpm" when compiling options to enable this module
●Adjustment after installation, mainly the establishment of configuration files and path optimization of corresponding command tools
● Install Zend Guardloader (to improve PHP parsing efficiency), and load configuration

1.4: Configure Ngnx to support PHP environment

■Call the local php-fpm process configuration method
●Create FPM configuration file php-fpm.conf, modify configuration options, such as: PD file running user, number of service processes, etc.
● start the php-pm process
●The serve} configuration section in the Ngnx configuration file configures the PHP web page request to be forwarded to the FPM module for processing
●The Server{} configuration section in the Ngnx configuration file configures the PHP web page request to be forwarded to the FPM module for processing

2. Nginx service construction

Note: The detailed process has been described in detail in the previous article. The detailed process will not be described here. Please refer to the previous blog post, which also includes Nginx's access status statistics deployment process and Nginx's access control (authorization-based access control, client-based access control, domain-based virtual web hosting, port-based virtual web hosting, IP-based virtual web hosting)

Three, build mysql database

3.1 Install mysql environment dependencies

[root@localhost ~]# yum -y install \
> ncurses \      ##A package of character terminals for easy terminal operation##
> ncurses-devel \
> bison \                      ##function library##
> cmake                  ##No configure,use cmake##
[root@localhost ~]# useradd -s /sbin/nologin mysql   ##create user##

3.2 cmake configuration and compilation installation

##Pass in the desired compressed package/opt Under contents##
[root@localhost ~]# cd /opt
[root@localhost opt]# tar xf mysql-boost-5.7.20.tar.gz
[root@localhost opt]# cd mysql-5.7.20/
[root@localhost mysql-5.7.20]# cmake \
> -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \    ##pid file location##
> -DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock \  
    ##sock;Communication file, connection database, carrier of communication protocol##
> -DSYSCONFDIR=/etc \     ##The configuration directory points to etc##
> -DSYSTEMD_PID_DIR=/usr/local/mysql \
> -DDEFAULT_CHARSET=utf8  \    ##This line and the next line are related to the character set##
> -DDEFAULT_COLLATION=utf8_general_ci \
> -DWITH_INNOBASE_STORAGE_ENGINE=1 \  ##This row and the next three rows are storage engine##
> -DWITH_ARCHIVE_STORAGE_ENGINE=1 \
> -DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
> -DWITH_PERFSCHEMA_STORAGE_ENGINE=1 \
> -DMYSQL_DATADIR=/usr/local/mysql/data \   ##designated storage location##
> -DWITH_BOOST=boost \       ##specify boost Location##
> -DWITH_SYSTEMD=1       ##daemon##
[root@localhost mysql-5.7.20]# make -j6  ##-j6 Specifies the number of cores to be processed, the maximum cannot exceed the number of cores##
[root@localhost mysql-5.7.20]# make install

3.3 Set the permissions of the /usr/local/mysql directory

[root@localhost mysql-5.7.20]# chown -R mysql.mysql /usr/local/mysql

3.4 Edit configuration file

##set up/usr/local/mysql directory permissions##
[root@localhost mysql-5.7.20]# cd /etc
[root@localhost etc]# vim my.cnf
##Delete the original text and add the following##
[client]
port = 3306
default-character-set=utf8
socket = /usr/local/mysql/mysql.sock

[mysql]
port = 3306
default-character-set=utf8
socket = /usr/local/mysql/mysql.sock

[mysqld]
user = mysql
basedir = /usr/local/mysql
datadir = /usr/local/mysql/data
port = 3306
character_set_server=utf8
pid-file = /usr/local/mysql/mysqld.pid
socket = /usr/local/mysql/mysql.sock
server-id = 1

sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_AUTO_VALUE_ON_ZERO,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,PIPES_AS_CONCAT,ANSI_QUOTES

3.5 Setting environment variables

[root@localhost etc]# chown mysql:mysql /etc/my.cnf
[root@localhost ~]# echo 'PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH' >> /etc/profile
[root@localhost ~]# echo 'export PATH' >> /etc/profile
[root@localhost ~]# source /etc/profile

3.6 Initialize the database

[root@localhost ~]# cd /usr/local/mysql/
bin/mysqld \
--initialize-insecure \
--user=mysql \
--basedir=/usr/local/mysql \
--datadir=/usr/local/mysql/data
[root@localhost mysql]# cp usr/lib/systemd/system/mysqld.service /usr/lib/systemd/system/

3.7 Open the database and check the status

[root@localhost mysql]# systemctl enable mysqld
Created symlink from /etc/systemd/system/multi-user.target.wants/mysqld.service to /usr/lib/systemd/system/mysqld.service.
[root@localhost mysql]# systemctl start mysqld
[root@localhost mysql]# systemctl status mysqld
[root@localhost mysql]# netstat -anpt | grep 3306
tcp6       0      0 :::3306                 :::*                    LISTEN      92257/mysqld        

3.8 Set the mysql password and log in to the database

[root@localhost mysql]# mysqladmin -u root -p password  ##At first, no password is empty, just press Enter, and then enter the password Abc123,Confirm password again##
Enter password: 
New password: 
Confirm new password: 
Warning: Since password will be sent to server in plain text, use ssl connection to ensure password safety.   ##Password set successfully##
[root@localhost mysql]# mysql -u root -p   ##Enter the password to log in to the database##
. . . . . . . . . . . . . . 
mysql> 

Fourth, PHP compilation and installation

4.1 Install the PHP environment package

[root@localhost ~]# yum -y install \
libjpeg \
libjpeg-devel \
libpng libpng-devel \
freetype freetype-devel \
libxml2 \
libxml2-devel \
zlib zlib-devel \
curl curl-devel \
openssl openssl-devel

4.2 Compile and install

##Pass in the installation package/opt Under contents##
[root@localhost opt]# cd /opt
[root@localhost opt]# tar xf php-7.1.10.tar.bz2
[root@localhost opt]# cd php-7.1.10/
[root@localhost php-7.1.10/]# ./configure \
--prefix=/usr/local/php \
--with-mysql-sock=/usr/local/mysql/mysql.sock \
--with-mysqli \
--with-zlib \
--with-curl \
--with-gd \
--with-jpeg-dir \
--with-png-dir \
--with-freetype-dir \
--with-openssl \
--enable-fpm \
--enable-mbstring \
--enable-xml \
--enable-session \
--enable-ftp \
--enable-pdo \
--enable-tokenizer \
--enable-zip
[root@localhost php-7.1.10/]# make -j6
[root@localhost php-7.1.10/]# make install

4.3 Three configuration files for configuring PHP

##php There are three configuration files  php.ini core configuration file php-fpm.conf Process service configuration file www.conf Extended configuration file##
[root@localhost php-7.1.10/]# cp php.ini-development /usr/local/php/lib/php.ini
[root@localhost php-7.1.10/]# vi /usr/local/php/lib/php.ini
##Find the location of the following two lines and modify these two lines##
mysqli.default_socket = /usr/local/mysql/mysql.sock
date.timezone = Asia/Shanghai	##After finding it, the front;Comment to remove##
[root@localhost php-7.1.10]# /usr/local/php/bin/php -m
##Verify installed modules##

4.4 Configure and optimize the FPM module

[root@localhost php-7.1.10]# cd /usr/local/php/etc/
[root@localhost etc]# cp php-fpm.conf.default php-fpm.conf
[root@localhost etc]# cd /usr/local/php/etc/php-fpm.d/
[root@localhost php-fpm.d]# cp www.conf.default www.conf
[root@localhost php-fpm.d]# cd /usr/local/php/etc/
[root@localhost etc]# vi php-fpm.conf
pid = run/php-fpm.pid   ##remove; remove##
;user = nginx
;group = nginx

4.5 Create soft links

[root@localhost etc]# /usr/local/php/sbin/php-fpm -c /usr/local/php/lib/php.ini
[root@localhost etc]# netstat -natp |grep 9000
tcp        0      0 127.0.0.1:9000          0.0.0.0:*               LISTEN      99975/php-fpm: mast 
[root@localhost etc]# ln -s /usr/local/php/bin/* /usr/local/bin/
[root@localhost etc]# ps aux | grep -c "php-fpm"

4.6 Edit, let nginx support PHP function

[root@localhost etc]# vi /usr/local/nginx/conf/nginx.conf
location ~ \.php$ {
            root           html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /usr/local/nginx/html$fastcgi_script_name;
            include        fastcgi_params;
        }   ##to remove; to remove/usr/local/nginx/html replace the original path##
[root@localhost etc]# vi /usr/local/nginx/html/index.php
 <?php
phpinfo();
?>  
[root@localhost etc]# killall -s HUP nginx   ##reboot nginx##
##exist windows type in the browser http://20.0.0.11/index.php, you can also enter http://www.51xit.top/index.php test##

4.7 Test whether the database works normally

[root@localhost ~]# mysql -u root -p  ##enter password Abc123##
CREATE DATABASE bbs;
GRANT all ON bbs.* TO 'bbsadm'@'%' IDENTIFIED BY 'admin123';
GRANT all ON bbs.* TO 'bbsadm'@'localhost' IDENTIFIED BY 'admin123';
flush privileges;
##At last exit quit##
[root@localhost ~]# vi /usr/local/nginx/html/index.php  
##delete previous content##
<?php
$link=mysqli_connect('20.0.0.11','bbsadm','admin123');
if($link) echo "<h1>Success!!</h1>";
else echo "Fail!!";
?>
[root@localhost ~]# killall -s HUP nginx   ##reboot##

##If the input is the following, the test result on the web page is as follows##
[root@localhost ~]# vi /usr/local/nginx/html/index.php
<?php
$link=mysqli_connect('20.0.0.21','bbsadm','admin123');    ##URL is wrong##
if($link) echo "<h1>Success!!</h1>";
else echo "Fail!!";
?>
[root@localhost ~]# killall -s HUP nginx   ##reboot##

Tags: Nginx lnmp

Posted by aksival on Wed, 18 May 2022 17:52:22 +0300