LNMP deployment and application

LNMP deployment and application

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 the dynamic resource request to fpm in PHP to process the dynamic request

——PHP main configuration file: php.ini

——Nginx allocates dynamic resources to FPM or Apache

(Two) MySQL installation and configuration

1. In order to be consistent with the Nginx and PHP environment, choose to install the MySQL component by using source code compilation.
2. The method of MySQL deployment

——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. There are two ways to configure web page dynamic separation and parsing PHP.

——FPM module using PHP

——Transfer the web request for accessing the PHP page to the Apache server for processing

2. 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

3. The single-server LNMP architecture usually uses FPM to parse PHP

4. PHP compilation and installation steps

- compile and install PHP
- Add compile option "- enable-fpm" to enable this module
- adjusted after installation, the main path is to establish the profile of the corresponding command optimization tool
- install Zend Guardloader (PHP parsing improve Efficiency), and load configuration-
CGI also has cross-platform functions

(4) Configure Nginx to support PHP environment

1. 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-fpm process

2. Configure the server {} configuration section in the Ngnx configuration file to forward the PHP web page request to the FPM module for processing

3. Configure the Server {} configuration section in the Nginx configuration file to forward 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

[[email protected] ~]#systemctl stop firewalld.service 
[[email protected] ~]#systemctl disable firewalld.service 
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[[email protected] ~]#setenforce 0

(1) Install Nginx service

1. Install dependent packages

[[email protected] ~]#yum -y install gcc gcc-c++ pcre-devel zlib-devel make

2. Create a running user

[[email protected] ~]#useradd -M -s /sbin/nologin nginx

3. Compile and install Nginx

Upload the nginx installation package to the /opt directory

[[email protected] opt]#cd /opt/
[[email protected] opt]#ls
nginx-1.12.0.tar.gz  rh

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

[[email protected] nginx-1.12.0]#make && make install

4. Optimize the path

[[email protected] 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


[[email protected] nginx-1.12.0]#chmod 754 /lib/systemd/system/nginx.service 
[[email protected] nginx-1.12.0]#systemctl start nginx.service 
[[email protected] 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

[[email protected] nginx-1.12.0]#yum -y install gcc gcc-c++ ncurses ncurses-devel bison cmake

2. Create a running user

[[email protected] 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

[[email protected] opt]#cd /opt/
[[email protected] opt]#ls
mysql-boost-5.7.20.tar.gz  nginx-1.12.0.tar.gz
nginx-1.12.0               rh
[[email protected] opt]#tar zxvf mysql-boost-5.7.20.tar.gz 
[[email protected] 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

[[email protected] 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

[[email protected] mysql-5.7.20]#chown -R mysql:mysql /usr/local/mysql/
[[email protected] mysql-5.7.20]#chown mysql:mysql /etc/my.cnf

6. Set the path environment variable

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

7. Initialize the database

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

8. Add mysql system service

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

9. Modify the mysql login password

[[email protected] bin]#mysqladmin -u root -p password "abc123"

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. The installation environment depends on the 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

2. Compile and install PHP

First upload the php-7.1.10.tar.bz2 compressed package to the /opt directory

[[email protected] opt]#cd /opt/
[[email protected] 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
[[email protected] 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

[[email protected] 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---------

[[email protected] php-7.1.10]#cp /opt/php-7.1.10/php.ini-development  /usr/local/php/lib/php.ini
[[email protected] 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 ----------

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

-------Adjust the extended configuration file------------

[[email protected] etc]#cd /usr/local/php/etc/php-fpm.d/
[r[email protected] php-fpm.d]#cp www.conf.default www.conf

5. Start php-fpm

[[email protected] php-fpm.d]#/usr/local/php/sbin/php-fpm  -c /usr/local/php/lib/php.ini 
[[email protected] 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

[[email protected] 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;
         }



[[email protected] mysql]#systemctl restart nginx.service

7. Verify the PHP test page

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

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.200.50/index.php

(4) Build Discuz Forum

[[email protected]?mysql]#cd /opt/
[[email protected]?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
[[email protected]?opt]#unzip Discuz_X3.4_SC_UTF8.zip  -d /opt/dis

[[email protected] opt]#cd /opt/dis/dir_SC_UTF8/
[[email protected] dir_SC_UTF8]#cp -r upload/ /usr/local/nginx/html/bbs
[[email protected] dir_SC_UTF8]#cd /usr/local/nginx/html/bbs/

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

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