Deploying LAMP with Nginx Proxy, Discuz, WordPress, and phpMyAdmin

Overview

This guide outlines a practical setup for deploying a multi-server LAMP environment with Nginx acting as a reverse proxy. It includes integration of Discuz, WordPress, and phpMyAdmin under separate domains, with enhenced Nginx configurations for caching, access control, logging, and security.

Infrastructure

  • Two CentOS 6 servers:
    • MySQL Server – IP: 192.168.137.102, Hostname: mysql
    • Web Server – IP: 192.168.137.107, Hostname: lamp
  • Services:
    • Nginx on port 80 (static content)
    • Apache on port 88 (PHP dynamic content)
    • MySQL for backend data
  • Domains:
    • bbs.abc.com – Discuz
    • blog.abc.com – WordPress
    • pma.abc.com – phpMyAdmin

MySQL Server Setup

Install MySQL from binary:

cd /usr/local/src/
tar zxvf mysql-5.5.42-linux2.6-x86_64.tar.gz
mv mysql-5.5.42-linux2.6-x86_64 /usr/local/mysql
mkdir -p /data/mysql
useradd -s /sbin/nologin -M mysql
chown -R mysql:mysql /data/mysql
cp support-files/my-large.cnf /etc/my.cnf
cp support-files/mysql.server /etc/init.d/mysqld
chmod 755 /etc/init.d/mysqld
vi /etc/init.d/mysqld  # update basedir and datadir
./scripts/mysql_install_db --user=mysql --datadir=/data/mysql
echo 'export PATH=$PATH:/usr/local/mysql/bin' > /etc/profile.d/mysql.sh
chkconfig --add mysqld
chkconfig mysqld on
service mysqld start

Create a remote access user for the web server:

mysql -e "grant all on *.* to 'webuser'@'192.168.137.107' identified by 'securepassword'; flush privileges;"

LAMP Stack on Web Server

Apache Installation

cd /usr/local/src/
tar zvxf httpd-2.2.16.tar.gz
cd httpd-2.2.16
./configure --prefix=/usr/local/apache2 --enable-mods-shared=most --enable-so
make && make install
cp /usr/local/apache2/bin/apachectl /etc/init.d/httpd
vi /etc/init.d/httpd  # add chkconfig lines
chkconfig --level 35 httpd on

PHP Installation

cd /usr/local/src/
tar zxvf php-5.3.28.tar.gz
cd php-5.3.28
./configure --prefix=/usr/local/php \
--with-apxs2=/usr/local/apache2/bin/apxs \
--with-config-file-path=/usr/local/php/etc \
--with-libxml-dir --with-gd --with-jpeg-dir --with-png-dir \
--with-freetype-dir --with-iconv-dir --with-zlib-dir --with-bz2 \
--with-openssl --with-mcrypt --enable-soap --enable-gd-native-ttf \
--enable-mbstring --enable-sockets --enable-exif --disable-ipv6 \
--with-mysql=mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd
make && make install
cp php.ini-production /usr/local/php/etc/php.ini

Configuer Apache for PHP

vi /usr/local/apache2/conf/httpd.conf

Add:

AddType application/x-httpd-php .php

Set:

DirectoryIndex index.html index.htm index.php
ServerName localhost:88

Update directory access:

<Directory "/usr/local/apache2/htdocs">
Options FollowSymLinks
AllowOverride None
Order deny,allow
Allow from all
</Directory>

Deploy Applications

