Introduction to the LNMP architecture and software deployment

The lnmp architecture is often heard at work, so what exactly is it?
lnmp is the mainstream WEB architecture of Internet companies.
In lnmp
l is a linux operating system
Nginx is a nginx for Publishing Web sites (for static web pages, disconnected databases (html)) nginx is a high performance HTTP and reverse proxy server as well as an IMAP/POP3/SMTP proxy server.Lightweight web server software, much lighter than apache.Processing static pages is ten times more efficient than apache.
m is mysql (Mariadb), a relational database, stored in a two-dimensional table.Mainly used to store data, such as user name, bank card account, balance, salary, etc.
p is php ((published as a dynamic website, etc.) per or python), a scripting language that is executed on the server side and embedded in HTML documents, and is a mainstream interpretive language.The php dynamic web page is written based on taking yellow pictures.Dynamic Web pages do not need to be compiled, and if they are to be published or published, they need to be supported by php modules or cgi programs.Parses a php dynamic page into an HTML static page, which can be published by nginx or apache.
lnmp is a web server construction built by linux, nginx, mysql and php.These four kinds of software are free open source software, combined to become a free, efficient and scalable website service system.

Scenarios for lnmp

Why deploy the lnmp architecture, mainly for Publishing Web static + php dynamic web pages.Used for browser access to form a gorgeous Internet surfing.

1. How lnmp works



1. Users visit the nginx WEB website through the browser and initiate three handshakes through domain name resolution
2. When the request arrives at nginx and the nginx server receives the user's request, it first determines the url of the request, matches it according to the url and locatioj, and detects whether the requested web file is static or dynamic;
3. If it is static html, nginx processes and responds directly to the client;
4. If it is a dynamic php (per, python), nginx cannot process it directly at this time. nginx will send the request to the php module or cgi at the back end. The php interpreter will parse the dynamic page into a static html page, and then return it to nginx. In this case, nginx will return the resolved html static page result to the customer.
Whether the request is a static page or a dynamic page, the result the client receives is the result of a nginx response
If PHP detects that it needs to connect to the back-end database, it reads and queries the data content through the php-mysql connection-driven database at the back-end of the connection, then parses the dynamic page into a static page, and nginx returns the content of the static page to the client

Fast CGI

What is CGI?CGI is a common gateway interface, a standard for interface between external applications and Web servers. CGI is designed to guarantee the web
The data passed by the server is in a standard format, making it easy for the cgi programmer to write.
Fast-CGI is a communication interface between nginx and php. The actual process of this interface parses the PHP script by starting the php-fpm process, that is, php-fpm is equivalent to a dynamic application server, so that nginx can parse PHP dynamically.Therefore, if the nginx server needs to support PHP resolution, you need to add the configuration of PHP in nginx.conf; forward the PHP script to the IP address and port that the fast-CGI process listens on (specified in php-fpm.conf).At the same time, when installing php, you need to turn on the option to support fast-CGI, compile and install php-fpm patches/extensions, and start the php-fpm process in order to parse PHP scripts forwarded by nginx via fastCGI.

How Fast-CGI works

1. Load Fast-CGI Process Manager (IIS ISAPI or Apache Module) when Web Server starts
2. FastCGI process manager initializes itself, starts multiple CGI interpreter processes (visible multiple php-cgi) and waits for a connection from the Web Server.
3. When a client request arrives at the Web Server, the Fast-CGI process manager selects and connects to a CGI interpreter.Web server sends CGI environment variables and standard inputs to the Fast-CGI subprocess php-cgi.
4. Fast-CGI subprocesses return standard output and error information from the same connection to the Web Server after processing.When the Fast-CGI subprocess closes the connection, the request is processed.The Fast-CGI subprocess then waits and processes the next connection from the Fast-CGI Process Manager (running in Web Server).In CGI mode, php-cgi exits here.

In these cases, you can imagine how slow CGIs are generally.Each Web request PHP must resolve php.ini, reload all extensions, and reinitialize all data structures.With Fast-CGI, all this happens only once when the process starts.An added benefit is that persistent database connections work.

PHP-CGI

