Speed up your sites with Nginx
Some of you might have experienced slow response times from your server, which cause the website to take a long time to load. In cPanel servers, Apache is the default webserver used, both for static and dynamic pages. Now let me tell you how you can speed things up with Nginx.
One way to improve performance is to setup
Nginx as a proxy. It will load static pages by default and whenever a request comes for PHP pages, those requests are passed on to Apache. But this is not what I am going to talk about today. If you want to know more about setting this up, check out this link.
To get even faster response times, even if your server is getting a lot of traffic, you can use nginx as the default webserver to load both static and dynamic pages.
In this post I will talk about setting this up on a cPanel server, on which no accounts have been created yet. Before starting the nginx installation, I recommend you to stop Apache, remove Apache from all run levels and disable Apache in the cPanel service monitoring daemon.
chkconfig --levels 12345 httpd off
In a cPanel server, by default, PHP and Apache will be installed on the server. You can remove the PHP binaries installed on the server or rename them. Then you should install PHP manually:
wget to download the source from http://php.net/downloads.php (php 5.3.x or php 5.2.x). Then untar, configure and install it using the following commands:
tar -zxf php-5.x.x.tar.gz
./configure --enable-fastcgi --enable-fpm --disable-pdo
--enable-bcmath --enable-calendar --enable-ftp --enable-gd-native-ttf
--enable-libxml enable-magic-quotes --enable-mbstring --enable-sockets
--with-curl=/opt/curlssl --with-curlwrappers --with-freetype-dir=/usr --with-gd
--with-gettext --with-imap=/opt/php_with_imap_client --with-imap-ssl=/usr
--with-jpeg-dir=/usr --with-kerberos --with-libdir=lib64
--with-mcrypt=/opt/libmcrypt/ --with-mime-magic --with-mysql=/usr
--with-openssl-dir=/usr --with-pcre-regex=/opt/pcre --with-pic
--with-png-dir=/usr --with-ttf --with-xmlrpc --with-xpm-dir=/usr --with-zlib
--with-zlib-dir=/usr --enable-sockets --enable-sysvsem --enable-sysvshm
--enable-pcntl --enable-memcache --enable-zip --with-pcre-regex
make && make install
If you get any dependency error while configuring or compiling php, you need to install the dependencies and should link it correctly while configuring PHP. Once php is installed, you can install memcache, ioncube, and other caching modules if you want.
After successfully installing php and other caching modules(if necessary), open the file “/usr/local/etc/php-fpm.conf”. Look for the lines:
Change the value to the username under which you will be running the php-fpm processes. The username should not be “root”(recommended “nobody”). After adding the username, make sure to remove the “!” marks. Otherwise, this line will be considered as a comment.
Now, download the latest stable source file for nginx from the location given below.
Then untar, configure and install it:
tar -zxf nginx-0.7.67.tar.gz
./configure --sbin-path=/usr/local/sbin --with-http_ssl_module
--without-mail_pop3_module --without-mail_imap_module --without-mail_smtp_module
make && make install
After nginx is installed, open the file “/usr/local/nginx/conf/nginx.conf” and paste the following lines in it.
Open the file “/usr/local/nginx/conf/fastcgi_params”, add the following lines in it.
Run the following commands:
mkdir mkdir /var/log/nginx/
Paste the following lines in it.
vi $(which php-fm)
Make sure that you are using the correct php-cgi path and the location of the php-fpm.conf is also correct.
Run the command pstree -aupl and check whether both services are running on the server and responding to requests.
You can add the above commands in rc.local file so that they will be started whenever the server is rebooted. Make sure to disable apache, easyapache script and apache init script.
You can add an account from cpanel. By, default cPanel will configure the account in httpd.conf file. Use the below script to create the website in nginx.
chmod 755 /usr/bin/nginx-siteconf
run the following command to configure the websites in shell.
Write a php info page inside your domain and check whether the nginx is loading php pages without any problem.
Now, you can see a consistent performance even if there is a large amount of traffic to your server.
One problem with running Nginx as the default webserver is that it does not follow the syntax of the rewrite rules of mod_rewrite(used by Apache). It has its own rewrite rule syntax. You can find most of them from this URL.
About the Author:
Padmakumar has been with Bobcares for just over 3 years now. In that time he’s become an expert in managing cPanel servers and is always on the lookout for new ways to get the best out of it. His love for cPanel is second only to Linux, which he’s been working with before joining Bobcares. When not diving into technical issues, he enjoys chatting with his friends, listening to music and playing cricket.