mkdir -p /data/{bbs,blog,pma}
unzip Discuz_X3.2_SC_UTF8.zip -d /data/bbs
mv /data/bbs/upload/* /data/bbs/
tar zxvf wordpress-4.2.2-zh_CN.tar.gz -C /data/blog
unzip phpMyAdmin-4.0.8-all-languages.zip -d /data/pma

Apache Virtual Hosts

vi /usr/local/apache2/conf/extra/httpd-vhosts.conf

Define virtual hosts on port 88:

<VirtualHost *:88>
ServerName bbs.abc.com
DocumentRoot "/data/bbs"
ErrorLog "logs/bbs.abc.com-error_log"
CustomLog "logs/bbs.abc.com-access_log" common
</VirtualHost>

<VirtualHost *:88>
ServerName blog.abc.com
DocumentRoot "/data/blog"
ErrorLog "logs/blog.abc.com-error_log"
CustomLog "logs/blog.abc.com-access_log" common
</VirtualHost>

<VirtualHost *:88>
ServerName pma.abc.com
DocumentRoot "/data/pma"
ErrorLog "logs/pma.abc.com-error_log"
CustomLog "logs/pma.abc.com-access_log" common
</VirtualHost>

Nginx as Reverse Proxy

Install Nginx

cd /usr/local/src/
wget http://nginx.org/download/nginx-1.6.2.tar.gz
tar zxvf nginx-1.6.2.tar.gz
cd nginx-1.6.2
./configure --prefix=/usr/local/nginx --with-pcre
make && make install

Init Script for Nginx

vi /etc/init.d/nginx
chmod 755 /etc/init.d/nginx
chkconfig --add nginx
chkconfig nginx on

Nginx Configuration

vi /usr/local/nginx/conf/nginx.conf

Include:

include vhosts/*.conf;

Create virtual host files in /usr/local/nginx/conf/vhosts/

Discuz Configuration

server {
listen 80;
server_name bbs.abc.com;
root /data/bbs;
index index.html index.htm index.php;

if ($http_user_agent ~* (bingbot|Sogou|360Spider|YisouSpider|YandexBot)) {
return 403;
}

location ~ admin.php {
allow 192.168.1.100;
deny all;
proxy_pass http://127.0.0.1:88;
proxy_set_header Host $host;
}

location ~ \.php$ {
proxy_pass http://127.0.0.1:88;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}

location ~ \.(js|css)$ {
expires 1d;
access_log off;
}

location ~ \.(gif|jpg|png|swf|flv|rar|zip|doc|pdf|gz|bz2|jpeg|bmp|xls)$ {
expires 7d;
valid_referers none blocked server_names *.abc.com *.baidu.com *.google.com;
if ($invalid_referer) {
return 403;
}
access_log off;
}

rewrite ^/topic-(.+)\.html$ /portal.php?mod=topic&topic=$1 last;
rewrite ^/forum-(\w+)-([0-9]+)\.html$ /forum.php?mod=forumdisplay&fid=$1&page=$2 last;
rewrite ^/thread-([0-9]+)-([0-9]+)-([0-9]+)\.html$ /forum.php?mod=viewthread&tid=$1&extra=page%3D$3&page=$2 last;

access_log /var/log/nginx/discuz.log combined;
}

WordPress Configuration

server {
listen 80;
server_name blog.abc.com;
root /data/blog;
index index.html index.htm index.php;

location /wp-admin/ {
allow 192.168.1.100;
deny all;
location ~ \.php$ {
proxy_pass http://127.0.0.1:88;
proxy_set_header Host $host;
}
}

location / {
proxy_pass http://127.0.0.1:88/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}

phpMyAdmin Configuration

server {
listen 80;
server_name pma.abc.com;
root /data/pma;
index index.html index.htm index.php;

location / {
auth_basic "Restricted Access";
auth_basic_user_file /usr/local/nginx/conf/htpasswd;
}

location ~ \.php$ {
proxy_pass http://127.0.0.1:88;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}

Log Management

Log rotation script:

vi /usr/local/sbin/logrotate.sh

#!/bin/bash
d=$(date -d "-1 day" +%Y%m%d)
mv /var/log/nginx/discuz.log /var/log/nginx/discuz_$d.log
/usr/local/nginx/sbin/nginx -s reload
cd /var/log/nginx
gzip discuz_$d.log

Schedule with cron:

chmod +x /usr/local/sbin/logrotate.sh
crontab -e
# Add:
0 0 * * * /usr/local/sbin/logrotate.sh

MySQL Backup Script

vi /usr/local/sbin/mysql_backup.sh

#!/bin/bash
backup_dir="/data/mysql_backup"
date=$(date +%F)
mkdir -p $backup_dir

databases=("wordpress" "discuz" "phpmyadmin")
for db in "${databases[@]}"; do
/usr/local/mysql/bin/mysqldump -u root -p'your_password' $db > $backup_dir/$db-$date.sql
done

rsync -avz -e ssh $backup_dir user@192.168.137.107:/remote/backup/path

Schedule:

crontab -e
# Run daily at 5 AM
0 5 * * * /usr/local/sbin/mysql_backup.sh

Tags: nginx apache PHP MySQL discuz

Posted on Wed, 13 May 2026 18:20:30 +0000 by tarlejh