The LEMP stands for Linux, Nginx, MySQL and PHP. Apart from LAMP Nginx server with PHP-FPM pagespeed module and memcached is a highly efficient and fast webserver setup, with this setup your website load time will be pretty fast. This setup reduces the server response time to a greater extent and is apt for faster websites made of CMSs like WordPress, Joomla, Magento etc. You can definitely reduce server cost with this setup because you can host your website on a small cloud VPS instead of a big dedicated server without losing any performance or website load time

For this setup, we need to disable SELinux (Security Enhanced Linux) first to avoid bottlenecks. For doing so, just edit the SELinux config file (/etc/selinux/config) as given below.

echo "SELINUX=disabled" > /etc/selinux/config
echo "SELINUXTYPE=targeted" >> /etc/selinux/config

Now we have to modify the SELinux mode with,

setenforce 0

To get or activate the current mode,

getenforce

Now before begining the install, let’s just clear the yum repo cache with the command:

yum clean all

Now we can begin the installation. First we need to setup the ‘epel’, ‘remi’ and ‘nginx’ repos to get the packages. Follow the steps given below for the purpose.

To seup the epel repo use ;

rpm -Uhv http://dl.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarch.rpm

To setup remi repo use;

rpm -Uhv http://rpms.famillecollet.com/enterprise/6/remi/x86_64/remi-release-6.5-1.el6.remi.noarch.rpm

We’ll have to setup nginx repo manually. For this we need to create a ‘nginx.repo’ file in the yum repo location ‘/etc/yum.repos.d’ and add the directives ‘name’, ‘baseurl’ etc. Use the commands below.

 echo "[nginx]" > /etc/yum.repos.d/nginx.repo
 echo "name=nginx repo" >> /etc/yum.repos.d/nginx.repo
 echo "baseurl=http://nginx.org/packages/centos/\$\releasever/\$\basearch/" >> /etc/yum.repos.d/nginx.repo
 echo "gpgcheck=0" >> /etc/yum.repos.d/nginx.repo
 echo "enabled=1" >> /etc/yum.repos.d/nginx.repo

Now that the repos are added, we can install all the packages and its dependencies simply with yum.

yum install -y unzip git gcc-c++ pcre-dev pcre-devel zlib-devel make 
php-apc php-bcmath php-curl php-dba php-dom php-fileinfo php-gd 
php-pecl-igbinary php-intl php-ioncube-loader php-json php-mbstring 
php-mcrypt php-memcache php-pecl-memcached php-mysql php-pdo 
php-pdo-mysql php-sqlite3 php-phar php-posix php-pspell php-recode 
php-soap php-tidy php-wddx php-xmlreader perl wget php-fpm memcached 
GeoIP GeoIP-devel perl-ExtUtils-Embed gd gd-devel libxml2 libxml2-devel 
libxslt-devel libxslt xslt pcre-devel openssl-devel nginx mysql-server

After this, we need to enable the services (mysqld, nginx, php-fpm, memcached) in chkconfig for auto start after reboot.

 chkconfig mysqld on
 chkconfig nginx on
 chkconfig php-fpm on
 chkconfig memcached on

Memcached is installed for caching the static contents like images etc.

Now we can move onto installing the pagespeed module for nginx. Follow these steps for the same:

 cd /root
 wget https://github.com/pagespeed/ngx_pagespeed/archive/release-1.8.31.4-beta.zip
 unzip release-1.8.31.4-beta.zip
 cd ngx_pagespeed-release-1.8.31.4-beta/
 wget https://dl.google.com/dl/page-speed/psol/1.8.31.4.tar.gz
 tar -xzvf 1.8.31.4.tar.gz
 cd /root
 wget http://nginx.org/download/nginx-1.6.0.tar.gz
 tar -xvzf nginx-1.6.0.tar.gz
 cd nginx-1.6.0/

The above given steps downloads the pagespeed module archives and exctrats it in the installaion direcory. Before installing the package, delete all the already compiled object files with the command given below.

make clean

This to clean up all the failed make, if any. After cleaning up, we can configure package with the necessary extensions, data directories etc. Here is my configuration.

