LNMP environment construction (php7.4.0)



  • preparation
    • Install WGet
    • Install net tools
    • Installing VIM
    • Configure display line number
    • Turn off the firewall
  • Install nginx
    • Installation dependency
    • Compile and install nginx
    • Configure environment variables
    • SYSTEMd management
  • Install MySQL
    • Installation dependency
    • Download boost
    • Compile and install MySQL
    • Configure environment variables
    • Modify configuration file
    • SYSTEMd management
    • Log in to MySQL
  • Install PHP
    • Installation dependency
    • Compile and install PHP
    • Configure SYSTEMd service
    • Associate nginx with PHP
  • Install redis
    • Compile and install
    • Configure environment variables
    • Configure background running
    • Configure SYSTEMd service
  • reference material

As a PHP developer, we must know how to build a PHP development environment. At present, the mainstream PHP development environment combination is lamp and LNMP. This article will introduce how to build LNMP development environment on centos7 *.

Description of each version:

CentOS7: 7.7

Nginx: 1.16.1




Install WGet

wgetIt is a free tool to automatically download files from the network. It supports downloading through the three most common TCP / IP protocols: http, HTTPS and FTP, and can use HTTP proxy.

sudo yum install wget

Install net tools

To minimize centos7 installation, you need to install if you cannot use the ifconfig commandnet-tools, if you are installing a version of centos6, you do not need to install

sudo yum -y install net-tools

Installing VIM

sudo yum -y install vim

Configure display line number

VIM ~ /. Vimrc ා edit. Vimrc configuration file
Set Nu ා exit saving after entering set nu

Turn off the firewall

systemctl stop  firewalld.service   #Shut down the firewall
systemctl disable  firewalld.service   #Turn off the firewall and start automatically
Enter IP through browser to test whether it is successful

Install nginx

Installation dependency

(1) InstallationnginxYou need to compile the source code downloaded from the official website. The compilation depends on GCC environment. If there is no GCC environment, you need to install gcc-c + +.

yum -y install gcc gcc-c++

(2) PCREIs a Perl library, Chinese “Perl compatible regular expression library.”. The purpose of installing nginx is to enable nginx to support the Rewrite Module with URI rewriting function. If the PCRE library is not installed, nginx cannot use the rewrite module function, and nginx’s rewrite module function is almost necessary for enterprise applications.

yum -y install pcre pcre-devel

(3) zlibThe library provides many ways to compress and decompress. Nginx uses zlib to gzip the contents of HTTP packages, so you need to install zlib library on CentOS.

yum -y install zlib zlib-devel

(4) OpenSSLIt is a powerful secure socket layer cipher library, including the main cryptographic algorithms, common key and certificate encapsulation management functions and SSL protocol, and provides rich application programs for testing or other purposes. Nginx not only supports HTTP protocol, but also supports HTTPS (that is to transfer HTTP over SSL Protocol), so OpenSSL library needs to be installed.

yum -y install openssl openssl-devel

Note: the PCRE version installed by Yum is relatively low, but it does not affect the use. However, it is better to manually compile and install the latest stable version of OpenSSL on the official website.

Check the underlying dependency package

After the above dependency installation is completed, you can check whether the dependency installation is successful through the following command

rpm -qa pcre pcre-devel
rpm -qa zlib zlib-devel
rpm -qa pcre pcre-devel

Compile and install nginx

#Here, we put the installation packages in the / usr / local / SRC directory for unified management
CD / usr / local / SRC ා switch to the package directory
wget  http://nginx.org/download/nginx-1.16.1.tar.gz    #Download nginx source package
Useradd nginx - S / SBIN / nologin - M ා create nginx users to manage nginx programs
tar -zxvf nginx-1.16.1. tar.gz   #Decompress nginx source code package

cd nginx-1.16.1

#Note that since the OpenSSL location has been customized, an error will be reported when making. You need to modify a conf file in the nginx source code
#/Usr / local / SRC / nginx-1.16.1/auto/lib/openssl/conf, see impression note for modification method

