当前位置:网站首页>Overview of LNMP architecture and construction of related services

Overview of LNMP architecture and construction of related services

2022-07-06 14:19:00 Impute

One 、LNMP Relevant concepts

LNMP Namely :Linux Under the system Nginx+MySQL+PHP This web server architecture .
contrast LAMP, The difference is the website server program ------Nginx(LNMP) and Apache(LAMP)
Nginx Is a high-performance HTTP And reverse proxy , Also a IMAP/POP3/SMTP proxy server .

Contrast item NginxApache
The configuration file concise complex
Use of resources Nginx contrast Apache Save more resources It takes up a lot of resources
Suitable for the status of the website static state Dynamic interaction

Generally speaking , Need performance web service , use nginx
If you don't need performance, just stability , More consideration apache

Two 、 install Nginx service

Premise

# Turn off the firewall 
systemctl stop firewalld
systemctl disable firewalld
setenforce 0

     
  • 1.
  • 2.
  • 3.
  • 4.

1、 Install dependency packages

#nginx The configuration and operation of pcre、zlib And so on , So you need to install these installed development packages , In order to provide the corresponding library and header file .
yum -y install pcre-devel zlib-devel gcc gcc-c++ make

     
  • 1.
  • 2.

2、 Create a running user

Nginx  The service program defaults to  nobody  Identity running , It is recommended to create a special user account for it , In order to more accurately control its access rights 
useradd -M -s /sbin/nologin nginx

     
  • 1.
  • 2.

3、 Compilation and installation

cd /opt/
tar zxvf nginx-1.12.0.tar.gz

cd nginx-1.12.0/
./configure \
 --prefix=/usr/local/nginx \
 --user=nginx \
 --group=nginx \
 --with-http_stub_status_module

#----- Configuration command interpretation 
--prefix=/usr/local/nginx      # Appoint nginx Installation path for 
--user=nginx                   # Specify user name 
--group=nginx                  # Specify the group name 
--with-http_stub_status_module # Enable  http_stub_status_module  Module to support State Statistics 
#-----

make && make install

     
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.

4、 Path optimization

# Put the application nginx Make a soft link to the environment path variable for the executable commands in , Let the system recognize nginx Operation command of

ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/

     
  • 1.

5、 add to Nginx system service