./configure --add-module=$HOME/ngx_pagespeed-release-1.8.31.4-beta 
--with-debug --prefix=/usr/share/nginx --sbin-path=/usr/sbin/nginx 
--conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log 
--http-log-path=/var/log/nginx/access.log --http-client-body-temp-path=/var/lib/nginx/tmp/client_body 
--http-proxy-temp-path=/var/lib/nginx/tmp/proxy --http-fastcgi-temp-path=/var/lib/nginx/tmp/fastcgi 
--http-uwsgi-temp-path=/var/lib/nginx/tmp/uwsgi --http-scgi-temp-path=/var/lib/nginx/tmp/scgi 
--pid-path=/var/run/nginx.pid --lock-path=/var/lock/subsys/nginx 
--user=nginx --group=nginx --with-file-aio --with-ipv6  
--with-http_realip_module --with-http_addition_module --with-http_xslt_module 
--with-http_image_filter_module --with-http_geoip_module 
--with-http_sub_module --with-http_dav_module --with-http_flv_module 
--with-http_mp4_module --with-http_gzip_static_module 
--with-http_random_index_module --with-http_secure_link_module 
--with-http_degradation_module --with-http_stub_status_module 
--with-http_perl_module --with-mail --with-mail_ssl_module 
--with-debug--with-pcre --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 
-fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 
-mtune=generic' --with-ld-opt=-Wl,-E

After configuration, create ‘makefile’ with,

make

Now, install the package with the created ‘makefile’ using the command,

make install

nginx –with-http_ssl_module this module will enable SSL module with nginx

nginx –with-pcre This library is necessary for regular expressions support in location directive and nginx rewrite module

Installation is now complete. Now we will have to create Nginx core and tmp directories.

 mkdir -p /var/lib/nginx/tmp/client_body
 mkdir -p /var/log/nginx/cores

Now we will have to start the services manually.

 service iptables restart
 service mysqld restart
 service nginx restart
 service php-fpm restart
 service memcached restart

One last thing to do is to set password for the MySQL root user. You can use the command below.

mysqladmin -u root password <desired_password>

 

With this, Nginx with PHP-FPM and pagespeed installation is complete. We’ll now move on to configuration of Nginx and wesite configuration with Nginx.

Below given is a sample of the Nginx configuration file (/etc/nginx/nginx.conf)

user              nginx;
worker_processes  24;
worker_rlimit_nofile 4096;
worker_rlimit_sigpending 132768;

error_log  /var/log/nginx/error.log;
#error_log  /var/log/nginx/error.log  notice;
#error_log  /var/log/nginx/error.log  info;

pid        /var/run/nginx.pid;

events {
    worker_connections  2048;
    use epoll;
    multi_accept on;
}

http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;
    charset utf-8;
    sendfile        on;
    tcp_nopush     on;
    tcp_nodelay on;
    directio 4m;
    output_buffers 32 512k;
    open_file_cache max=2000 inactive=20s;
    open_file_cache_valid    30s;
    open_file_cache_min_uses 2;
    open_file_cache_errors   off;
    server_tokens off;
    keepalive_timeout  2 2;
    keepalive_requests 100000;
    client_max_body_size 1024M;
    client_body_buffer_size 128k;
    client_body_timeout 10;
    client_header_timeout 10;
    send_timeout 2;
    gzip  on;
    gzip_http_version 1.1;
    gzip_vary on;
    gzip_comp_level 5;
    gzip_min_length 1;
    gzip_proxied any;
    gzip_proxied    expired no-cache no-store private auth;
    gzip_types      text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript text/js application/javascript application/js image/x-jg image/bmp image/gif image/x-icon image/jpeg image/png image/jpg;
    gzip_buffers 16 8k;
    gzip_disable "MSIE [1-6]\.(?!.*SV1)";
    index  index.html index.htm index.php;

upstream php {
server 127.0.0.1:9000;
}
upstream memcached-servers {
server 127.0.0.1:11211;
}
upstream php-fpm {
server 127.0.0.1:9000;
}
fastcgi_buffers 256 16k;
fastcgi_buffer_size 128k;
fastcgi_connect_timeout 3s;
fastcgi_send_timeout 120s;
fastcgi_read_timeout 120s;
fastcgi_busy_buffers_size 256k;
fastcgi_temp_file_write_size 256k;
reset_timedout_connection on;
server_names_hash_bucket_size 100;



