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
reboot

Install IUS repository 

yum -y install gcc
yum -y install epel-release
For Cent OS-6 
rpm -Uvh http://dl.iuscommunity.org/pub/ius/stable/Redhat/6/x86_64/ius-release-1.0-11.ius.el6.noarch.rpm
For CentOS-7
rpm -Uvh http://dl.iuscommunity.org/pub/ius/stable/Redhat/7/x86_64/ius-release-1.0-11.ius.el6.noarch.rpm
sed -i '/dev\/shm/ s/defaults/defaults,nosuid,noexec/' /etc/fstab

Install Nginx

mkdir /root/configs 
cd /root/configs 
wget files.acpaneltech.com/nginx.repo 
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

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
wget files.acpaneltech.com/varnish.repo 
mv varnish.repo /etc/yum.repos.d/ 
yum install varnish

Install MariaDB 

cd /root/configs
wget files.acpaneltech.com/MariaDB.repo
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


Download configuration files from repository

cd /root/configs
wget http://files.acpaneltech.com/nginx.conf
wget http://files.acpaneltech.com/varnish
wget http://files.acpaneltech.com/default.vcl
wget http://files.acpaneltech.com/www.conf
wget http://files.acpaneltech.com/website.tld.conf
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 
[client]
user=root
pass='same_stong_pass'
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/
wget wordpress.org/latest.zip
unzip latest.zip
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

Your email address will not be published. Required fields are marked *