Install Nginx, MariaDB, PHP (LEMP) and WordPress on CentOS 7 - NETSEC


Learning, Sharing, Creating

Cybersecurity Memo

Saturday, October 26, 2019

Install Nginx, MariaDB, PHP (LEMP) and WordPress on CentOS 7

Create CentOS 7 VM & Update

sudo -i
yum update -y
yum install epel-release yum-utils -y
yum clean install

Install Nginx

yum install nginx -y
service nginx start
*browser to VM's public IP to test 

sudo systemctl enable nginx
sudo systemctl start nginx

Install MariaDB

yum install mariadb-server -y
service mariadb start

MariaDB will ask you for the root password, however, since this is initial installation, you don’t have any, so just press enter. Next prompt will ask if you want to set a root password, enter Y and follow the instructions:
Enter current password for root (enter for none):
OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorization.

New password: password
Re-enter new password: password
Password updated successfully!
Reloading privilege tables..
 ... Success!
You can safely click ENTER key and accept default settings for all other questions. After you complete the setup, proceed further to PHP installation.

Create WordPress DB

mysql -u root -p
CREATE USER wordpressuser@localhost IDENTIFIED BY 'password1234';
GRANT ALL PRIVILEGES ON wordpress.* TO wordpressuser@localhost IDENTIFIED BY 'password1234';

Note: If Wordpress and DB are not in same machine, you will need grant user remote access privileges using following command:
GRANT ALL PRIVILEGES ON wordpress.* TO 'wordpressuser'@'%' IDENTIFIED BY 'password1234';


If you are using MySQL8, some commands changed:

mysql -u root -p
CREATE USER 'wp1'@'%' IDENTIFIED BY 'password';

or with limit to only DB wordpress
GRANT ALL PRIVILEGES ON wordpress.* TO wp1@"%";


Install PHP7.2

sudo yum install

rpm -Uvh
rpm -Uvh
rpm -Uvh
sudo yum install yum-utils -y

sudo yum-config-manager --enable remi-php72

sudo yum install php-cli php-fpm php-mysql php-json php-opcache php-mbstring php-xml php-gd php-curl

Configure PHP 7 by modify the config file of php. Make sure to uncomment & change the ;cgi.fix_pathinfo=1 to cgi.fix_pathinfo=0.
# vi /etc/php.ini
Open php-fpm configuration file and modify the lines same as below.
# vi /etc/php-fpm.d/www.conf
user = nginx
group = nginx

listen = /var/run/php-fpm/php-fpm.sock
listen.owner = nginx = nginx
service php-fpm restart

sudo systemctl enable php-fpm

sudo systemctl start php-fpm

echo "<?php phpinfo(); ?>" > phpversion.php

Note: Php 7.1 installation  - verified:

Php7.3 installation (Not Verified):

yum install php73-php-fpm php73-php-mysql


# 启动PHP-FPM 
systemctl start php73-php-fpm 
# 开机启动 
systemctl enable php73-php-fpm 
# 启动mysql 
systemctl start mysqld

# 回车输入密码即可
mysql -u root -p
# 创建名字为wordpress的数据库
create database wordpress;

# 创建数据库用户,用户名wordpressuser,密码password1234
grant all privileges on wp.* to 'wordpressuser'@'%' identified by 'password1234';

# 刷新授权
flush privileges;

# 目录为 /opt/application
tar -xvf latest-zh_CN.tar.gz

# 用模板复制修改配置文件
cp wp-config-sample.php wp-config.php
// ** MySQL 设置 - 具体信息来自您正在使用的主机 ** //
/** WordPress数据库的名称 */
define( 'DB_NAME', 'wordpress' );
/** MySQL数据库用户名 */
define( 'DB_USER', 'wordpressuser' );
/** MySQL数据库密码 */
define( 'DB_PASSWORD', 'password1234' );
/** MySQL主机 */
define( 'DB_HOST', 'localhost' );
/** 创建数据表时默认的文字编码 */
define( 'DB_CHARSET', 'utf8' );
/** 数据库整理类型。如不确定请勿更改 */
define( 'DB_COLLATE', '' );


server {
  # 监听的端口
  listen 80;
  # 域名
  index index.html index.php;
  # 访问日志目录
  access_log /var/log/nginx/blog_access.log main;
  # 网站根目录
  root /opt/application/wordpress;

  location / {
    root /data/www;

  location ~ \.php$ {
  # php程序启动的端口
    fastcgi_index  index.php;
    fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
    include        fastcgi_params;

Configure Nginx 


Create a new configuration file called wordpress under folder /etc/nginx/sites-available
sudo nano /etc/nginx/sites-available/wordpress

Then copy and paste the content below into the file and save it. Replace with your own domain name. You might also want to add your wordpress site's public ip into servername list for your testing before you switched to domain.
server {
    listen 80;
    listen [::]:80;
    root /var/www/html/wordpress;
    index  index.php index.html index.htm;

     client_max_body_size 100M;

    location / {
        try_files $uri $uri/ /index.php?$args;        

    location ~ \.php$ {
    include snippets/fastcgi-php.conf;
    fastcgi_pass             unix:/var/run/php/php7.2-fpm.sock;
    fastcgi_param   SCRIPT_FILENAME $document_root$fastcgi_script_name;


sudo vi /etc/nginx/conf.d/wordpress.conf
server {
    listen   80;

    # note that these lines are originally from the "location /" block
    root   /var/www/html/wordpress;
    index index.php index.html index.htm;

    location / {
        try_files $uri $uri/ /index.php?$args;
    error_page 404 /404.html;
    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
        root /usr/share/nginx/html;

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;

Install WordPress

yum install wget
cd /tmp && wget
tar -zxvf latest.tar.gz

sudo mv wordpress /var/www/html/wordpress

sudo chown -R nginx: /var/www/html/wordpress
sudo chmod -R 755 /var/www/html/wordpress/
sudo mv /var/www/html/wordpress/wp-config-sample.php /var/www/html/wordpress/wp-config.php

Configure WordPress

Change configuration to connect to your local new MariaDB server:
sudo nano /var/www/html/wordpress/wp-config.php

// ** MySQL settings - You can get this info from your web host ** //
/** The name of the database for WordPress */
define('DB_NAME', 'wordpress');

/** MySQL database username */
define('DB_USER', 'wordpressuser');

/** MySQL database password */
define('DB_PASSWORD', 'password1234');

/** MySQL hostname */
define('DB_HOST', 'localhost');

/** Database Charset to use in creating database tables. */
define('DB_CHARSET', 'utf8');

/** The Database Collate type. Don't change this if in doubt. */
define('DB_COLLATE', '');


No comments:

Post a Comment