Article Directory
Preface
■ LNMP refers to a group of acronyms for free software that are usually used together to run dynamic websites or servers. L refers to Linux, N refers to Nginx, M generally refers to MySQL, or MariaDB, and P generally refers to PHP, or Perl or Python
■LNMP stands for: Nginx+MySQL+PHP this kind of website 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 embedded in HTML documents executed on the server side
■These four kinds of software are all free and open source software, combined together to become a free, efficient and extensible website service system
1. Deployment of LNMP architecture
1.1 Overview of LNMP architecture
■The LNMP platform is a combined architecture of Linux, Ngnix, MySQL, and PHP, and requires a Linux server, MySQL database, and PHP analysis environment
■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 sends 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 and configuration
■In order to be consistent with the Nginx and PHP environment, here choose to use source code compilation to install My SQL components
■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 webpage dynamic separation, parsing PHP, there are two ways to choose
- FPM module using PHP
- Forward the web request to access 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 specifically parses 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
- Adjustments after installation are mainly the establishment of configuration files and path optimization of corresponding command tools
1.4, configure Ngnx to support PHP environment
■Call the local php-fpm process configuration method
- Create 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-pm process
- The configuration section of serve} in the Ngnx configuration file is configured to forward the PHP web page request to the FPM module for processing
- Configure the Server{} configuration section in the Ngnx configuration file to forward the PHP web page request to the FPM module for processing
2. Nginx service construction
Note: The detailed process can refer to the previous blog
Three, build mysql database
(1) Install the mysql environment dependency package
[root@localhost ~]# yum -y install \
> ncurses \ ##字符终端的包,方便终端操作##
> ncurses-devel \
> bison \ ##函数库##
> cmake ##没有configure,使用cmake##
[root@localhost ~]# useradd -s /sbin/nologin mysql ##创建用户##
(2) cmake configuration, compilation and installation
##将所需压缩包传入/opt目录下##
[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文件位置##
> -DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock \
##sock;通讯文件,连接数据库,通讯协议的载体##
> -DSYSCONFDIR=/etc \ ##配置目录指向etc##
> -DSYSTEMD_PID_DIR=/usr/local/mysql \
> -DDEFAULT_CHARSET=utf8 \ ##此行和下一行为字符集相关##
> -DDEFAULT_COLLATION=utf8_general_ci \
> -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 ##守护进程##
[root@localhost mysql-5.7.20]# make -j6 ##-j6指定处理的核心数,最大不能超过本身核心数##
[root@localhost mysql-5.7.20]# make install
(3) Set the permissions of the /usr/local/mysql directory
[root@localhost mysql-5.7.20]# chown -R mysql.mysql /usr/local/mysql
(4) Edit the configuration file
##设置/usr/local/mysql目录的权限##
[root@localhost mysql-5.7.20]# cd /etc
[root@localhost etc]# vim my.cnf
##将原文删除,添加以下内容##
[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
(5) Set 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
(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/
(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
(8) Set the mysql password and log in to the database
[root@localhost mysql]# mysqladmin -u root -p password ##刚开始没密码是空的直接回车,然后输入密码Abc123,再次确认密码##
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. ##密码设置成功##
[root@localhost mysql]# mysql -u root -p ##输入密码,即可登录到数据库##
. . . . . . . . . . . . . .
mysql>
Four, PHP compilation and installation
(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
(2) Compile and install
##将安装包传入/opt目录下##
[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
(3) Configure the three configuration files of PHP
##php有三个配置文件 php.ini核心配置文件 php-fpm.conf进程服务配置文件www.conf 扩展配置文件##
[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
##找到以下两行的位置,修改这两行##
mysqli.default_socket = /usr/local/mysql/mysql.sock
date.timezone = Asia/Shanghai ##找到后,前方的;注释要去掉##
[root@localhost php-7.1.10]# /usr/local/php/bin/php -m
##验证安装的模块##
(4) Configure and optimize 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 ##将;去掉##
;user = nginx
;group = nginx
(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”
(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;
} ##将;去掉,用/usr/local/nginx/html替换原来的路径##
[root@localhost etc]# vi /usr/local/nginx/html/index.php
<?php
phpinfo();
?>
[root@localhost etc]# killall -s HUP nginx ##重启nginx##
##在windows的浏览器中输入http://20.0.0.11/index.php,也可以输入http://www.51xit.top/index.php测试##
Test:
Enter in the browser: http://20.0.0.25/index.php
(7) Test whether the database is working properly
root@localhost ~]# mysql -u root -p ##输入密码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;
##最后exit退出##
[root@localhost ~]# vi /usr/local/nginx/html/index.php
##将之前的内容删除##
<?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 ##重启##
Entering the above network card again will display the following page:
If the IP is different, an error will be reported:
##如果输入的是以下内容,在网页测试结果如下图##
[root@localhost ~]# vi /usr/local/nginx/html/index.php
<?php
$link=mysqli_connect('20.0.0.2','bbsadm','admin123'); ##网址不对##
if($link) echo "<h1>Success!!</h1>";
else echo "Fail!!";
?>
[root@localhost ~]# killall -s HUP nginx ##重启##