./configure \
--user=nginx \
--group=nginx \
--prefix=/usr/local/nginx-1.16.1 \
--with-http_v2_module \
--with-http_ssl_module \

Make & make install

cd /usr/local
Ln - s nginx-1.16.1 nginx ා create the soft link of nginx

Installation instructions

--Prefix = path ා set the installation Road force
--User = user ා process user rights
--Group = group ා process user group permissions
--with-http_v2_module  # HTTP2
--with-http_ stub_ status_ Module ා activation status information
--with-http_ ssl_ Module ා activate SSL function

Configure environment variables

vim /etc/profile
export PATH=/usr/local/nginx/sbin:$PATH
source /etc/profile

SYSTEMd management

New and edit/usr/lib/systemd/system/nginx.servicefile

vim /usr/lib/systemd/system/nginx.service

And add the following content (the configuration here is configured according to the path where nginx is installed/usr/localContents)

Description=The nginx HTTP and reverse proxy server
After=network.target remote-fs.target nss-lookup.target

ExecStartPre=/usr/local/nginx/sbin/nginx -t
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop


Nginx installed through yum, default nginx.service The configuration is as follows, which can be used as a reference

# /usr/lib/systemd/system/nginx.service
Description=The nginx HTTP and reverse proxy server
After=network.target remote-fs.target nss-lookup.target

# Nginx will fail to start if /run/nginx.pid already exists but has the wrong
# SELinux context. This might happen when running `nginx -t` from the cmdline.
# https://bugzilla.redhat.com/show_bug.cgi?id=1268621
ExecStartPre=/usr/bin/rm -f /run/nginx.pid
ExecStartPre=/usr/sbin/nginx -t
ExecReload=/bin/kill -s HUP $MAINPID


Overloaded daemon

Run the following command to reload SYSTEMd and scan for new or changed cells

systemctl daemon-reload

Set auto start

systemctl enable  nginx.service  #Set auto start
systemctl disable  nginx.service  #Cancel boot service

Common commands for nginx service management

systemctl status  nginx.service  #View nginx status
systemctl start  nginx.service  #Open nginx
systemctl stop  nginx.service  #Close nginx
systemctl reload  nginx.service  #Overload configuration
systemctl restart  nginx.service   #Restart nginx (equivalent to stop & start)

Service startup check

This command can be used to query who occupies port 80

lsof -i :80

If the command is not recognized, it needs to be installedlsof

sudo yum -y install lsof

Install MySQL

Installation dependency

(1) Cmake is a compiler tool for the new version of MySQL and must be installed

sudo yum -y install ncurses-devel perl perl-devel autoconf

Do not use the yum method to install cmake, because the default version of cmake is lower, which will lead to the problem that the PHP version is too low to install. Because cmake is required for MySQL installation, the latest stable version of cmake is directly installed here

cd /usr/local/src
yum remove cmake
wget https://github.com/Kitware/CMake/releases/download/v3.16.1/cmake-3.16.1.tar.gz
tar -zxvf cmake-3.16.1.tar.gz

cd cmake-3.16.1
./configure \
make && make install

cd /usr/local
ln -s /usr/local/cmake/bin/cmake /usr/bin/cmake
# ln -s /usr/local/cmake/share/cmake-3.16.1 /usr/share/cmake

Download boost

If you install MySQL version 5.7 or above, you need to install boost before compiling and installing, because the higher version of MySQL requires the installation of the boot library to run normally. Otherwise, it will be reportedCMake Error at cmake/boost.cmake:81error

Switch to/usr/local/srcDirectory, and then download boost from this directory
MySQL 5.7.28 requires the boost version to be 1.59, and higher versions are not applicable to MySQL 5.7.28

wget http://www.sourceforge.net/projects/boost/files/boost/1.59.0/boost_1_59_0.tar.gz

Compile and install MySQL

