How to supercharge wordpress using nginx and varnish

This is how I set up a wordpress hosting for my clients. And believe me, I have got a page load time of less than one second, and when set up in a $10 digital ocean server, it could handle ~200 concurrent users.

Update server and disable SElinux 

yum clean all
yum -y update
yum -y install wget lsof mailx nano rsync perl
sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
echo 0 > /selinux/enforce

Install IUS repository 

yum -y install gcc
yum -y install epel-release
For Cent OS-6 
rpm -Uvh
For CentOS-7
rpm -Uvh
sed -i '/dev\/shm/ s/defaults/defaults,nosuid,noexec/' /etc/fstab

Install Nginx

mkdir /root/configs 
cd /root/configs 
mv nginx.repo /etc/yum.repos.d/
yum -y install nginx
chkconfig nginx on
rm -f /etc/nginx/conf.d/default.conf 
touch /etc/nginx/conf.d/default.conf; 
chattr +i /etc/nginx/conf.d/default.conf

Install PHP-FPM 

PHP 5.6

yum -y install php56u-bcmath php56u-cli php56u-common php56u-fpm php56u-gd php56u-imap php56u-intl php56u-mbstring php56u-mcrypt php56u-mysqlnd php56u-pdo php56u-pear php56u-pecl-memcache php56u-soap php56u-xml php56u-xmlrpc php56u-devel php56u-opcache

PHP 7 

yum install php70u-opcache php70u-mbstring php70u-xmlrpc php70u-pear php70u-pecl php70u-gd php70u-soap php70u-fpm php70u-pecl php70u-cli php70u-xml php70u-common php70u-devel php70u-mcrypt php70u-intl php70u-pdo php70u-mysqlnd php70u-imap php70u-bcmath php70u-process php70u-json

PHP 7.2

yum install php72u-opcache php72u-mbstring php72u-xmlrpc php72u-pear php72u-pecl php72u-gd php72u-soap php72u-fpm php72u-pecl php72u-cli php72u-xml php72u-common php72u-devel php72u-mcrypt php72u-intl php72u-pdo php72u-mysqlnd php72u-imap php72u-bcmath php72u-process php72u-json

Install Varnish

While I suggest to install varnish to speed up the WP , if you are doing a development activity on wordpress, i would suggest only to install this cache just before you go live.

cd /root/configs
mv varnish.repo /etc/yum.repos.d/ 
yum install varnish

Install MariaDB 

cd /root/configs
mv MariaDB.repo /etc/yum.repos.d/
yum install MariaDB-server MariaDB-client

Enable Varnish, Nginx, MariaDB and php-fpm at boot.

for i in varnish php-fpm nginx mysql ; do chkconfig $i on ; done



Now that we have installed most of the necessary Applications, we now need to proceed to configuring them.  Replace WEBSITE_NAME below with the website name that you’re going to create.

mkdir -p /home/websites/nginx
mkdir -p /home/websites/content/WEBSITE_NAME/public_html
adduser websites
chsh -s /sbin/nologin websites

[the_ad id=”11935″]
Download configuration files from repository

cd /root/configs
mv /etc/nginx/nginx.conf /etc/nginx/nginx.conf.old
mv nginx.conf /etc/nginx/nginx.conf 

Next 4 steps are not needed when you are not installing varnish 

mv /etc/varnish/default.vcl /etc/varnish/default.vcl.old 
mv default.vcl /etc/varnish/
mv /etc/sysconfig/varnish /etc/sysconfig/varnish.old
mv varnish /etc/sysconfig/varnish

mv /etc/php-fpm.d/www.conf /etc/php-fpm.d/www.conf.old
mv www.conf /etc/php-fpm.d/www.conf
mv website.tld.conf /home/websites/nginx/WEBSITE_NAME.conf
chown -R websites:websites /home/websites/content

At this point, you will need to edit the file /home/websites/nginx/WEBSITE_NAME.conf and replace the occurrences of website.tld to your website URL.  ( in lines 5 , 14 )


If you do not have the varnish installed, you will need to open the nginx config file, and enable port 80 instead of port 8080.

Start Services

service nginx start
service php-fpm start
service mysql start 
/usr/bin/mysqladmin -u root password 'some_strong_pass'
service varnish start
cat >> /root/.my.cnf << END 

That should be it and you have now set up a server to host your wordpress site.

Download and install wordpess.

cd /home/websites/content/WEBSITE_NAME/
rsync -av wordpress/ public_html/
rm -rf latest.tar.gz wordpress

Create MySQL Database

mysqladmin create mysite_db

Login to mysql console and run this.

GRANT ALL ON mysite_db.* to 'mysite_user'@'localhost' identified by 'strongpass';
flush privileges;

With this, you should be able to load the site if the DNS resolution is complete and then install and configure wordpress.

Leave a Reply

Close Menu