How To Set Up WordPress On A Digital Ocean VPS Running Ubuntu

So you’ve got the perfect idea for your own personal blog or new e-commerce website and you know you’re going to need some way of managing the content on your site that is proven, powerful, easy to use, and flexible for whatever your specific needs are. After reviewing the available options you chose to use WordPress because it is very popular and there are a lot of amazing themes and plugins you can use to easily change the appearance and add functionality to your website. It is also easy to create your own WordPress plugins or themes if you have some experience with web programming languages like PHP, HTML, CSS, and JavaScript. WordPress also has one of the largest communities so support will never be hard to find.

Great! So now what?


This guide will show you how to start working with WordPress today. You will install WordPress on a virtual private server (VPS) and configure everything to create your own website. This guide recommends Digital Ocean to run WordPress on a Ubuntu server however it should be basically the same on any other Linux server. Hope you follow along and if you have any questions feel free to ask them below in the comments. Let’s begin!

  1. Register your domain if you haven’t already.
  2. Create a new project on Digital Ocean.
  3. Create and setup a new droplet on Digital Ocean.
  4. Install software and configure web server
  5. Install and configure WordPress
  6. Start publishing content

A droplet is what Digital Ocean calls their VPS. The options when you create your droplet allow you to choose which operating system to install on your droplet. You can also install pre-configured disk images or restore from a droplet backup or custom made image. A majority of the sites on the web are running on a Linux server although there are other options like using Windows. For most people though, if this is your first time setting up a droplet, you will want to choose the Ubuntu distribution as most tutorials you find will probably assume you are using Linux.

You then need to choose the plan for your droplet. This will determine the amount of resources the droplet will have access to. For a small to medium site you can start with a basic shared CPU plan with 1GB of ram and 25GB of storage. You may find as you develop your site or app that you will need additional resources to make your application run. The good news is that it is easy to reconfigure your droplet as needed if you require more ram or storage space.

Digital Ocean also offers block storage which you can choose to add if you need programatic control over your storage volumes or if you have files that you want to easily share between droplets. If this is your first and/or only website then you probably won’t need to use block storage yet.

The next step is to choose the region where your droplet will be located. This is important if you want to optimize the speed of your site for a particular region.

You can choose to add additional features to your droplet like IPv6, and performance monitoring/alerts.

Next, you need to set up a way of authenticating yourself on your new droplet. The best way to do this is with a SSH key. You can create an SSH private key and public key on your computer. Alternatively you can create a password for the root user.

Give your droplet a name that will help you identify it and assign it to your Digital Ocean project that you created earlier. It is also a good idea to add automatic backups for 20% of the droplet cost to ensure that if you make a mistake you can restore your site if anything goes wrong.

The next thing to do is point your domain to your digital ocean droplet. How to do this depends on where you registered your domain but one method is to change the settings for your domain to update the domain’s nameservers to point to Digital Ocean’s. Having your nameservers set to Digital Ocean’s allows you to manage all of your DNS settings and your droplets in one location from the Digital Ocean control panel. After setting your domain’s nameservers to Digital Ocean you can setup the DNS settings on Digital Ocean to point to your droplet. Alternatively, you could modify the DNS settings of your domain to direct traffic to the IP address of your Digital Ocean droplet.

If you have set your domain’s nameservers to Digital Ocean’s and are using Digital Ocean to manage your DNS settings then before you can connect to your site you need to set the A Record for your site. To do this choose the Networking menu option from the Manage menu and add your new domain. Then in the “Create New Record” section enter @ for the hostname and choose your droplet from the “Will direct to” dropdown. Leave the TTL as is and press the create record button. TTL means Time-To-Live and it affects how long your DNS settings are cached by DNS servers. DNS settings may take a little while to propagate through the internet and are cached by DNS servers for the TTL time, so if you can’t reach your site right away you should be able to in a few minutes.

Now let’s start setting up our VPS. We first need to connect to our server in a terminal using SSH. Open a terminal and run the following command to connect to your VPS.

> SSH root@domainname.com

The first part “SSH” is the application we are running to connect to our site. the next part “root@domainname.com” tells the SSH program to connect as the user called root to the server located at domainname.com. If you have properly set up the DNS for your site you should see some information in your terminal that you are now connected to Ubuntu or if this is your first time connecting to the server you will see a message like the following:

The authenticity of host 'yourdomain.com (123.456.789.010)' can't be established.