#Add MySQL user
useradd -s /sbin/nologin -M mysql

#Switch to the / usr / SRC directory
cd /usr/local/src

#Download MySQL
wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.28.tar.gz

#Unzip MySQL
tar -zxvf mysql-5.7.28.tar.gz

#Unzip boost and move to MySQL / boost
tar -zxvf boost_1_59_0.tar.gz
mv boost_1_59_0 mysql-5.7.28/boost

#Go to the MySQL directory
cd mysql-5.7.28

cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql-5.7.28 \
-DWITH_BOOST=boost \
-DWITH_SSL=system \
-DMYSQL_UNIX_ADDR=/var/lib/mysql/mysql.sock \
-DMYSQL_DATADIR=/var/lib/mysql/data \
-DDEFAULT_COLLATION=utf8mb4_general_ci \
-DWITH_ZLIB=bundled \

#Compile & install
make && make install

#Create soft links
cd /usr/local
ln -s mysql-5.7.28 mysql

Configure environment variables

#Add to environment variable
vim /etc/profile
export PATH=/usr/local/mysql/bin:$PATH
source /etc/profile

Modify configuration file

  1. stay/var/libCreate amysqlfolder

    mkdir -p /var/lib/{mysql,mysql/data}
    touch /var/lib/mysql/mysqld.pid
    chown mysql.mysql -R /var/lib/mysql/
  2. modify/etc/my.cnffile

    #Modify / etc/ my.cnf File, edit the configuration file as follows
  3. establishmysqld.logandmysqld.pidFile, and modify the file permissions

    #Create mysqld.log  And mysqld.pid file
    mkdir /var/log/mysql
    touch /var/log/mysql/mysqld.log
    chown mysql.mysql -R /var/log/mysql/
  4. Initialize database

    #A password of - initialize means that the password is not initialized
    mysqld --initialize-insecure --user=mysql --basedir=/usr/local/mysql --datadir=/var/lib/mysql/data

SYSTEMd management

Create a/usr/lib/systemd/system/mysqld.serviceFile and then edit as follows

vim /usr/lib/systemd/system/mysqld.service
Description=MySQL Server





# Disable service start and stop timeout logic of systemd for mysqld service.

# Execute pre and post scripts as root

# Needed to create system tables

# Start main service
ExecStart=/usr/local/mysql/bin/mysqld --daemonize --pid-file=/var/lib/mysql/mysqld.pid $MYSQLD_OPTS

# Use this to switch malloc implementation

# Sets open_files_limit
LimitNOFILE = 5000




Overloaded daemon

Run the following command to reload SYSTEMd and scan for new or changed cells

systemctl daemon-reload

Start MySQL

systemctl start  mysqld.service  #Start MySQL
systemctl stop  mysqld.service  #Close MySQL
systemctl status  mysqld.service  #View MySQL status

Start up automatically

systemctl enable  mysqld.service  #Set auto start
systemctl disable  mysqld.service  #Cancel boot

Log in to MySQL

MySQL - U root - P ා you don't need a password for the first login, just press enter
set password for  [email protected]  =Password ('root '); modify password

Install PHP

Installation dependency

sudo yum -y install gcc gcc-c++ zip unzip libxml2 libxml2-devel curl-devel autoconf libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel gd-devel bzip2 bzip2-devel libsqlite3x libsqlite3x-devel oniguruma oniguruma-devel

Libzip upgrade

yum remove libzip
cd /usr/local/src
wget https://libzip.org/download/libzip-1.5.2.tar.gz
tar -zxvf libzip-1.5.2.tar.gz
cd libzip-1.5.2
mkdir build
cmake .
make && make install

echo '/usr/local/lib64
ldconfig -v

Compile and install PHP

cd /usr/local/src
wget http://hk1.php.net/get/php-7.4.0.tar.gz/from/this/mirror -O php-7.4.0.tar.gz

