Blog / Linux/ LNMP 2.0 Installation and WordPress Configuration Guide for CentOS 7.6

LNMP 2.0 Installation and WordPress Configuration Guide for CentOS 7.6

wordpress运行环境LNMP安装教程(.2.6更新为lnmp2.0正式版)

This tutorial explains how to install the LNMP 2.0 stack on CentOS 7.6 and configure the necessary components and optimizations for WordPress.

Basic Server Configuration

After logging in via SSH, perform basic security hardening: change the SSH port, disable root password login, and restrict access to key-based authentication. Refer to security guides for details.

System Tools and Development Packages

Install essential tools and development packages:

yum install wget screen -y && screen -S lnmp
sudo yum check-update || sudo yum update -y
yum groupinstall -y 'Development Tools'
yum install -y epel-release
yum install -y perl perl-devel perl-ExtUtils-Embed libxslt libxslt-devel libxml2 libxml2-devel gd gd-devel GeoIP GeoIP-devel

Download LNMP Installation Package

Create a directory and download the LNMP 2.0 package:

cd / && mkdir codefiles && cd codefiles
wget http://soft.vpser.net/lnmp/lnmp2.0.tar.gz -cO lnmp2.0.tar.gz && tar zxf lnmp2.0.tar.gz && cd lnmp2.0

Download and extract the ngx_cache_purge module for Nginx cache management:

cd src && wget https://publicfilesforkk-1251162478.cos.ap-chongqing.myqcloud.com/ngx_cache_purge-2.3.tar.gz && tar zxvf ngx_cache_purge-2.3.tar.gz && rm -rf ngx_cache_purge-2.3.tar.gz && cd ..

Configure Installation Settings

Edit the lnmp.conf file to set installation parameters. Key configuration example:

Download_Mirror='https://soft.lnmp.com'

Nginx_Modules_Options='--add-module=/codefiles/lnmp2.0/src/ngx_cache_purge-2.3'
PHP_Modules_Options=''

##MySQL/MariaDB database directory##
MySQL_Data_Dir='/usr/local/mysql/var'
MariaDB_Data_Dir='/usr/local/mariadb/var'
##Default website home directory##
Default_Website_Dir='/home/wwwroot/default'

Enable_Nginx_Openssl='y'
Enable_Nginx_Lua='y'
Enable_Ngx_FancyIndex='n'
Enable_Swap='y'
Enable_PHP_Exif='n'
Enable_PHP_Fileinfo='y'
Enable_PHP_Ldap='n'
Enable_PHP_Bz2='y'
Enable_PHP_Sodium='y'
Enable_PHP_Imap='n'

Install LNMP Stack

Run the installation script:

./install.sh lnmp

Follow prompts to select database type (e.g., MariaDB or MySQL), set the root password, and choose a PHP version. The installation may take some time.

Install Additional Software

Clean Up Installation Files

After installation, remove downloaded archives to free up space:

rm -rf /codefiles/lnmp2.0.tar.gz && rm -rf /codefiles/lnmp2.0/src/ngx_cache_purge-2.3.tar.gz

Install Memcached

Use the LNMP addon script to install Memcached:

cd /codefiles/lnmp2.0/ && ./addons.sh install memcached

To enable Memcached object caching for WordPress, download object-cache.php to the wp-content directory. Add server configuration to wp-config.php:

global $memcached_servers;
$memcached_servers = array(
    array(
        '127.0.0.1', // Memcached server IP
        11211        // Memcached port
    )
);

For a remote Memcached server, adjust the IP and port, and ensure firewall rules allow access.

Enable OPcache

OPcache is included but disabled by default. Enable it with:

./addons.sh install opcache

Configure Wildcard SSL Certificate

To request a Let's Encrypt wildcard certificate, use DNS API validation. Example for HE.net:

export HE_Username="your_he_username"
export HE_Password="your_he_password"
lnmp dnsssl he

Follow prompts to enter the root domain (e.g., example.com) and wildcard domain (e.g., *.example.com).

Backup Key Configuration Files

Back up these critical files for migration or recovery:

  • Nginx main config: /usr/local/nginx/conf/nginx.conf
  • Virtual host config: /usr/local/nginx/conf/vhost/domain.conf
  • MySQL/MariaDB config: /etc/my.cnf
  • PHP config: /usr/local/php/etc/php.ini
  • PHP-FPM config: /usr/local/php/etc/php-fpm.conf

Configure Scheduled Tasks (Cron)

Set up regular tasks via crontab, such as monitoring MySQL, log rotation, and backups:

*/5 * * * * /bin/bash /codefiles/lnmp1.4/tools/checkmysql.sh
*/5 * * * * /bin/bash /codefiles/lnmp1.4/tools/check502.sh
00 03 * * * /bin/bash /codefiles/lnmp1.4/tools/cut_nginx_logs.sh
00 23 * * 3 /bin/bash /codefiles/lnmp1.4/backup.sh

Note: Adjust script paths according to your actual installation directory.

Firewall and Email Configuration

Configure iptables or cloud security groups to open necessary ports (e.g., 80, 443, 22). To enable server email sending (e.g., for alerts), configure /etc/mail.rc:

set [email protected]
set smtp=smtp.example.com
set smtp-auth-user=username
set smtp-auth-password=authorization_code
set smtp-auth=login

smtp-auth-password is the SMTP authorization code, not the login password.

SNMP Monitoring Setup

Install SNMP service for third-party monitoring platforms:

yum install net-snmp net-snmp-devel net-snmp-utils -y

Configure SNMP v2c or v3 authentication and set firewall rules to allow the monitoring server IP to access UDP port 161.

Server Backup Strategy

Implement a multi-layer backup strategy:

  1. Daily incremental database backups to cloud storage.
  2. Use cloud provider snapshot features to regularly create snapshots of system and data disks.
  3. Monthly backup of website files and databases to remote FTP or object storage, with a retention policy (e.g., 30 days).

Website Optimization Tips

  • Static-Dynamic Separation: Serve static assets (images, CSS, JS) directly via Nginx to reduce PHP load.
  • Full HTTPS: Enable SSL for all sites to improve security and SEO.
  • PHP-FPM Process Pool Isolation: Configure separate PHP-FPM pools for I/O-intensive operations (e.g., remote requests) to avoid blocking main processes.

Advantages of Nginx + PHP-FPM Architecture

Compared to Apache + mod_php, the Nginx and PHP-FPM combination is better for static-dynamic separation, load balancing, and failover. PHP-FPM supports multiple process pools, each with independent settings and even different PHP versions, offering greater flexibility and isolation.

Using fastcgi_next_upstream in Nginx allows requests to be forwarded to another available node if a backend PHP-FPM process fails, improving availability.