php-cgi is a program that interprets PHP scripts. It is only a CGI program. It can only parse requests, return results, and do not manage processes by itself.

PHP-FPM

PHP-FPM is a program that implements Fastcgi. PHP-FPM is managed by php-cgi.Officially accepted by PHP.
After the PHP kernel integrated PHP-FPM, it was much more convenient to use the compilation parameter -enalbe-fpm.

1. php-fpm is a completely independent program that does not depend on php-cgi or php.Because php-fpm is a FastCGI service with a built-in PHP interpreter, php.ini and php-fpm.conf configurations can be read on startup by themselves.
2. A master process that supports multiple pools. Each pool is monitored on a different port by the master process. There are multiple worker processes in the pool.
3. Each worker process has a built-in PHP interpreter, and processes reside in the background to support prefork dynamic growth.
4. Each worker process supports compiling scripts at runtime and caching generated opcode s in memory to improve performance.
5. Each worker process supports automatic restart after configuration responds to a specified number of requests, and the master process restarts the suspended worker process.
6. Each worker process can maintain a persistent connection to MySQL/Memcached/Redis to achieve a "connection pool", avoid duplicate connections, and be transparent to programs.
7. The master process receives and distributes requests asynchronously using the epoll model, listen s on the port, and epoll_wait waits for a connection.
8. It is then distributed to the worker process in the corresponding pool, which poll s the connection after accpet requests.
9. If the worker process is not enough, the master process will prefork more processes.
A. If prefork reaches the pm.max_children limit and the worker process is busy, the master process will suspend the request to the connection queue backlog (default is 511).

How software is deployed

Source Compilation Installation
1. Install mysql
Install required dependent software

Unzip the mysql package and enter:
precompile

cmake -DCMAKE_INSTALL_PREFIX=/usr/local/lnmp/mysql
-DMYSQL_DATADIR=/usr/local/lnmp/mysql/data -DMYSQL_UNIX_ADDR=/usr/local/lnmp/mysql/data/mysql.sock -DWITH_MYISAM_STORAGE_ENGINE=1 -DWITH_INNOBASE_STORAGE_ENGINE=1 -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DEXTRA_CHARSETS=all -DWITH_BOOST=boost/boost_1_59_0/

Install dependent packages as prompted during precompilation.

Compile Installation
make && make install


Add MySQL users and user groups

groupadd -g 27 mysql
useradd -u 27 -g 27 mysql -s /sbin/nologin


Configuring MySQL environment variables

vim ~/.bash_profile
PATH=PATH:PATH:PATH:HOME/bin
PATH=PATH:PATH:PATH:HOME/bin:/usr/local/lnmp/mysql/bin
source ~/.bash_profile

Configure MySQL profile

cd support-files/
cp my-default.cnf /etc/my.cnf
vim /etc/my.cnf

mysql security initialization

[root@server1 support-files]# mysql_secure_installation

Securing the MySQL server deployment.

Enter password for user root: #Enter the password to initialize here

The existing password for the user account root has expired. Please
set a new password.

New password:

Re-enter new password:

VALIDATE PASSWORD PLUGIN can be used to test passwords and improve
security. It checks the strength of password and allows the users to
set only those passwords which are secure enough. Would you like to
setup VALIDATE PASSWORD plugin?