tar -zxvf php-7.4.0.tar.gz
cd  php-7.4.0
./configure \
--prefix=/usr/local/php-7.4.0 \
--enable-fpm \
--with-fpm-user=nginx \
--with-fpm-group=nginx \
--with-zlib \
--enable-mysqlnd \
--enable-bcmath \
--enable-gd \
--with-pdo-mysql=mysqlnd \
--with-mysqli=mysqlnd \
--with-mysql-sock=/var/lib/mysql/mysql.sock \
--with-jpeg \
--with-freetype \
--with-iconv \
--with-openssl \
--with-curl \
--enable-mbstring \
--enable-static \
--enable-sockets \

make && make install

Detailed explanation of compilation parameters

./configure \
--Prefix = / usr / local / php-7.4.0
--Enable FPM means to activate php-fpm mode service, that is to run PHP service in factcgi mode.
--With FPM user = nginx \ ා specify the user managed by php-fpm process as WWW, which is better to be unified with nginx service user.
--With FPM group = nginx ා specify the php-fpm process management user group as WWW, which is better unified with the nginx service user group.
--With zlib \ ා turn on zlib library support for HTTP compressed transmission
--enable-mysqlnd \
--with-pdo-mysql=mysqlnd \
--with-mysqli=mysqlnd \
--with-mysql-sock=/var/lib/mysql/mysql.sock \
--With Gd support for opening GD Library
--with-png-dir \
--with-jpeg-dir \
--with-freetype-dir \
--Open OpenSSL, which is used for encrypted transmission
--With curl turn on curl browser support 
--Enable mbstring ා multibyte, string support
--Enable static \ ා generate static link library
--Enable zip \ ා turn on support for zip
--Enable sockets \ \ turn on sockets support

to configure

cd /usr/local

ln -s php-7.4.0 php
cp  /usr/local/src/php-7.4.0/php.ini-development /usr/local/php-7.4.0/lib/php.ini
 vim /usr/local/php/lib/php.ini
 date.timezone  =PRC (about 954 lines)
 expose_ PHP = off ා avoid exposing PHP information in HTTP headers (about 369 lines)
 display_ Errors = off (if the production environment is set to off, the development environment is set to on for debugging)
 Description: dispaly is set_ After errors is off, you need to- fpm.conf Open error logging path error_ log = log/php- fpm.log
cd /usr/local/php 
cp etc/php-fpm.conf.default etc/php-fpm.conf

cd /usr/local/php/etc/php-fpm.d/
cp www.conf.default www.conf
cd /usr/local/php
ps -e | grep php-fpm

If -- with MySQL = mysqlnd and -- with PDO MySQL = mysqlnd are specified when compiling PHP, socket connection problems may be encountered in production. The solution is to php.ini Add command: PDO_ mysql.default_ socket=/usr/local/mysql/tmp/ mysql.sock

It is best to specify when compiling PHP mysql.socket Location:

Manage php-fpm

vim /usr/local/php/etc/php-fpm.conf
pid = run/php-fpm.pid
error_ log = log/php- fpm.log  #Line 24 this is in php.ini Set display_ Enabled when errors = off
Restart the server after setting
Process management can be completed by sending signals to the process
Stop: Kill - Int ` cat / usr / local / PHP / var / run / PHP- fpm.pid `
Smooth stop: Kill - quit ` cat / usr / local / PHP / var / run / PHP- fpm.pid `
Restart: Kill - usr2 ` cat / usr / local / PHP / var / run / PHP- fpm.pid `
Reopen log: Kill - usr1 ` cat / usr / local / PHP / var / run / PHP- fpm.pid `

Configure environment variables

vim /etc/profile
export PATH=/usr/local/php/bin:$PATH
source /etc/profile

Configure SYSTEMd service

In fact, PHP- fpm.service The PHP file has been configured for us. We just need to copy it to the specified location and enable it.

cp /usr/local/src/php-7.4.0/sapi/fpm/php-fpm.service /usr/lib/systemd/system/