include /etc/nginx/conf.d/*.conf;

}

In the above given Nginx config file, there is an ‘include’ directive with which we can include other files into the configuration. In our setup we are creating separate conf files for each website to specify its VirtualHost.

Here is a sample of website conf file included in the Nginx configuration. The conf file shown below is ‘/etc/nginx/conf.d/domain.conf‘, where domain.conf is the conf file of an example domain/website.

server {
    listen   11.11.11.11:80;
    server_name  www.iserversupport.com

        access_log      /var/log/nginx/iserversupport.com.access.log main;
        error_log       /var/log/nginx/iserversupport.com.error.log;
#        rewrite        domain.com/new/ http://iserversupport.com/new/index.php$request_uri? permanent;
if ( $host ~* ^(?!www\.) ) {
    rewrite .* $scheme://www.$host$request_uri permanent;
}

    root   /home/user/public_html/;
location ~* \.(js|css|png|jpg|jpeg|gif|ico)(\?ver=[0-9.]+)?$ {
    expires 1y;
}
        charset utf-8;
        pagespeed   on;
        pagespeed RewriteLevel PassThrough;
        pagespeed MemcachedThreads 1;
        pagespeed MemcachedServers "localhost:11211";
        pagespeed FileCachePath /var/ngx_pagespeed_cache;
        pagespeed Domain friv.co;
        pagespeed Domain *.friv.co;
        #pagespeed MaxCombinedCssBytes -1;
        pagespeed FileCachePath "/var/cache/pagespeed/";
        pagespeed FileCacheSizeKb 1024000;
        pagespeed FileCacheCleanIntervalMs 3600000;
        pagespeed FileCacheInodeLimit 500000;
        pagespeed LRUCacheKbPerProcess 8192;
        pagespeed LRUCacheByteLimit 16384;
        pagespeed RateLimitBackgroundFetches off;
        pagespeed EnableFilters defer_javascript;
        pagespeed EnableFilters combine_css;
        pagespeed EnableFilters rewrite_style_attributes;
        pagespeed EnableFilters combine_javascript;
        pagespeed EnableFilters combine_heads;
        pagespeed EnableFilters insert_dns_prefetch;
        pagespeed EnableFilters rewrite_css,sprite_images;
        pagespeed EnableFilters remove_comments;
        pagespeed EnableFilters collapse_whitespace;
        pagespeed EnableFilters rewrite_javascript;
        pagespeed UseExperimentalJsMinifier on;
        pagespeed EnableFilters canonicalize_javascript_libraries;
        pagespeed Library 128 MD5 canonical_url;
        pagespeed EnableFilters rewrite_css;
        pagespeed EnableFilters move_css_to_head;
        #pagespeed EnableFilters trim_urls;
        pagespeed EnableFilters convert_meta_tags;
        pagespeed EnableFilters extend_cache;
        pagespeed EnableFilters fallback_rewrite_css_urls;
        pagespeed EnableFilters flatten_css_imports;
        pagespeed EnableFilters inline_css;
        pagespeed EnableFilters inline_import_to_link;
        pagespeed EnableFilters inline_javascript;
        pagespeed JsInlineMaxBytes 1024000000;
        pagespeed EnableFilters rewrite_style_attributes;
        pagespeed EnableFilters inline_images;
        pagespeed EnableFilters recompress_images;
        pagespeed CriticalImagesBeaconEnabled false;
        pagespeed EnableFilters lazyload_images;
        pagespeed LazyloadImagesAfterOnload on;
        pagespeed EnableFilters convert_png_to_jpeg;
        pagespeed EnableFilters jpeg_subsampling;
        pagespeed EnableFilters resize_rendered_image_dimensions;
        pagespeed EnableFilters recompress_png;
        pagespeed EnableFilters recompress_jpeg;
        pagespeed EnableFilters strip_image_meta_data;
        pagespeed EnableFilters resize_images;
        pagespeed EnableFilters rewrite_images;
        pagespeed RespectVary on;
        pagespeed DisableRewriteOnNoTransform off;
        pagespeed LowercaseHtmlNames on;
        pagespeed XHeaderValue "Powered By PageSpeed";
        pagespeed Statistics off;
        pagespeed StatisticsLogging off;
        pagespeed LogDir /var/log/pagespeed;
        include 'pagespeed.conf';
        include 'location.conf';
        include 'supercache.conf';
        include 'php-fpm-cache.conf';
        include 'memcached.conf';
        include 'wordpress.conf';
        include 'mobile.conf';
        include 'static.conf';
        include 'php.conf';
        include 'denies.conf';
    }


server {
    server_name www.iserversupport.com;
    rewrite ^/(.*)$ http://domain.com/$1 permanent;

 

 

Now please upload your contents to /home/user/public_html/
With this, you are set to host websites with Nginx with PHP-FPM, MySQL, PageSpeed and Memcached setup. Enjoy your time :)

install nginx php-fpm mysql