Press y|Y for Yes, any other key for No: No Using existing password
for root. Change the password for root ? ((Press y|Y for Yes, any
other key for No) :

... skipping. By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have a user
account created for them. This is intended only for testing, and to
make the installation go a bit smoother. You should remove them before
moving into a production environment.

Remove anonymous users? (Press y|Y for Yes, any other key for No) :

... skipping.

Normally, root should only be allowed to connect from 'localhost'.
This ensures that someone cannot guess at the root password from the
network.

Disallow root login remotely? (Press y|Y for Yes, any other key for
No) :

... skipping. By default, MySQL comes with a database named 'test'
that anyone can access. This is also intended only for testing, and
should be removed before moving into a production environment.

Remove test database and access to it? (Press y|Y for Yes, any other
key for No) :

... skipping. Reloading the privilege tables will ensure that all
changes made so far will take effect immediately.

Reload privilege tables now? (Press y|Y for Yes, any other key for No) y Success.

All done!


test

PHP Source Installation


./configure --prefix=/usr/local/lnmp/php --with-config-file-path=/usr/local/lnmp/php/etc --with-openssl --with-snmp --with-gd --with-zlib --with-curl --with-libxml-dir --with-png-dir --with-jpeg-dir --with-freetype-dir --with-gmp --with-gettext --with-pear --enable-mysqlnd --with-mysql=mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --enable-inline-optimization --enable-soap --enable-ftp --enable-sockets --enable-mbstring --enable-fpm --with-fpm-user=nginx --with-fpm-group=nginx --with-mcrypt --with-mhash

Installation required
oniguruma-devel-5.9.5-3.el7.x86_64.rpm
oniguruma-5.9.5-3.el7.x86_64.rpm
Successful precompilation
Compile Installation
make && make install

Copy and configure php profile to add nginx user

[root@server1 php-7.4.1]# cd /usr/local/lnmp/php/etc/
[root@server1 etc]# ls
pear.conf php-fpm.conf.default php-fpm.d
[root@server1 etc]# useradd nginx
[root@server1 etc]# cp php-fpm.conf.default php-fpm.conf
[root@server1 etc]# vim php-fpm.conf
25 pid = run/php-fpm.pid


Copy files, modify time zone
[root@server1 php-7.4.1]# cp php.ini-production /usr/local/lnmp/php/etc/php.ini
[root@server1 php-7.4.1]# vim /usr/local/lnmp/php/etc/php.ini


Configure php service startup script

[root@server1 php-7.4.1]# cd sapi/
[root@server1 sapi]# ls
apache2handler cgi cli embed fpm litespeed phpdbg
[root@server1sapi]# cd fpm/
[root@server1 fpm]# ls config.m4 fpm
init.d.php-fpm.in Makefile.frag php-fpm.8 php-fpm.conf
php-fpm.service status.html tests www.conf.in CREDITS init.d.php-fpm LICENSE php-fpm php-fpm.8.in
php-fpm.conf.in php-fpm.service.in status.html.in www.conf
[root@server1 fpm]# cp init.d.php-fpm init.d.php-fpm
init.d.php-fpm.in
[root@server1 fpm]# cp init.d.php-fpm /etc/init.d/php-fpm
[root@server1 fpm]# chmod +x /etc/init.d/php-fpm


Test Start php Service

nginx source installation


Dependency Resolution
yum install pcre-devel -y

precompile

./configure --prefix=/usr/local/lnmp/nginx --with-threads
–with-file-aio --with-http_ssl_module --with-http_stub_status_module --user=nginx --group=nginx


Compile Installation

make && make intall

Modify the Nginx configuration file

vim /usr/local/lnmp/nginx/conf/nginx.conf
//Lines 65-71, uncomment #
//Combining nginx with php
2 user nginx nginx;
3 worker_processes auto;
42
43 location / {
44 root html;
45 index index.php index.html index.htm;
46 }

65 location ~ .php$ { 66 root html;
67 fastcgi_pass 127.0.0.1:9000; 68
fastcgi_index index.php; 69 #fastcgi_param
SCRIPT_FILENAME /scripts$fastcgi_script_name;
70 include fastcgi.conf;
71 }
cd sbin/
./nginx -t #View the configuration file. /nginx
-v #View Nginx version




//Make soft links and test them
ln -s /usr/local/lnmp/nginx/sbin/nginx /usr/local/sbin/
//Open the nginx service and view the ports
nginx
netstat -antlupe

Access Test
cd /usr/local/lnmp/nginx/html/
vim index.php

<?php phpinfo() ?>


Test PHP file reported 404, you can see from the error log of nginx that head cannot be resolved, this is only a problem of nginx configuration file, change php's / script to $document_root, reload nginx.conf file test succeeded





Test successful, nginx compiled successfully

46 original articles published, 0 praised, 605 visits
Private letter follow

Tags: PHP Nginx MySQL Web Server

Posted on Fri, 07 Feb 2020 00:38:27 -0500 by cleanice