Web

Installing a LEMP server with WordPress and Google Pagespeed

Installing a LEMP server with Wordpress and Google Pagespeed

If you are looking to host a webserver and want a really reliable, stable platform that has excellent performance, a LEMP server using the latest flavor of Ubuntu – 14.04 along with Nginx and Google Pagespeed is hard to beat. ย Google Pagespeed can be a little tricky to get going with Nginx, however, in the below post we want to go over installing a LEMP server with WordPress and Google Pagespeed and the steps involved, components and configuration needed to make this happen.

First things first, it is assumed that you already have a fresh install of Ubuntu 14.04 Server patched and ready to go before completing the below steps. ย We are using a VM to install into so we can leverage snapshots and other virtual machine specific technologies that makes our lives easier when it comes to system administration.

Installing Core LEMP components

  • sudo apt-get install mysql-server mysql-client
  • sudo apt-get install nginx
  • sudo service nginx start
  • sudo apt-get install php5-fpm
  • setup sites-available in nginx or this step can wait
  • sudo apt-get install php5-mysql php5-curl php5-gd php5-intl php-pear php5-imagick php5-imap php5-mcrypt php5-memcache php5-ming php5-ps php5-pspell php5-recode php5-snmp php5-sqlite php5-tidy php5-xmlrpc php5-xslย hp5-xcache php5-memcached memcached
  • sudo service php5-fpm restart
***Note*** – If you are running the install inside a VM, snapshot the virtual machine at this point before running steps below as bad things can happen either accidentally or otherwise running the steps below
ย 
***Note*** – If restoring a snapshot, you need to sync time up on the server as things can get weird if not after restoring:
ย 
  • ntpdate ntp.ubuntu.com

The following steps involve removing the default nginx installed with Ubuntu and installing the compiled version with Pagespeed and basically writing over the top of the default installation and is a modified version of steps found here.

Check for latest version of pagespeed here:

https://developers.google.com/speed/pagespeed/module/release_notes

  • apt-get install build-essential zlib1g-dev libpcre3 libpcre3-dev
  • cd ~
  • wget https://github.com/pagespeed/ngx_pagespeed/archive/master.zip
  • sudo apt-get install unzip
  • unzip master.zip
  • cd ngx_pagespeed-master/
  • wget https://dl.google.com/dl/page-speed/psol/1.9.32.1.tar.gz
  • tar -xzvf 1.9.32.1.tar.gz
  • cd ~
  • wget https://labs.frickle.com/files/ngx_cache_purge-2.1.tar.gz
  • tar -xvf ngx_cache_purge-2.1.tar.gz
  • check https://nginx.org/en/download.html for the latest version
  • wget https://nginx.org/download/nginx-1.7.4.tar.gz
  • tar -xvzf nginx-1.7.4.tar.gz
  • cd nginx-1.7.4/
  • sudo apt-get install libgd2-xpm-dev
  • sudo apt-get install openssl libssl-dev libperl-dev
  • ./configure –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 –user=www-data –group=www-data –with-http_ssl_module –with-http_gzip_static_module –with-http_image_filter_module –with-http_spdy_module –add-module=$HOME/ngx_pagespeed-master –add-module=$HOME/ngx_cache_purge-2.1
  • make
  • service nginx stop
  • mv /etc/nginx ~
  • rm -rf /usr/sbin/nginx /etc/nginx /usr/share/nginx
  • make install
  • Edit the new fastcgi_params file /etc/nginx/fastcgi_params and add: fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  • Return the configuration files to their respective places in /etc/nginx Move or copy or create the files in sites-available, symlink them to sites-enabled, and so on
  • service start nginx
  • If you have errors about the port being in use, kill nginx (Note the last time I ran through theses steps, just killing nginx didn’t work….still got the port in use error. What resolved the issue was copying over the nginx.conf file from the other server. I think something in the default file doesn’t call the right sites-available or one at all)
  • ps -e pkill nginx or killall -9 nginx
  • service nginx start

Copying over data from one web server to another, creating the new database and installing WordPress.

  • Create the DB on the target server
  • Comment out the bind-address in the my.cnf under /etc/mysql if you have issues connecting to MySQL with MySQL workbench
  • login to MYSQL commandline on the target server and run GRANT ALL PRIVILEGES ON *.* to ‘root’@’%’ IDENTIFIED BY ‘password’;
  • Then run flush privileges;
  • Create the directory /var/www
  • Download the latest wordpress wget https://wordpress.org/latest.tar.gz to the /var/www directory just created
  • tar -xzvf latest.tar.gz
  • Rename toย the desired website folder name by using the mv wordpress yourfoldername
  • configure the wp-config.php file for database parameters and connection information
  • Copy over the wp-content directory from backup if you are moving a WordPress site to this new server
  • set permissions using chown -R www-data:www-data /var/www command
  • If setting up local authentication using the apache utilities setup using the following command:
  • sudo apt-get install apache2-utils

Setup your Default sites-available file for Nginx with Pagespeed turned on:

server {
        listen   80;


        root /var/www;
        index index.php index.html index.htm;

        server_name yourservername;
ย  ย  ย  ย pagespeed on;
ย ย ย ย ย ย  # let's speed up PageSpeed by storing it in the super duper fast memcached
ย ย ย ย ย ย  pagespeed MemcachedServers "localhost:11211";ย 
ย 
ย ย ย ย ย ย  pagespeed RewriteLevel OptimizeForBandwidth;
ย ย ย ย ย ย  pagespeed EnableFilters collapse_whitespace,remove_comments,combine_javascript,sprite_images,inline_google_font_css,inline_preview_images,resize_mobile_images;
        location / {
                try_files $uri $uri/ /index.php?q=$uri&$args;
        }

        error_page 404 /404.html;

        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
              root /usr/share/nginx/www;
        }

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        location ~ \.php$ {
                try_files $uri =404;
                #fastcgi_pass 127.0.0.1:9000;
                # With php5-fpm:
                fastcgi_pass unix:/var/run/php5-fpm.sock;
                fastcgi_index index.php;
                include fastcgi_params;
                 }
        

}

Final Thoughts

Keep in mind that you may need to do more than the above config to get your server in operating order as each environment and various components can be different depending on which OS and web components are installed or need to be installed for your particular website. ย However, using the above steps you will be able to get a fully functional LEMP server with Pagespeed up and running fairly quickly.

Subscribe to VirtualizationHowto via Email ๐Ÿ””

Enter your email address to subscribe to this blog and receive notifications of new posts by email.



Brandon Lee

Brandon Lee is the Senior Writer, Engineer and owner at Virtualizationhowto.com, and a 7-time VMware vExpert, with over two decades of experience in Information Technology. Having worked for numerous Fortune 500 companies as well as in various industries, He has extensive experience in various IT segments and is a strong advocate for open source technologies. Brandon holds many industry certifications, loves the outdoors and spending time with family. Also, he goes through the effort of testing and troubleshooting issues, so you don't have to.

Related Articles

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.