LNMP architecture and deployment

1. The deployment of LNMP architecture

1. Overview of LNMP architecture

(1) The LNMP platform is a combined architecture of Linux, Ngnix, MySQL, and PHP. It requires a Linux server, MySQL database, and a PHP parsing environment.
(2) Features

Ngnix specialty: high concurrency, low resources, very strong in handling static network access requests

Apache: Both static processing and dynamic processing can be done, more suitable for dynamic processing

Nginx: hand over 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

2. MySQL installation and configuration

(1) In order to be consistent with the Nginx and PHP environment, here choose to use source code compilation to install the MySQL component
(2) MySQL deployment method:
compile and install MySQL
optimize and adjust the initialization database
Start the mysq service and set the password of the root database account

3. Installation of PHP parsing environment

(1) Configure web page dynamic and static separation and parse PHP. There are two ways to choose.
Use PHP's FPM module to
forward the web request for accessing PHP pages to the Apache server for processing.
(2) The newer version of PHP has its own FPM module. To manage PHP parsing instances and optimize parsing efficiency
FastCG separates Http Server and dynamic scripting language.
Nginx specializes in processing static requests and forwards dynamic requests.
PHP FPM specializes in parsing PHP dynamic requests.
(3) Single-server LNMP architecture usually uses FPM. To parse PHP
(4) PHP compilation and installation steps Add "-enable-fpm" when
compiling and installing PHP
compilation options to enable this module
adjustments after installation, mainly the establishment of configuration files and the path optimization of the corresponding command tools to
install Zend Guardloader (improve PHP parsing efficiency), and load configuration
CGI and cross-platform functions

4. Configure Nginx to support PHP environment

(1) Invoke the configuration method of the php-fpm process of this machine.
Establish the FPM configuration file php-fpm.conf, modify the configuration options, such as: PD file running user, number of service processes, etc.
Start the php-fpm process
(2) Configuration in Ngnx The server {} configuration section configuration in the file transfers the PHP web page request to the FPM module for processing
(3) The Server {} configuration section configuration in the Nginx configuration file transfers the PHP web page request to the FPM module for processing

Two, deploy LNMP steps and related commands

First, you need to turn off the firewall

[root@localhost ~]#systemctl stop firewalld.service 
[root@localhost ~]#systemctl disable firewalld.service 
[root@localhost ~]#setenforce 0

1. Install Nginx service

(1) Install dependent packages

[root@localhost ~]#yum -y install gcc gcc-c++ pcre-devel zlib-devel make

(2) Create a running user

​[root@localhost ~]#useradd -M -s /sbin/nologin nginx

(3) Compile and install Nginx and
upload the nginx installation package to the /opt directory

[root@localhost opt]#cd /opt/
[root@localhost opt]#ls
nginx-1.12.0.tar.gz  rh

[root@localhost opt]#tar zxvf nginx-1.12.0.tar.gz -C /opt/
[root@localhost opt]#cd nginx-1.12.0/
[root@localhost nginx-1.12.0]#./configure \
> --prefix=/usr/local/nginx \
> --user=nginx \
> --group=nginx \
> --with-http_stub_status_module

[root@localhost nginx-1.12.0]#make && make install

(4) Optimize the path

[root@localhost nginx-1.12.0]#ln -s /usr/local/nginx/sbin/* /usr/local/sbin/ 

(5) Add 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
ExecReload=/bin/kill -1 $MAINPID
ExecStop=/bin/kill -3 $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target


[root@localhost nginx-1.12.0]#chmod 754 /lib/systemd/system/nginx.service 
[root@localhost nginx-1.12.0]#systemctl start nginx.service 
[root@localhost nginx-1.12.0]#systemctl enable nginx.service 
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.

2. Install MySQL service

(1) Install the MySQL environment dependency package

[root@localhost nginx-1.12.0]#yum -y install gcc gcc-c++ ncurses ncurses-devel bison cmake

(2) Create a running user

[root@localhost nginx-1.12.0]#useradd -M -s /sbin/nologin mysql

(3) Compile and install
First upload the mysql-boost-5.7.20.tar.gz package to the /opt directory