RSA key fingerprint is 97:8c:1b:f2:6f:14:6b:5c:3b:ec:aa:46:46:74:7c:40.
Are you sure you want to continue connecting (yes/no)? yes

This is normal when connecting for the first time so just type yes to continue. You are now connected to your VPS as the root user. The root user has authorization to do anything they want on the server but generally for security it is best not to use the root user account unless your have to so now is a good idea to set up a user account for yourself.

You can create a new user by running the following command replacing username with whatever you want your username to be.

> adduser username

When you create your new user, you will need to choose a password (make it a good one) and enter some information about the new user. Once your user is created you should add them to a special group called sudo that allows users to elevate their privileges when necessary to have administrator rights for some commands (known as becoming the super user). You can then execute administrator commands by prefacing them with the sudo command e.g. sudo reboot. To add your newly created user to the sudo group enter the following command and replace username with the name of the user you created earlier.

> usermod -aG sudo username

It is also a good idea at this time to secure our VPS with a firewall to help prevent hacking. We will set up a firewall using a program called ufw (Uncomplicated FireWall) this will allow us to restrict access to our VPS to only the ports that we want to allow. In this case we will allow access via SSH as well as HTTP and HTTPS. Later we might add permission to access our database from our local development computer but for now lets set up ufw by entering the following commands

> ufw allow OpenSSH
> ufw allow http
> ufw allow https

It is important to make sure ssh has been allowed otherwise we won’t be able to connect to our server in the terminal any more. We can check what ufw will allow with the following command.

> ufw status

It should show that SSH is allowed. If so we can enable the firewall with the command

> ufw enable

Check the status again to ensure it is enabled. We should now switch over to our newly created user but first we will need to copy our ssh public key into our new user’s folder so we will be authorized to connect as that user over SSH. There are a few ways to do this but I like to use the rsync utility. Enter the following command to copy the ssh folder from the root user to your new user. Replace username with the name of the user you created.

> rsync --archive --chown=username:username ~/.ssh /home/username

This command is saying to use rsync in archive mode to copy the folder .ssh from the root user’s home directory to the new home directory for the new user that was created and to change the ownership of that folder to the new user. We can now disconnect from our VPS by typing

> exit

And now we should be able to reconnect as our user by typing

> ssh username@domainname.com

Now it’s time to install some software on your new droplet. The collection of software that runs a website is called the application stack. A common stack for websites is LAMP for Linux Apache MySQL and PHP (or WAMP on Windows and MAMP on Mac however most servers run Linux), another common stack is MEAN for MongoDB, Express, Angular, and Node or MERN (replacing Angular with React) or MEVN (replacing Angular with Vue). LAMP is a time-tested classic stack and is also the stack that WordPress runs on so that’s what we’ll install now.

We have the Linux part of the stack installed on our droplet already. In order to use a web browser to connect to our website we need to install a web server. The web server in LAMP is Apache (Express in MEAN). Installing the apache web server on your VPS is easy. Ubuntu uses a program called apt to install packages (other programs). First we need to make sure apt is up to date and it’s a good idea to update (upgrade) the current packages as well (usually it is safe to do this but it is always a good idea to backup your site first in case something goes wrong. Digital Ocean makes it easy with the Snapshot feature allowing you to create a backup image of your site that you can revert to at any time). Enter the following commands to update apt, upgrade the current packages, and then install apache2.

> sudo apt update
> sudo apt upgrade
> sudo apt install apache2

When apt is done installing apache we need to configure it. Let’s start by creating the folder that will hold our website files. Enter the following commands but change epicprogramming.com to the name of your website.

> sudo mkdir -p /var/www/epicprogramming.com/html

Giving the mkdir command the -p flag will tell it to create the parent directories as well if they don’t already exist. Enter the following commands to assign the appropriate read/write permissions to the folders.

> sudo chown -R $USER:$USER /var/www/epicprogramming.com/html
> sudo chmod -R 755 /var/www/epicprogramming.com

Apache uses virtual host configuration files to manage sites. Let’s set up a virtual host so Apache knows where to find the files for our website. This is important if we want to have multiple websites hosted on the same VPS, we can have multiple virtual hosts configured.

Add a virtual host file with the following command. (Change epicprogramming.com to the name of your domain)

> sudo nano /etc/apache2/sites-available/epicprogramming.com.conf

