Install LEMP stack for WordPress on DigitalOcean

December 23, 2015   Ohsik Park

Install Ubuntu + Nginx + MySql + PHP on Mac for your WordPress site.
Addition to that, we will install Postfix and SSL.

– First thing first, you need to get a droplet from DigitalOcean and set it up with Ubuntu. It will be a click through process on DigitalOceans dashboard.
For more info,
https://www.digitalocean.com/community/tutorials/how-to-create-your-first-digitalocean-droplet-virtual-server.

– In order to connect to your droplet securely, you need to set up SSH key.
https://www.digitalocean.com/community/tutorials/how-to-use-ssh-keys-with-digitalocean-droplets
By doing this process, you will have id_rsa and id_rsa.pub files in your local machine and authorized_keys on your droplet.

– It is recommended to create another user beside ‘root’ user. This tutorial will show you how to create new user that you will be using for most of time.
https://www.digitalocean.com/community/tutorials/initial-server-setup-with-ubuntu-14-04
You will end up with new users directory with new authorized_keys file. ‘e.g. home/YOUR_NEW_USERNAME/.ssh/authorized_keys’.

Now you have a new username with SSH key which will allow you to login to your droplet without password.
by typing ssh YOUR_NEW_USERNAME@YOUR_IP_ADDRESS in your terminal.

– Recommended setting for Ubuntu 14.04 server
https://www.digitalocean.com/community/tutorials/additional-recommended-steps-for-new-ubuntu-14-04-servers

 

The fun part begins here.

Let’s install Linux, nginx, MySQL, PHP (LEMP) stack on your Ubuntu 14.04!
If you completed above steps, you can jump right into installing Nginx web server on this article.
This article will walk you through installing Nginx, MySql and PHP in your Ubuntu web server and how to configure it to work together.

https://www.digitalocean.com/community/tutorials/how-to-install-linux-nginx-mysql-php-lemp-stack-on-ubuntu-14-04

Now you have your Ubuntu configured and installed LEMP stack! Ready to go with WordPress install!

You will need to do

  1. Create a MySQL Database and User for WordPress
  2. Download WordPress to your Server
  3. Configure WordPress
  4. Copy the Files to the Document Root (from your user folder to document root)
  5. Modify Nginx Server Blocks
  6. Open up your site on web browser and finish installation

This article will walk you through all those steps.
https://www.digitalocean.com/community/tutorials/how-to-install-wordpress-with-nginx-on-ubuntu-14-04

** At this point, you have a fresh WordPress website running on your Ubuntu Server.
Try running your site on Google pageload speed tool or https://loader.io/ to see what performance you get. You can compare this result with the result you will get after you applying your theme and activating some plugins you might need and migrating DB if you are moving your site.

Allow WordPress to download/update themes/plugins.
If you have trouble downloading/updating themes or plugins from admin panel, run below command to change permission.
chown -Rf www-data.www-data /var/www/html/

Having trouble uploading files from FTP(FileZilla)?

  • add user “demo” to group “www-data” (Replace demo with your username) sudo usermod -a -G www-data demo
  • set permissions for user group www-data sudo chgrp -R www-data /var/www/html
  • Run sudo chmod -R g+w /var/www/html

More info, https://www.digitalocean.com/community/questions/why-doesn-t-chown-r-root-www-data-work-on-my-wordpress-installation

Optimize Nginx Configuration for better performance
https://www.digitalocean.com/community/tutorials/how-to-optimize-nginx-configuration

 

** Postfix and SSL
If you want your WordPress to send out email such as account validation or any notice email by wp_mail() or mail() functions, you need to install Postfix or Sendmail.
As far as I researched, Postfix has more popularity over Sendmail and known to be perform better.

– Install and set up Postfix on your server

https://www.digitalocean.com/community/tutorials/how-to-install-and-setup-postfix-on-ubuntu-14-04

– Create an SSL Certificate on Nginx for Ubuntu 14.04
https://www.digitalocean.com/community/tutorials/how-to-create-an-ssl-certificate-on-nginx-for-ubuntu-14-04

 

Also check out WordPress migrate plugins if you are moving your site to DigitalOcean. It will make your work easier.
https://wordpress.org/plugins/wp-migrate-db/
https://github.com/wp-sync-db/wp-sync-db
https://wordpress.org/plugins/all-in-one-wp-migration/

After moving your WordPress site, check your new WordPress site’s performance.
https://developers.google.com/speed/pagespeed/?hl=en
http://tools.pingdom.com/fpt/
https://gtmetrix.com/
https://loader.io/
https://www.blitz.io/

 

Setting up the Web server by yourself seems very intimidating and it still does to me. According to DigitalOcean, completing these steps will get you a decent web server for your WordPress site. However, you just have to look & play around with it to understand how things are working (at least I feel like it).

There are some scripts you can use to automate all these steps. If you like that approach, check out https://easyengine.io/. I wanted to learn and understand what is going on with my server so I will have ability to set up better server as I go on which made me to take me the long way and hoping one day I will get all this 🙂

 

Backup process

List of things to go through

  1. Create backup folder (Need for the first time only)
  2. MySql backup
  3. WordPress folder backup (including theme and upload folder)
  4. Download files to desktop
  5. Remove backup files on remote server

MySql backup

  1. Create backup folder in remote server for sql files
    mkdir /backup/mysql
  2. Run mysqldump command
    mysqldump -uUSERNAME -hlocalhost -pPASSWORD DB_NAME | gzip -9 > /backup/mysql/BACKUP_FILE_NAME-`date +\%m\%d\%y`.sql.gz
  3. Check backup file is generated in /backup/mysql folder

WordPress backup
This will backup all php files and media files in entire WordPress folder.

  1. Create backup folder in remote server for WordPress
    mkdir /backup/wordpress
  2. Get to the directory where you see the directory you want to archive and gzip (this will keep the backup file clean)
    cd /var/www/html/
  3. Run command to archive, gzip and move the file to target location
    tar czvfz /backup/wordpress/BACKUP_FILE_NAME-`date +\%m\%d\%y`.tar.gz NAME_OF_DIRECTORY_YOU_WANT_TO_ZIP
    Try run this with sudo if you are getting error message of tar: Exiting with failure status due to previous errors
  4. Check backup file is generated in /backup/wordpress folder

Download files
Pull command
rsync -a username@remote_host:/backup LOCAL_MACHINE_LOCATION
or
scp -r username@remote_host:/backup .
to download current location in local machine.

Remove backup files on remote server to save space
If you have enough space, feel free to keep your files in the server.

Leave a Reply

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