[root@localhost opt]#cd /opt/
[root@localhost opt]#ls
mysql-boost-5.7.20.tar.gz  nginx-1.12.0.tar.gz
nginx-1.12.0               rh
[root@localhost opt]#tar zxvf mysql-boost-5.7.20.tar.gz 
[root@localhost opt]#cd /opt/mysql-5.7.20/
cmake \
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock \	'sock;通讯文件,连接数据库,通讯协议的载体'
-DSYSCONFDIR=/etc \	'配置目录指向etc'
-DSYSTEMD_PID_DIR=/usr/local/mysql \	'pid文件位置'
-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 \	'指定boost位置'
-DWITH_SYSTEMD=1	'守护进程'
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 -j 2 && make install

(4) Modify the mysql configuration file

[root@localhost mysql-5.7.20]#vim /etc/my.cnf
#删除原有内容,添加以下内容
[client]					#client 用户程序
port = 3306				
socket =/usr/local/mysql/mysql.sock

[mysqld]					#以后对msql配置时 更多的会针对于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	#PID文件目录
socket = /usr/local/mysql/mysql.sock	#通讯文件
bind-address = 0.0.0.0
skip-name-resolve
max_connnection=2048
default-storage-engine=INNODB
max_allowed_packet=16M
server-id = 1				#服务id,在之后的mysql集群中用于标识mysql服务器

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
[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

(5) Change the owner group of the mysql installation directory and configuration file

[root@localhost mysql-5.7.20]#chown -R mysql:mysql /usr/local/mysql/
[root@localhost mysql-5.7.20]#chown mysql:mysql /etc/my.cnf

(6) Set the path environment variable

[root@localhost mysql-5.7.20]#echo 'export PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH' >> /etc/profile
[root@localhost mysql-5.7.20]#source /etc/profile

(7) Initialize the database

[root@localhost mysql-5.7.20]#cd /usr/local/mysql/bin/
[root@localhost bin]#
./mysqld \
--initialize-insecure \
--user=mysql \
--basedir=/usr/local/mysql \
--datadir=/usr/local/mysql/data

(8) Add mysql system service

[root@localhost bin]#cp /usr/local/mysql/usr/lib/systemd/system/mysqld.service /usr/lib/systemd/system/
[root@localhost bin]#systemctl daemon-reload 
[root@localhost bin]#systemctl start mysqld.service
[root@localhost bin]#systemctl enable mysqld 
Created symlink from /etc/systemd/system/multi-user.target.wants/mysqld.service to /usr/lib/systemd/system/mysqld.service.
[root@localhost bin]#netstat -anpt | grep 3306
tcp        0      0 0.0.0.0:3306            0.0.0.0:*               LISTEN      29473/mysqld   

(9) Modify the login password of mysql

[root@localhost bin]#mysqladmin -u root -p password "123456"

(10) Authorize remote login

mysql -u root -p
grant all privileges on *.* to 'root'@'%' identified by 'abc123';
#授予root用户可以在所有终端远程登录,使用的密码是abc123,并对所有数据库和所有表有操作权限

show databases;			#查看当前已有的数据库

3. Install and configure PHP parsing environment

(1) Installation environment dependent packages

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

(2) Compile and install PHP,
first upload the php-7.1.10.tar.bz2 compressed package to the /opt directory

[root@localhost opt]#cd /opt/
[root@localhost opt]#ls
mysql-5.7.20               nginx-1.12.0.tar.gz
mysql-boost-5.7.20.tar.gz  php-7.1.10.tar.bz2
nginx-1.12.0               rh
[root@localhost opt]#tar jxvf php-7.1.10.tar.bz2
cd  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

(3) Path optimization

[root@localhost php-7.1.10]#ln -s /usr/local/php/bin/* /usr/local/bin/

(4) Modify the PHP configuration file
PHP has three configuration files

 php.ini        主配置文件 
 php-fpm.conf   进程服务配置文件
 www.conf       扩展配置文件

Modify the main configuration file

[root@localhost php-7.1.10]#cp /opt/php-7.1.10/php.ini-development  /usr/local/php/lib/php.ini
[root@localhost php-7.1.10]#vim /usr/local/php/lib/php.ini 
------修改1170行------
mysqli.default_socket = /usr/local/mysql/mysql.sock
------取消注释并修改939行------
date.timezone = Asia/Shanghai


php -m   #验证安装的模块

Modify the process service configuration file

[root@localhost etc]#cd /usr/local/php/etc
[root@localhost etc]#cp php-fpm.conf.default php-fpm.conf
[root@localhost etc]#vim php-fpm.conf
-----17行去掉注释-----
pid = run/php-fpm.pid

Modify the extended configuration file

[root@localhost etc]#cd /usr/local/php/etc/php-fpm.d/
[root@localhost php-fpm.d]#cp www.conf.default www.conf

(5) Start php-fpm

[root@localhost php-fpm.d]#/usr/local/php/sbin/php-fpm  -c /usr/local/php/lib/php.ini 
[root@localhost php-fpm.d]#netstat -natp | grep 9000
tcp        0      0 127.0.0.1:9000          0.0.0.0:*               LISTEN      35323/php-fpm: mast 

#PHP-FPM(FasttCGI Process Manager: FastCGI 进程管理器)是一个 PHPFastCGI 管理器,由于Nginx服务器不能处理动态页面,需要由nginx 把动态请求交给 php-fpm 进程进行解析。

(6) Configure Nginx to support php parsing

[root@localhost php-fpm.d]#vim /usr/local/nginx/conf/nginx.conf
------65行取消注释并修改------
  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;
         }