And edit the file

vim /usr/lib/systemd/system/php-fpm.service

php- fpm.service The contents of the document are as follows:

# It's not recommended to modify this file in-place, because it
# will be overwritten during upgrades.  If you want to customize,
# the best way is to use the "systemctl edit" command.

Description=The PHP FastCGI Process Manager

ExecStart=/usr/local/php/sbin/php-fpm --nodaemonize --fpm-config /usr/local/php/etc/php-fpm.conf
ExecReload=/bin/kill -USR2 $MAINPID


Overloaded daemon

Run the following command to reload SYSTEMd and scan for new or changed cells

systemctl daemon-reload

Start up automatically

systemctl enable php-fpm.service
systemctl disable php-fpm.service


systemctl start php-fpm.service

Associate nginx with PHP

nginx.conf to configure

#user  nobody;
#There is a working subprocess, which can be modified by itself, but it is too big to benefit because of the CPU contention
#General setting CPU number * core number
worker_processes  1; 

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;

events {
#It is generally used to configure the characteristics of nginx process and connection
#If several work at the same time
    multi_ Accept on; ා turn on the ability to accept multiple new network connection requests at the same time.
    Use epoll; ා use epoll event driven, because epoll's performance is much better than other event drivers
    worker_ Connections 10240; ා this refers to the maximum number of 10240 connections allowed for a child process

Http {# this is the main section for configuring the HTTP server
    include       mime.types;
    default_type  application/octet-stream;
    #Hide nginx software version number
    server_tokens off;
    #Activate TCP_ Nodelay function improves I / O performance
    tcp_nodelay on;

    #Set the timeout for reading client request header data. The value here is 15 in seconds, which is the empirical reference value
    client_header_timeout 15;

    #Set the timeout for reading the client request body
    client_body_timeout 15;

    #Specifies the timeout for the response client
    send_timeout 25;

    #Upload file size limit
    client_max_body_size 8m;
    #Compression configuration
    gzip on;
    gzip_min_length 1k;
    gzip_buffers 4 16k;
    gzip_http_version 1.0;
    gzip_comp_level 2;
    gzip_types text/css text/xml application/javascript;
    gzip_vary on;
    #include extra/gzip.config;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    include extra/*.conf;

Install redis

Compile and install

#Decompress the source code file
tar -zxf redis-5.0.6.tar.gz

#Switch to the unzip directory
cd redis-5.0.6

#Compile and install
mkdir /usr/local/redis-5.0.6
make PREFIX=/usr/local/redis-5.0.6 install

mkdir /usr/local/redis-5.0.6/etc
cp redis.conf /usr/local/redis-5.0.6/etc/

#Create soft links
cd /usr/local
ln -sf redis-5.0.6 redis

Configure environment variables

vim /etc/profile
export PATH=/usr/local/redis/bin:$PATH
Source / etc / profile ා make the changes take effect immediately

Configure background running

Give WayredisRun as background process

vim /usr/local/redis/etc/redis.conf

#Daeonize no
#Change to - >
daemonize yes

Configure SYSTEMd service

stay/etc/systemd/system/Add oneredis.serviceFile and add the following

ExecStart=/usr/local/redis/bin/redis-server /usr/local/redis/etc/redis.conf
ExecStop=/usr/local/redis/bin/redis-cli shutdown

Overloaded daemon

Run the following command to reload SYSTEMd and scan for new or changed cells

systemctl daemon-reload

Start up automatically

systemctl enable redis.service

Start redis service

systemctl start redis.service

reference material

Source code compilation and installation of mysql5.7 for centos7

Related configuration of MySQL system D in Linux 7

Managing MySQL Server with systemd

Compile and install centos7 7.3 PHP

Compile and install php7.3 under centos7

Cmake installation configuration and Getting Started Guide

Compile cmake 3.15 and GCC 5.3.0

Centos7 upgrade OpenSSL to 1.1.1