This will open the text editor application, nano, for you to edit the contents of the virtual host file. Enter the following template for your virtual host (but change epicprogramming.com to your domain name. This also sets up the server alias for the www prefixed domain. If you don’t want to set this up then you can omit the ServerAlias line. (If you do want the www prefix it also requires its own A record in your DNS settings)

<VirtualHost *:80> 
ServerAdmin admin@epicprogramming.com
ServerName epicprogramming.com
ServerAlias www.epicprogramming.com
DocumentRoot /var/www/epicprogramming.com/html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined

# this allows Apache to use the .htaccess file for configuring your site
# which is important for WordPress to rewrite the url for permalinks
<Directory /var/www/epicprogramming.com>
   AllowOverride All
</Directory>

</VirtualHost>

Now enable the site with the following command. (again change epicprogramming.com to the name of your website)

> sudo a2ensite epicprogramming.com.conf

Disable the default site.

> sudo a2dissite 000-default.conf

Run apache config test.

> sudo apache2ctl configtest

Finally, restart apache.

> sudo systemctl restart apache2

If you get a message “AH00558: Could not reliably determine the server’s fully qualified domain name” edit the apache2.conf configuration file and add ServerName 127.0.0.1 to the end of the file.

> sudo nano /etc/apache2/apache2.conf

Let’s test to see if your Apache web server is set up correctly. Create a file called index.html in the directory you set up earlier. index.html is a special file that Apache knows to look for and load as the entry point for your website.

> nano /var/www/epicprogramming.com/html/index.html

Hopefully you should see the page is working properly, but this page is kind of boring right? In order to get WordPress set up we first need to install a database application for WordPress to use. The database is where WordPress will store all of its information. Install MariaDB (which is the basically the same thing as mysql) using this command.

> sudo apt install mariadb-server

configure the mysql server with this command.

> sudo mysql_secure_installation

During install set the root user password, remove anonymous users, disallow remote root login, remove test database, and reload privilege tables.

Now that MariaDB is installed, you need to create a database for WordPress to use. Login to MariaDB as root.

> sudo mariadb -u root -p

Create a new mysql user for WordPress

MariaDB> CREATE USER 'wordpressuser'@'%' IDENTIFIED BY 'replace_with_password';

Create the database for WordPress

MariaDB> CREATE DATABASE wordpress;

Grant privileges to the WordPress user account

MariaDB> GRANT ALL PRIVILEGES ON wordpress.* TO 'wordpressuser'@'%';

Exit mysql

MariaDB> exit

You have now setup a Linux VPS with the Apache web server and MariaDB mysql database and also secured it with the UFW firewall. You should also secure your site so that when users access the site in their browser the communication is end-to-end encrypted so that no one else can see what is being transmitted. This is helpful to prevent user data breaches and increase SEO rankings and user trust in the site but is especially important for e-commerce websites. In order to secure your site which is currently being served with the http protocol and have it instead be served over the secure https protocol you need to install an SSL certificate. Some sites make you pay for this service but this is how you can easily install an SSL certificate on your VPS for free. To install a free SSL certificate you can use Let’s Encrypt. Let’s Encrypt offers a tool that makes installation and renewal of SSL certificates easy. The tool is called certbot and we can install it using snapcraft another package manager like apt. Make sure snapcraft is up to date using this command

> sudo snap install core
> sudo snap refresh core

Then install certbot using snapcraft with this command

> sudo snap install --classic certbot

Make a symbolic link to the certbot application so it can be run easily from the usr/bin folder.

> sudo ln -s /snap/bin/certbot /usr/bin/certbot

Now run certbot to set up the SSL certificates.

> sudo certbot --apache

This will make a new virtual host configuration file based on your existing virtual host that will redirect any traffic that comes in on http to https. It will also set up a reoccurring renewal so you won’t have to do it manually when the certificates expire.

The next part of the LAMP stack we need is PHP. PHP is a server side programming language that can be called by Apache to execute code on the server before sending the response back to the client. To install the latest version of PHP you need to add a repository using the following commands.

> sudo apt install software-properties-common
> sudo add-apt-repository ppa:ondrej/php

You have a choice to run PHP as a standalone PHP-CGI or as an Apache module PHP-FPM. I recommend the FPM as it’s faster and uses less memory.

> sudo apt install php8.0-fpm libapache2-mod-fcgid

enable the PHP-FPM module

> sudo a2enmod proxy_fcgi setenvif
> sudo a2enconf php8.0-fpm

and restart Apache

> systemctl restart apache2

If you want to check which mods are enabled in apache you can run the following command.

> sudo apache2ctl -M

In order to let PHP communicate with mysql we also need to install the php-mysql module

> sudo apt install php-mysql

When apache looks for the entry point to your website it looks for a file named index.html. If we want it to look for a file named index.php first, we need to modify the configuration. Open the following file and move index.php to the beginning of the list.

> sudo nano /etc/apache2/mods-enabled/dir.conf

It is also a good idea to enable http2 which is the new version of the http protocol that is faster and more efficient.

> sudo a2enmod http2
> sudo nano /etc/apache2/apache2.conf

Add the following at the end of the apache2.conf file

Protocols h2 http/1.1

And restart Apache for the changes to take effect.

> systemctl restart apache2

You can use a tool like https://http2.pro/ to check if you have http2 configured correctly.

You are almost ready to install WordPress but first some additional PHP modules needed. You can see which PHP modules are installed by checking the phpinfo page or typing php -m. Here is a list of php modules recommended for WordPress (https://make.wordpress.org/hosting/handbook/handbook/server-environment/). If you have been following this guide so far you just need to add curl, dom (virtual package installed with php-xml), mbstring, imagick, xml and zip.

The following command installs the php modules recommended for WordPress.

> sudo apt-get install php8.0-curl php8.0-mbstring php8.0-imagick php8.0-xml php8.0-zip

(Note: PHP configuration files are located /etc/php/8.0, Apache configuration files are located in /etc/apache2)

It’s taken a lot of work to get this far so congratulations you should be proud of what you accomplished and you are almost finished! You are now ready to install WordPress. An easy way to install and manage WordPress on your virtual private server is by using the WordPress CLI. This is a command line tool that will download and set up WordPress for you. It can also be used to do maintenance on WordPress and install plugins. First use curl to download the WP-CLI

> curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar

The WP-CLI needs to be made into and executable file and placed in a location where the system can locate it called the PATH. These commands will accomplish this.

> chmod +x wp-cli.phar
> sudo mv wp-cli.phar /usr/local/bin/wp

Confirm that WP-CLI was installed correctly by typing

> wp --info

Download WordPress into a folder

> wp core download --path=epicprogramming-wp

Change directory into the folder you downloaded WordPress into

> cd epicprogramming-wp

Now use WP-CLI to generate a wpconfig file. This file is used to configure WordPress settings such as the database connection. The following command will create a wpconfig file with the database settings for a database user wordpressuser to access the database wordpress. You will have to enter the password for that database that we set up earlier.

> wp config create --dbname=wordpress --dbuser=wordpressuser --prompt=dbpass

In order to let wordpress install plugins and manage files on the server it is recommended to give wordpress filesystem access. Enter the following command to add the file system access to the wpconfig

> echo "define('FS_METHOD', 'direct');" >> wp-config.php

Apache will also need permission to write to the folders when updating WordPress or its plugins and themes. To give Apache permission first you need to find out which user Apache is running as. Enter the following command:

sudo apachectl -S

Near the end of the output you should something like

User: name="www-data" id=33
Group: name="www-data" id=33

In this case Apache is running as user www-data in the www-data group. Run the following commands to change the group ownership and read/write permissions for your WordPress site (change the directory to the one where your site is installed)

sudo chown -R $USER:www-data /var/www/epicprogramming.com
sudo chmod g+w -R /var/www/epicprogramming.com

Finally it’s time to install WordPress. The following command will do that. (Change the url and other options where appropriate)

> wp core install --url=epicprogramming.com --title="Epic Programming" --admin_user=epicadmin --admin_email=admin@epicprogramming.com --prompt=admin_password

If you see a message “sh: 1: /usr/sbin/sendmail: not found” this is because WP-CLI is attempting to send a message to the newly created WordPress admin user’s email. You can disregard this as its recommended to use the WP SMTP plugin to configure WordPress to send mail.

If you successfully made it this far you should have a working WordPress site that you can login to by going to your domain.com/wp-admin and entering the account information you used when installing WordPress. Now the fun begins as you can modify your WordPress site by installing themes and plugins and creating your first posts and pages!

If you enjoyed this guide, please leave a comment about how your experience was installing WordPress on DigitalOcean. This was the first article I have created so please send me some feedback if you have any suggestions for how to improve it or any topics that you think I should cover in future articles and please feel free to ask any questions you may have. Hope you have fun and have lots of success creating your WordPress website!

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments