Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Deploying LNMP Stack with Nginx, MySQL, and PHP on Linux

Tech 1

System Preparation

Disable Firewall and SELinux

systemctl disable --now firewalld
setenforce 0

Install Required Dependencies

yum -y install pcre-devel zlib-devel gcc gcc-c++ make

Create Dedicated User for Nginx

useradd -M -s /sbin/nologin nginx

Install Nginx from Source

Download and Extract Nginx

cd /opt
tar zxvf nginx-1.22.0.tar.gz -C /opt/

Configure and Build Nginx

cd nginx-1.22.0/
./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_stub_status_module

make && make install

Create Symbolic Link for Nginx Commands

ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/

Set Up 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

systemctl daemon-reload
chmod 777 /lib/systemd/system/nginx.service

Install MySQL from Source

Install MySQL Dependencies

yum -y install \
ncurses \
ncurses-devel \
bison \
cmake

yum -y install gcc gcc-c++ cmake bison bison-devel zlib-devel libcurl-devel libarchive-devel boost-devel ncurses-devel gnutls-devel libxml2-devel openssl-devel libevent-devel libaio-devel

Create MySQL User

useradd -M -s /sbin/nologin mysql

Compile and Install MySQL

cd /opt
tar zxvf mysql-boost-5.7.20.tar.gz

cd /opt/mysql-5.7.20/
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 && make install

Configure MySQL Settings

vim /etc/my.cnf

[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

Adjust Onwership for MySQL Directories

chown -R mysql:mysql /usr/local/mysql/
chown mysql:mysql /etc/my.cnf

Update System Path for MySQL

echo 'export PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH' >> /etc/profile
source /etc/profile

Initialize MySQL Database

cd /usr/local/mysql/bin/
./mysqld \
--initialize-insecure \
--user=mysql \
--basedir=/usr/local/mysql \
--datadir=/usr/local/mysql/data

Enable MySQL System Service

cp /usr/local/mysql/usr/lib/systemd/system/mysqld.service /usr/lib/systemd/system/
systemctl daemon-reload
systemctl start mysqld.service
systemctl enable mysqld

Set MySQL Root Password

mysqladmin -u root -p password "123456"

Installl PHP from Source

Install PHP Dependencies

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

Compile and Install PHP

cd /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

Optimize PHP Paths

ln -s /usr/local/php/bin/* /usr/local/bin/
ln -s /usr/local/php/sbin/* /usr/local/sbin/

Configure PHP Files

PHP uses three configuration files: php.ini (main), php-fpm.conf (process), and www.conf (extension).

Adjust Main Configuration

cp /opt/php-7.1.10/php.ini-development /usr/local/php/lib/php.ini
vim /usr/local/php/lib/php.ini
--1170行--modify
mysqli.default_socket = /usr/local/mysql/mysql.sock
--939行--uncomment and modify
date.timezone = Asia/Shanghai

php -m

Adjust Process Configurasion

cd /usr/local/php/etc/
cp php-fpm.conf.default php-fpm.conf
vim php-fpm.conf
--17行--remove ";" comment
pid = run/php-fpm.pid

Adjust Extension Configuration

cd /usr/local/php/etc/php-fpm.d/
cp www.conf.default www.conf

Start PHP-FPM

/usr/local/php/sbin/php-fpm -c /usr/local/php/lib/php.ini
netstat -anpt | grep 9000
cd /opt/php-7.1.10/sapi/fpm
cp php-fpm.service /usr/lib/systemd/system/php-fpm.service
systemctl restart php-fpm.service

Configure Nginx for PHP Processing

Modify Nginx Configuration

vim /usr/local/nginx/conf/nginx.conf
--65行--uncomment and modify
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;
}
 
systemctl restart nginx.service

Create PHP Test Page

vim /usr/local/nginx/html/index.php
<?php
phpinfo();
?>

Verify LNMP Setup

Check PHP Page

Access 172.168.1.11/index.php in a browser.

Test Database Connection

mysql -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;

Update Test Page for Database Verification

vim /usr/local/nginx/html/index.php
<?php
$link=mysqli_connect('172.168.1.11','bbsuser','admin123');
if($link) echo "<h1>Success!!</h1>";
else echo "Fail!!";
?>

Access 172.168.1.11/index.php in a browser.

Deploy Discuz! Forum

Install Discuz! Application

cd /opt
unzip Discuz_X3.4_SC_UTF8.zip
cd /opt/dir_SC_UTF8/
cp -r upload/ /usr/local/nginx/html/bbs/

Set Permissions for Forum Directories

cd /usr/local/nginx/html/bbs/
chown -R nginx ./config/
chown -R nginx ./data/
chown -R nginx ./uc_client/
chown -R nginx ./uc_server/
chmod -R 777 ./config/
chmod -R 777 ./data/
chmod -R 777 ./uc_client/
chmod -R 777 ./uc_server/

Access Forum Installation Page

Navigate to http://172.168.1.11/bbs/install/index.php.

Deploy WordPress Blog

Install WordPress

unzip wordpress-6.1.1-zh_CN.zip
cp -r wordpress  /usr/local/nginx/html/

chmod 777 wordpress/ -R

Set Up Database for WordPress

mysql -u root -p123456
CREATE DATABASE blog;
GRANT all ON blog.* TO 'bloguser'@'%' IDENTIFIED BY 'admin123';
GRANT all ON blog.* TO 'bloguser'@'localhost' IDENTIFIED BY 'admin123';
flush privileges;

Access WordPress Installation Page

Navigate to http://172.168.1.11/wordpress/wp-admin/install.php.

Tags: LNMP

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.