[root@localhost mysql]#systemctl restart nginx.service

(7) Verify the PHP test page

[root@localhost mysql]#vim /usr/local/nginx/html/index.php
<?php
phpinfo();
?>

Visit in the virtual machine browser: http://192.168.177.18/index.php
Insert picture description here
(8) Verify that the database is working properly

mysq1 -u root -p 

CREATE DATABASE bbs;
GRANT all ON bbs.* To 'bbsuser'@'%' IDENTIFIED BY 'admin123';
GRANT all ON bbs.* To 'bbsuser'@'localhost' IDENTIFIED BY 'admin123';
flush privileges;

vim /usr/local/nginx/html/index.php  ###原来的测试页内容更改如下
<?php
$link=mysqli_connect('192.168.200.50','bbsuser','admin123');
if($link) echo"<h1>Success!!</h1>";
else echo "Fail!!";
?>




systemctl restart nginx

在网页测试:http://192.168.177.18/index.php

Insert picture description here

4. Build Discuz Forum

[root@localhost?mysql]#cd /opt/
[root@localhost?opt]#ls
Discuz_X3.4_SC_UTF8.zip    nginx-1.12.0.tar.gz
mysql-5.7.20               php-7.1.10
mysql-boost-5.7.20.tar.gz  php-7.1.10.tar.bz2
nginx-1.12.0               rh
[root@localhost?opt]#unzip Discuz_X3.4_SC_UTF8.zip  -d /opt/dis

[root@localhost opt]#cd /opt/dis/dir_SC_UTF8/
[root@localhost dir_SC_UTF8]#cp -r upload/ /usr/local/nginx/html/bbs
[root@localhost dir_SC_UTF8]#cd /usr/local/nginx/html/bbs/

[root@localhost bbs]#chown -R root:nginx ./config/
[root@localhost bbs]#chown -R root:nginx ./data/
[root@localhost bbs]#chown -R root:nginx ./uc_client/
[root@localhost bbs]#chown -R root:nginx ./uc_server/
[root@localhost bbs]#chmod -R 777 ./config/
[root@localhost bbs]#chmod -R 777 ./data/
[root@localhost bbs]#chmod -R 777 ./uc_client/
[root@localhost bbs]#chmod -R 777 ./uc_server/

论坛页面访问:
http://192.168.177.18/bbs/install/index.php

Insert picture description here
Database server: localhost
database name: bbs
database user name: bbsuser
database password: admin123
administrator account: admin
administrator password: admin123

Guess you like

Origin blog.csdn.net/tefuiryy/article/details/113134531