Tech Blog
WordPress on VPS

Installing WordPress on a Virtual Private Server (VPS) from scratch
Overview
When we first set up our Evergreen Innovations website, we used a managed WordPress solution via shared resources on a GoDaddy server. We then asked the question – how difficult is it to set up a WordPress site from scratch on a self-managed virtual private server (VPS)? Trawling existing online forums did not provide a satisfactory answer. Having now gone through the process, our answer is – not all that hard – if you can grasp the basics of managing a Linux environment. This blog summarizes the steps required to go from a raw VPS to a fully functioning website. The includes setting up the VPS, installing WordPress, enabling SSL (HTTPS) certificates, and setting up Virtual Hosts for multiple domain entry points.
Virtual Private Server (VPS)
- Getting a VPS - The first step is to purchase a VPS. We opted for a GoDaddy product that gives us a dedicated fixed IP address and a single CPU with sufficient memory and storage. The responsiveness of our website improved drastically compared to the shared resources option. - There are plenty of VPS providers out there, so take your pick. In terms of operating system, we opted for Ubuntu 16.04, simply because we use Ubuntu for many of our non-web projects and are very familiar with the environment. Other more web-focused Linux platforms should work just as well. - Your VPS provider is likely to offer serval support options – we opted for the unmanaged solution, whereby the provider gives us the option of rebuilding the server (to say Ubuntu 16.04), but all other management is down to us. We prefer it that way, as we want full control over our server environment. 
- Logging in the first time - One key advantage of a VPS is that you will have direct root SSH access. If you are unfamiliar with SSH, imagine this as a simple way of accessing a remote server via the command line. - $ ssh your-username@your-ip-address $ bash- The password and user account (your-username) is generally set up via your VPS provider during the (Ubuntu) installation phase. The - bashcommand starts up the bash terminal environment.
- Making sure your VPS is up-to-date - Run an update once the server OS is build. On Ubuntu, this works as follows - $ sudo apt-get update $ sudo apt-get dist-upgrade
- Install some basic tools - These tools are useful later on in the process - $ sudo apt-get install htop $ sudo apt-get install vim $ sudo apt-get install git- The - htoptool is our favorite resource monitor,- vimis an excellent command line editor and the- gitsource control system is required for some- gitproject cloning. These tools are optional for the instructions here, but- vimis used in some of the below commands. We recommend installing these three basic packages, as they are very commonly required.
- Install Apache web server - An Apache web server may already be running on your platform (it was pre-installed on our Ubuntu 16.04 system obtained via GoDaddy). Check http://your-ip-address, and if the web server is running you should see the Apache landing page. If not, you can install the Apache server as follows - $ sudo apt-get install apache2 apache2-utils $ sudo systemctl enable apache2 $ sudo systemctl start apache2
- Install MySQL - WordPress stores its information in a MySQL database. Install MySQL as follows - $ sudo apt-get install mysql-client mysql-server $ sudo mysql_secure_installation- The secure installation will give you some options concerning password strength etc. Use whichever settings seem appropriate to your usage, and stay on the more secure-side if you are unsure about how your system will be used eventually. 
- Install PhP - PhP is required for WordPress. Install as follows (version 7.0 was the latest at the time of writing this blog) - $ sudo apt-get install php7.0 php7.0-mysql libapache2-mod-php7.0 php7.0-cli php7.0-cgi php7.0-gd- Once installed, create the file info.php in your root web directory. - $ sudo vim /var/www/public_html/info.php- Note that this directory is commonly under - /var/www/htmlor- /var/www/public_htmlas shown above, but you may want to opt for a different directory structure if you are planning to host multiple websites on the same VPS. If you are unsure, scroll down to look at the section on Virtual Hosts. Edit the file- info.phpas follows:- <?php phpinfo(); ?>- Check the link http://your_ip_address/info.php, and if all is working, you should see a PhP landing page with lots of detailed information about your PhP installation. 
Install WordPress and MySQL database
- Install WordPress Content Management System (CMS) - In your server’s home directory (/home/your-username), get the latest WordPress distribution and unpack the tar - $ wget -c http://wordpress.org/latest.tar.gz $ tar -xzvf latest.tar.gz- Move the WordPress content to the correct folder. - $ sudo rsync -av wordpress/* /var/www/public_html/- Note again that this destination folder may differ if you wish to run several pages on the same server. Set the permissions such that pages can be loaded correctly in the public space - $ sudo chown -R www-data:www-data /var/www/public_html/ $ sudo chmod -R 755 /var/www/public_html/
- Update the database search function - Update the database as follows - $sudo updatedb
- Resolve a common Permalink issue - This step is required for WordPress Permalinks (such as www.your-domain.com/your-post) to work properly. First, activate mod_rewrites - $ sudo a2enmod rewrite $ sudo service apache2 restart- Then open the Apache config file - $ sudo vim /etc/apache2/apache2.conf- Within this file, change - AllowOverride noneto- AllowOverwrite Allunder- /var/www. Finally, restart the Apache server for the changes to apply.- $ sudo service apache2 restart
- Create the WordPress database - Start up the MySQL Shell, where the password is the root password of your Linux system - $ mysql -u root -p- Create the database and then show all databases to see that the new database was created successfully - mysql> CREATE DATABASE wp_some_name; mysql> SHOW databases;- Set the username and password (replace - some_userwith your chosen user name and- some_passwordwith your chosen password), and exit the MySQL Shell- mysql> GRANT ALL PRIVILEGES ON wp_some_name.* TO 'some_user'@'localhost' IDENTIFIED BY 'some_password'; mysql> FLUSH PRIVILEGES; mysql> EXIT;- Next, we need to edit the WordPress configuration file to point to the correct database - $ cd /var/www/public_html/ $ sudo mv wp-config-sample.php wp-config.php $ sudo vim wp-config.php- Make the following changes to the - wp-config.phpfile- define('DB_NAME', 'wp_some_name’); /** MySQL database username */ define('DB_USER', 'some_user’); /** MySQL database user name as chosen above */ define('DB_PASSWORD', 'some_password’); /** MySQL password as chosen above */- The remaining settings in the - wp-config.phpfile can remain at their default values. Finally, restart the Apache and MySQL services- $ sudo systemctl restart apache2.service $ sudo systemctl restart mysql.service
Setting up WordPress
You have now installed all the required modules to set up your WordPress installation. Simply go to http://your-ip-address and follow the on-screen instruction. Once all is set up, you can login at http://your-ip-address/wp-admin.
Making sure your website is encrypted
All above links pointed to http://, not https://. We strongly recommend that you set up SSL encryption! A website without SSL does just not look professional. It’s simple and free to set up SSL (https), so follow these instructions. Your host / VPS provider will try to sell you SSL certificates – but unless you like spending money on pretty much nothing – we recommend the free route via Let’s Encrypt (certbot).
- Install certbot on your system - $ sudo apt-get update $ sudo apt-get install software-properties-common $ sudo add-apt-repository ppa:certbot/certbot $ sudo apt-get update $ sudo apt-get install python-certbot-apache
- Configure certbot - $ sudo certbot --apache- Follow the on-screen instructions. We recommend selecting option [2] when asked for - All HTTPS.
- Additional domains - If you add domains to your Virtual Host (see below), you will also need to extend your SSL certificates for these domains. This is easily done as follows - sudo certbot certonly --cert-name first-domain.com -d www.first-domain.com,www.second-domain.com,second-domain.com- To make sure all entry points are covered, we added both the - www.form and the form without- www..
- Make sure everything in your WordPress installation points to HTTPS - There may be some residual links or images in your WordPress installation (database) that point to http content. For example, an image may be stored under http://your-domain/wp-content/uploads/image.png, instead of https://your-domain/wp-content/uploads/image.png. This can lead to mixed content warnings in your browser, which are very distracting (for both the web admin and the user). To fix this, we had good success with the WordPress - Search & Replaceplugin, where you can replace any http database entry by https. Backup your database before running this plugin. The- Search & Replaceplugin allows for a dry-run, showing all the offending http occurrences.
Setting up Virtual Hosts
Taking our website as an example, we use the domains https://www.evergreeninnovations.co.uk and also https://www.evergreeninnovations.co. In our DNS settings, both of these domains point to the same IP address. To ensure that they are directed to the same content, we can set up a Virtual Host on our server.
- Create a config file - Copy the existing Apache configuration template and open the copied file - $ sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/your-domain.conf $ sudo vim /etc/apache2/sites-available/your-domain.conf
- Set up the VirtualHost - Set up your virtual host environment. In the example shown below, we set up - ServerAliasand- RewriteCondfor two sites (example1.com and example2.com).- <VirtualHost *:80> ServerAdmin your-name@your-domain ServerName example1.com ServerAlias www.example1.com example2.com www.example2.com DocumentRoot /var/www/public_html ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined RewriteEngine on RewriteCond %{SERVER_NAME} =www.example1.com [OR] RewriteCond %{SERVER_NAME} =example1.com [OR] RewriteCond %{SERVER_NAME} =www.example2.com [OR] RewriteCond %{SERVER_NAME} =example2.com [OR] RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent] </VirtualHost>
- Enable the new config file - sudo a2ensite your-domain.conf
- Restart server after making changes - sudo service apache2 restart
The above ensures that all domain are captured, and the rewrite rule points all domains (even if they are entered using http) to the correct https address. If you wanted to direct the two pages to different content, you can create several document roots, e.g. var/www/example1/public_html and var/www/example2/public_html. The Virtual Host then requires different entry points (instead of the * used here) for each domain.
Building the Website in WordPress
Now that your server and WordPress environment are fully setup, you can start the fun part of building your website. To get started, we recommend the excellent video series by Tyler.
We hope that you found our post helpful. Just email us if you feel we should cover any particular points in more detail.