vim /lib/systemd/system/nginx.service
[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStart=/usr/local/nginx/sbin/nginx
ExecrReload=/bin/kill -s HUP $MAINPID
ExecrStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target

chmod 754 /lib/systemd//system/nginx.service
systemctl start nginx.service 
systemctl enable nginx.service

     
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.

3、 ... and 、 install MySQL service

1、 install Mysql Environment dependent packages

yum -y install  \
ncurses \
ncurses-devel \
bison \
cmake
#-------- Environmental package interpretation ------------
ncurses				# Dynamic library of graphic interaction function under character terminal 
ncurses-devel 		#ncurses Development kit 
bison 				# parsers 
cmake				#mysql Need to use cmake Compilation and installation 

     
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.

2、 Create a running user

useradd -M -s /sbin/nologin  mysql

     
  • 1.

3、 Compilation and installation

cd /opt
tar zxvf mysql-boost-5.7.20.tar.gz

cd /opt/mysql-5.7.20/
cmake \
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock \
-DSYSCONFDIR=/etc \
-DSYSTEMD_PID_DIR=/usr/local/mysql \
-DDEFAULT_CHARSET=utf8  \
-DDEFAULT_COLLATION=utf8_general_ci \
-DWITH_EXTRA_CHARSETS=all \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_ARCHIVE_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITH_PERFSCHEMA_STORAGE_ENGINE=1 \
-DMYSQL_DATADIR=/usr/local/mysql/data \
-DWITH_BOOST=boost \
-DWITH_SYSTEMD=1

make && make install

 Be careful : If in CMAKE There are errors in the process of , When the error is resolved , Need to put the source directory of CMakeCache.txt File deletion , And then... Again CMAKE, Otherwise, the mistake remains 

-DCMAKE_INSTALL_PREFIX=/usr/local/mysql     	# Appoint mysql Installation path for 
-DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock   # Appoint mysql The process listens to socket files ( Database connection file ) Storage path for 
-DSYSCONFDIR=/etc                               # Specify the storage path of the configuration file 
-DSYSTEMD_PID_DIR=/usr/local/mysql              # Specifies the storage path of the process file 
-DDEFAULT_CHARSET=utf8                          # Specifies the default character set encoding , Such as  utf8
-DDEFAULT_COLLATION=utf8_general_ci  			# Specifies the character set collation rules to be used by default 
-DWITH_EXTRA_CHARSETS=all 						# Specify support for other character set encoding 
-DWITH_INNOBASE_STORAGE_ENGINE=1               # install INNOBASE Storage engine 
-DWITH_ARCHIVE_STORAGE_ENGINE=1                # install ARCHIVE Storage engine  
-DWITH_BLACKHOLE_STORAGE_ENGINE=1              # install BLACKHOLE Storage engine  
-DWITH_PERFSCHEMA_STORAGE_ENGINE=1            # install FEDERATED Storage engine  
-DMYSQL_DATADIR=/usr/local/mysql/data         # Specify the storage path of the database file 
-DWITH_BOOST=boost           # Appoint boost The path of , If you use mysql and boost Integration package installation is -DWITH_BOOST=/usr/local/boost
-DWITH_SYSTEMD=1								# Generation facilitates systemctl Administrative document 

 Storage engine options :
#MYISAM,MERGE,MEMORY, and CSV The engine is compiled to the server by default , There is no need to explicitly install .
# Statically compile a storage engine to the server , Use -DWITH_engine_STORAGE_ENGINE= 1
# The available storage engine values are :ARCHIVE, BLACKHOLE, EXAMPLE, FEDERATED, INNOBASE (InnoDB), PARTITION (partitioning support),  and PERFSCHEMA (Performance Schema)
# Two common storage engines are :MYISAM and INNOBASE(InnoDB).

     
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.

4、 modify mysql The configuration file

# Delete the original configuration item , Add the following again 
vim /etc/my.cnf
[client]
port = 3306
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
bind-address = 0.0.0.0
skip-name-resolve
max_connections=2048
default-storage-engine=INNODB
max_allowed_packet=16M
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

-------------------------------------------- interpretative statement ------------------------------------------------------
# Client side Settings 
[client]
port = 3306
socket=/usr/local/mysql/mysql.sock			

# Service global settings 
[mysqld]
# Set up administrative users 
user = mysql
# Specify the installation directory of the database 
basedir=/usr/local/mysql
# Specify the storage path of the database file 
datadir=/usr/local/mysql/data
# Designated port 
port = 3306
# Set the server character set encoding format to utf8
character-set-server=utf8
# Appoint pid  Process file path 
pid-file = /usr/local/mysql/mysqld.pid
# Specify the database connection file 
socket=/usr/local/mysql/mysql.sock
# Set listening address ,0.0.0.0 The representative allows all , If multiple IP It needs to be separated by spaces 
bind-address = 0.0.0.0
# Ban DNS analysis 
skip-name-resolve
# Set up mysql Is the maximum number of connections 
max_connections=2048
# Specify the default storage engine 
default-storage-engine=INNODB
# Set the maximum packet size received by the database 
max_allowed_packet=16M
# Designated Services ID Number 
server-id = 1


sql_mode Common values are as follows :
NO_ENGINE_SUBSTITUTION
 If the required storage engine is disabled or not compiled , Then throw an error . When this value is not set , Replace... With the default storage engine , And throw an exception 

STRICT_TRANS_TABLES
 In this mode , If a value cannot be inserted into a transaction table , Then interrupt the current operation , No restrictions on non transaction tables 

NO_AUTO_CREATE_USER
 prohibit GRANT establish mima Empty users 

NO_AUTO_VALUE_ON_ZERO
mysql The self growing columns in can be derived from 0 Start . By default, self growing columns are generated from 1 At the beginning , If you insert a value of 0 The data will report an error 

NO_ZERO_IN_DATE
 Zero days and months are not allowed 

NO_ZERO_DATE
mysql Database is not allowed to insert zero date , Inserting a zero date throws an error instead of a warning 

ERROR_FOR_DIVISION_BY_ZERO
 stay INSERT or UPDATE In the process , If the data is divided by zero , It's a mistake, not a warning . By default, when data is divided by zero MySQL return NULL

PIPES_AS_CONCAT
 take "||" Treat as the concatenation operator of a string, not as the or operator , This sum Oracle The database is the same , And string concatenation function Concat Similar 

ANSI_QUOTES
 Enable ANSI_QUOTES after , You can't use double quotes to refer to strings , Because it's interpreted as an identifier 

     
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.

5、 change mysql The primary group of the installation directory and configuration files

chown -R mysql:mysql /usr/local/mysql/
chown mysql:mysql /etc/my.cnf

     
  • 1.
  • 2.

6、 Set the path environment variable

echo "export PATH=$PATH:/usr/local/mysql/bin:/usr/local/mysql/lib">> /etc/profile
source /etc/profile

     
  • 1.
  • 2.

7、 Initialize database

cd /usr/local/mysql/bin/
./mysqld \
--initialize-insecure \
--user=mysql \
--basedir=/usr/local/mysql \
--datadir=/usr/local/mysql/data

     
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.

8、 add to mysqld system service

cp /usr/local/mysql/usr/lib/systemd/system/mysqld.service /usr/lib/systemd/system/
systemctl daemon-reload
systemctl start mysqld.service
systemctl enable mysqld
netstat -anpt | grep 3306

     
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.

9、 modify mysql The login mima

mysqladmin -u root -p password "123456"

     
  • 1.

Four 、 Installation configuration PHP Analyze the environment

1、 Install environment dependency package

yum -y install \
gd \
libjpeg libjpeg-devel \
libpng libpng-devel \
freetype freetype-devel \
libxml2 libxml2-devel \
zlib zlib-devel \
curl curl-devel \
openssl openssl-devel

     
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.

2、 Compilation and installation

cd /opt
tar jxvf php-7.1.10.tar.bz2
cd /opt/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

make && make install

 Related explanations :
--prefix=/usr/local/php 				# Specify the  PHP  The installation path of the program 
--with-mysql-sock=/usr/local/mysql/mysql.sock  # Appoint mysql  The storage path of the database connection file 
--with-mysqli   # add to  MySQL  Extended support  #mysqli Extension technology can not only call MySQL Stored procedure 、 Handle MySQL Business , It can also make the work of accessing database more stable 
--with-zlib     # Support zlib function , Provides data compression 
--with-curl     # Turn on curl Extend the functionality , Realization HTTP Of Get To download and Post Requested method 
--with-gd       # Activate gd  Library support 
--with-jpeg-dir # Activate jpeg  Support for 
--with-png-dir  # Activate png  Support for 
--with-freetype-dir
--with-openssl
--enable-fpm
--enable-mbstring  # Enable multi byte string function , In order to support Chinese and other codes 
--enable-xml       # Open the extensible markup language module 
--enable-session   # conversation 
--enable-ftp       # Text transfer protocol 
--enable-pdo       # function library 
--enable-tokenizer # Token interpreter 
--enable-zip       #ZIP Compressed format 

     
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.

3、 Path optimization

ln -s /usr/local/php/bin/* /usr/local/bin/

     
  • 1.

4、 adjustment PHP The configuration file

php There are three profiles
Master profile ------php.ini
Process service profile ------php-fpm.conf
Extended profile ------www.conf

(1) Adjust the main profile

cp /opt/php-7.1.10/php.ini-development /usr/local/php/lib/php.ini
vim /usr/local/php/lib/php.ini
#939 That's ok ; uncomment , modify 
date.timezone = Asia/Shanghai
#1170 That's ok ; modify 
mysqli.default_socket = /usr/local/mysql/mysql.sock

php -m

     
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.

(2) Adjust the process service profile

cd /usr/local/php/etc/
cp php-fpm.conf.default php-fpm.conf
vim php-fpm.conf
#17 That's ok ; Remove the annotator “;”
pid = run/php-fpm.pid

     
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.

(3) Adjust the extended profile

cd /usr/local/php/etc/php-fpm.d/
cp www.conf.default www.conf

     
  • 1.
  • 2.

5、 start-up php-fpm

#PHP-FPM(FastCGI Process Manager:FastCGI Process Manager ), It's a PHPFastCGI Manager , because Nginx The server can't handle dynamic pages , Need to put Nginx Give the dynamic request to php-fpm The process continues to parse .
/usr/local/php/sbin/php-fpm  -c /usr/local/php/lib/php.ini
netstat -anpt | grep 9000

     
  • 1.
  • 2.
  • 3.

6、 To configure Nginx Support PHP analysis

vim /usr/local/nginx/conf/nginx.conf
        #65 That's ok ; Uncomment and modify 
        location ~ \.php$ {
            root           html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            # take /scripts Modified into nginx Working directory of 
            fastcgi_param  SCRIPT_FILENAME  /usr/local/nginx/html$fastcgi_script_name;
            include        fastcgi_params;
        }
        
systemctl restart nginx.service

     
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.

7、 Test access page

vim /usr/local/nginx/html/index.php
<?php phpinfo(); ?>

# Use browser access 
http://192.168.163.10/index.php

     
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.

8、 Verify database

mysql -u root -p
# Create a database 
CREATE DATABASE bbs;
# hold bbs The permissions of all tables in the database are granted to bbsuser, And set up mima123456
GRANT all ON bbs.* TO 'bbsuser'@'%' IDENTIFIED BY '123456';
# Refresh database 
flush privileges;
# See what databases are 
show databases;

vim /usr/local/nginx/html/index.php
<?php $link=mysqli_connect('192.168.163.10','bbsuser','123456'); if($link) echo "Success!!"; else echo "Fail!"; ?>

     
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
原网站

版权声明
本文为[Impute]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202131406288284.html