Site icon Thetechhacker

How to Install WordPress on Digital Ocean with Apache (LAMP)

Install WordPress on Digital Ocean

WordPress on Digital Ocean, the dreams come true. One of the best cloud hosting providers hosting the CMS powering almost 40% of the entire internet known to mankind. What could be better than that? Today we are going to unveil the secrets of combining them both. Whether it’s a blog or a professional website, if you want to build it using WordPress, this guide will be a quick start. We will be guiding you from setting up your server to giving your website a name. So, without wasting time, let’s get started.

Before Signing up for Digital Ocean

If you already signed up for Digital Ocean, bad luck for you. But if you haven’t, you have a chance to get $100 credit for 2 months for trying Digital Ocean. Yup, for 2 months you do not have to worry about paying a penny as long as you choose a server within that credit limit. So, if you want to save some money or give Digital Ocean a try before paying them, you can use this link. Simply click on it and claim your $100 digital ocean credit.

Using the One-Click App (Automatic)

While the main focus of this tutorial is to provide the complete steps for manually configuring and setting a server from scratch and installing WordPress on it, we understand that everyone doesn’t want to do that. For those who want a quick solution to get a WordPress website up and running on a Digital Ocean server, there is a One-Click app provided by them.

This app simply does almost everything mentioned in this tutorial automatically. All you have to do is giving it a couple of information such as your email address and some passwords for the tools we are going to install. So, how can we get started?

As soon as you complete phase 1, you should immediately access the droplet and complete phase 2 mentioned below as well. It is not recommended to leave the droplet after the phase 1. If you do not have enough time to complete the process, do not create a droplet now.

1. Set up the Droplet

  1. Just go to the Digital Ocean marketplace page of the WordPress one-click installer. You will have all the information you need regarding the WordPress one-click installer on that page.
  2. Click on the Create WordPress Droplet button.
  3. If you haven’t signed in to Digital Ocean, do that. It will take you to a screen where you can choose your server resources and confiture things like backups, database clusters, server locations, etc.
  4. Choose the appropriate settings as per your intentions.
  5. When you are ready, simply click on the Create Droplet button.
  6. Wait for the digital ocean to complete the droplet setup.

As soon as the droplet setup is complete, you can see a demo page when you visit the IP address of the droplet. But we are not done yet.

You can find the IP address of the droplet by hovering over the droplet in the dashboard.

2. Access the Droplet

We are not done yet. We have a bit more setup remaining. To continue, you have to access the droplet via SSH. Using an SSH client such as PuTTY.

  1. Open PuTTY or any other SSH clients of your choice.
  2. Type in the IP address of your droplet, leave the port number as it is.
  3. Click on Open button.
  4. PuTTY will warn you about the trustworthiness of the connection. No problem. Just click on Yes.
  5. You will be taken to a black screen where you can enter your username and password. You have to enter the username and password you received via email.
  6. After successfully logging into the droplet, it will ask you a couple more things such as your email address. Provide them as well. Just read the instructions on the screen and proceed accordingly.
  7. After providing all the details, and connecting the domain (if you have one), visiting the website will bring up the WordPress setup screen as we mentioned in the last section of this tutorial. You continue from there.

Step 1. Setup

To manually install WordPress, the first thing we need is a server that has all the requirements set by WordPress. According to the official WordPress website, as of writing this article, the recommended hosting requirements for installing WordPress are as follows.

That’s it. That’s all you need to run a WordPress website on any web server including Digital Ocean. So, we just have to make sure that these things are installed on top of the server software of our choice.

Wait! Server software? Yes. There are mainly two web server software widely used by millions of websites all over the globe. Apache/NGINX. Both of them has benefits and downsides. We will cover tutorials for both the web servers on our website. But which one you go for?

So, beginners, go with Apache. People with some experience managing websites/servers who run high-traffic websites, go with NGINX. You can do more research on these two servers. Here is a great article on NGINX vs Apache if you want to take a look. In this guide though, we will concentrate on the Apache.

1. Create the Droplet

The first thing you have to do is creating a droplet we can work with. Digital Ocean makes it extremely easy to setup droplets. We are going to use their user friendly interface to setup an Ubuntu server from scratch.

  1. Login to your Digital Ocean account.
  2. Click on the Create button at the top.
  3. Choose Droplets. It will take you to the droplet creation screen where you can choose configurations of your droplet.
  4. Choose an image/distribution, as of now, you can go with Ubuntu 20.04. But you are free to use any Ubuntu versions.
  5. Choose the plan, backup, and database settings as needed. It is completely based on your preferences. As a rule of thumb, for running a small website a $5 or $10 droplet is enough. Those who run multiple websites may need bigger droplets based on their requirements. So, you have to decide before continuing.
  6. Choose a data center as well. It is better to choose a location closest to your target audience.
  7. Choose an authentication method. Passwords will do. But if you need a more secure method, you can go with SSH keys (it will require additional setup).
  8. As soon as everything is ready, give your project a name and click on Create Droplet button at the bottom.

Digital Ocean will take you to the dashboard where you can see the progress of the droplet creation. Wait for it to complete. In most cases, it will only take a couple of minutes.

Throughout the tutorial, you may see several “output” code blocks. It is just to show you how the output will look like in the console. You SHOULD NOT enter it in the console. So watch out for “output” at the beginning of the code.

2. Initial Setup

Just creating a droplet is not enough. There are some things you should do before continuing. Login to the SSH client of your choice by following the steps mentioned in the “Access the Droplet” section in the one-click installer method.

Let’s create a user first. Replace “john” with a username of your choice.

adduser john

This will create a user named john which we are going to use to login to the console later.

But, our user doesn’t have any permissions. Let’s give him the necessary permissions to perform operations on the server.

usermod -aG sudo john

Now, we added “john” to the “sudo” group which will allow him to perform operations on the server using the “sudo” keyword at the beginning of every single command as you will see later in this tutorial.

Now, let’s setup a very basic firewall for our web server. UFW is a firewall that comes with Digital Ocean droplets. Let’s see what applications are there right now which can be configured on our firewall.

ufw app list

You should see a result like this. DO NOT enter it. It is just an output.

Output
Available applications:
  OpenSSH

Now, let’s allow OpenSSH on the firewall.

ufw allow OpenSSH

Now we can enable the firewall.

ufw enable

And, check the firewall status.

ufw status

You should get an output like this.

Output
Status: active

To                         Action      From
--                         ------      ----
OpenSSH                    ALLOW       Anywhere
OpenSSH (v6)               ALLOW       Anywhere (v6)

Cool. We are ready to go.

Until now, we used the root user to perform operations. From now on, we are going to use the “sudo” user we just created. Now, close the SSH client and open it again. This time, use the username and password of the user we just created.

Step 2. Install

In order to make things working, we have to install our stack. Let’s do it.

1. Install Apache

If you chose Apache as your web server, we will have to install the LAMP (Linux, Apache, MySQL, and PHP) stack on the server in order to continue.

First, we are going to install Apache. Run the following commands (one after the other) to do that.

If you see multiple lines of codes in the following code blocks, be sure to run them one after the other. Not both at the same time.

sudo apt update
sudo apt install apache2

You may be asked to enter your droplet password. Provide that and press Enter. If prompted to confirm the installation, type Y and press Enter. The process will start installing Apache on the server.

Now, we have to allow the HTTP traffic to the server by configuring the UFW firewall that comes with Digital Ocean droplets. The below command will list all the available UFW application profiles.

sudo ufw app list

You can see something like this. (DO NOT enter it in the console.)

Output
Available applications:
  Apache
  Apache Full
  Apache Secure
  OpenSSH

For now, we can allow the traffic on port 80 because we didn’t yet set up the SSL. When we do, we will configure it again. No worries. Enter the following command in the console and press Enter.

sudo ufw allow in "Apache"

Then to verify the changes have taken effect, use,

sudo ufw status

It should output something like this. (DO NOT enter it in the console).

Output
Status: active

To                         Action      From
--                         ------      ----
OpenSSH                    ALLOW       Anywhere                                
Apache                     ALLOW       Anywhere                  
OpenSSH (v6)               ALLOW       Anywhere (v6)                    
Apache (v6)                ALLOW       Anywhere (v6) 

We have now enabled traffic through port 80. If everything went perfectly, you can now visit your servers IP address and see an Apache web page there.

http://servers_ip_address

If you do not know the IP address of your server, you can just look at the droplet you just created in the Digital Ocean dashboard.

2. Install MySQL

Now, we need a database software. In this tutorial we are going to use MySQL which is one of the most popular tools for database management.

Use this command to install MySQL on your droplet.

sudo apt install mysql-server

If prompted type Y and press Enter. It will now start installing MySQL. Patiently wait for the process to complete.

Now, we have to secure it a bit. MySQL, by default, comes with a script that allows us to do that. Run

sudo mysql_secure_installation

It will ask you if you want to enable the VALIDATE PASSWORD PLUGIN.

If enabled, this feature will reject any passwords that don’t match certain criteria. That means everyone can only use passwords that are strong and secure based on your preferences. While this will enhance the security of MySQL, it is not actually required. So, we recommend you to type N and press Enter when asked if you want to enable this feature or not.

Next, it will ask you to enter and confirm a password for MySQL root user (note it down somewhere safe, you will need it later). Provide a strong password. Keep in mind that this password is not as same as the System password. This is the MySQL root password.

For all the questions after this, press Y and then Enter. These will remove anonymous users and the test database, disable remote root logins to MySQL, and load these new rules.

To confirm MySQL has been successfully installed, use this command.

sudo mysql

You should see a screen similar to this. DO NOT enter it in the console.

Output
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 22
Server version: 8.0.19-0ubuntu5 (Ubuntu)

Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> 

To exit the screen, type exit in the mysql> command line and press Enter.

The MySQL server is now installed on your droplet and ready to be used. But we are not done yet, we have one more thing to install on the server.

3. Install PHP

WordPress is made of PHP. So, obviously, we need PHP to run the application and some additional modules to connect it to the database and server. We can start off by installing them. Enter the following command and then press Enter key.

sudo apt install php libapache2-mod-php php-mysql

When asked confirm the action by pressing Y and Enter. Wait for the process to complete. You can verify everything went perfectly using this command.

php -v

It should output something like this.

Output
PHP 7.4.3 (cli) (built: Mar 26 2020 20:24:23) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
    with Zend OPcache v7.4.3, Copyright (c), by Zend Technologies

That’s it. Now, you have everything required to install WordPress on your web server.

Install PHP extensions

We already installed PHP on our server. But it is not enough to run WordPress. Let’s install some PHP extensions as well.

sudo apt update
sudo apt install php-curl php-gd php-mbstring php-xml php-xmlrpc php-soap php-intl php-zip

These extensions will do for now. But in the future, if any plugin/theme you install requires additional extensions, you might have to install them as well using the same command replacing the extension names.

Let’s reload Apache again.

sudo systemctl restart apache2

Step 3. Configure

Now, we have to configure our webserver to serve our website. We will now set up the apache virtual host file to connect to our WordPress website and adjust its configurations.

In the below sections, be sure to replace “example.com” with whatever folder name you like, preferably the name of your website. And also “example” with something as well. Just make sure to be consistent in the naming. Otherwise, things will break. Things you should replace are provided in yellow.

First of all, let’s create a folder to put our WordPress files.

sudo mkdir /var/www/example.com

Now, let’s assign the ownership of this folder to the current user.

sudo chown -R $USER:$USER /var/www/example.com

Then, we can create a new configuration file for our website.

sudo nano /etc/apache2/sites-available/example.conf

In the editor, paste the following lines. To paste something, just Right-Click after copying.

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

We can save and close the editor by pressing Ctrl + X, then Y and Enter.

Now, we can enable the newly created virtual host by typing.

sudo a2ensite example.conf

By default, apache creates a virtual host file, we do not need that as we already have a custom one set up. So, let’s disable it.

sudo a2dissite 000-default

Before moving forward, let’s make sure that the apache configuration files doesn’t have any errors.

sudo apache2ctl configtest

And finally, let’s reload Apache.

sudo systemctl reload apache2

So, now, the website is ready to be used. Anything you place in the /var/www/example.com folder will be available on the domain name or IP address. To test it, let’s create a file and try to access it.

nano /var/www/example.com/index.html

Then paste the following code in it.

<html>
  <head>
    <title>Example.com</title>
  </head>
  <body>
    <h1>It's Working...</h1>
  </body>
</html>

Now save and close the editor by pressing Ctrl + X, then Y and Enter.

Let’s see if it is working. Try visiting your domain/IP address again. It should say “It’s Working…”.

Great. But we are not done yet. By default, Apache gives preference to the index.html file than the index.php file that comes with WordPress. So, we have to change that.

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

It will open the dir.conf file which you have to modify as follows.

<IfModule mod_dir.c>
        DirectoryIndex index.php index.html index.cgi index.pl index.xhtml index.htm
</IfModule>

This will give preference to the index.php file. Save and close the editor by pressing Ctrl + X, the Y and then Enter.

Now, reload Apache for the changes to take effect.

sudo systemctl reload apache2

You may also remove the index.html file we created in previous steps using this command.

sudo rm /var/www/example.com/index.html

Allowing Apache Configuration Overrides

WordPress uses .htaccess file in its root folder to control the server behavior and configurations. By default, this file may not be able to do that. In order to allow .htaccess and other files in the website root folder to control the configuration, we have to make a little adjustment. Remember the Apache configuration file we created? Let’s edit it again.

sudo nano /etc/apache2/sites-available/example.conf

Inside the <virtualhost> tags in the already existing code, place the following lines as well.

<Directory /var/www/example.com/>
    AllowOverride All
</Directory>

Your virtual host file should look something like this now.

<VirtualHost *:80>
    ServerName example.com
    ServerAlias www.example.com
    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/example.com
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
    <Directory /var/www/example.com/>
        AllowOverride All
    </Directory>
</VirtualHost>

Enable Apache Rewrite Module

WordPress need the Apache Rewrite module to be enabled to rewrite the URLs, let’s enable it.

sudo a2enmod rewrite

Then, be sure to check that there are no errors with this command.

sudo apache2ctl configtest

You might receive the following warning. You can ignore it.

Output
AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1. Set the 'ServerName' directive globally to suppress this message
Syntax OK

If you want to fix that, you can edit the /etc/apache2/apache2.conf file and add a ServerName directive to it. It can be something like your primary domain or IP address.

Step 4. Create a MySQL Database

We already installed MySQL, but we haven’t created a database where WordPress can store its data. Let’s do it. Login to the MySQL console by typing.

mysql -u root -p

When prompted, provide the MySQL password you set during the MySQL installation.

In the following steps, make sure that you use the ; (semi-colon) whenever it is mentioned. Do not do it in a hurry. Every MySQL statement should end with a semi-colon.

Now, let’s create a database. Replace “example” with a database name. DO NOT include any spaces. If you need a separation, use underscores instead. Example: my_database.

CREATE DATABASE example DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci;

We have been using the default root user so far. We can create an additional user on our MySQL installation and grant the necessary permissions.

Be sure to change “example” to the database name you just created, “exampleuser” with a username, and “password” to a strong password of your choice. Again, no spaces.

GRANT ALL ON example.* TO 'exampleuser'@'localhost' IDENTIFIED BY 'password';

For the changes to take effect, let’s flush the priviliages.

FLUSH PRIVILEGES;

We are done with MySQL. So, we can exit now.

EXIT;

Step 5. Setup WordPress

Now, everything is done except WordPress. Let’s download and set it up.

1. Download WordPress

We can download WordPress to a temporary folder.

cd /tmp
curl -O https://wordpress.org/latest.tar.gz

Then extract it.

tar xzvf latest.tar.gz

Create an .htaccess file.

touch /tmp/wordpress/.htaccess

Copy the wp-config-sample.php file to our actual wp-config.php file which will be used to connect to our database and control some WordPress configuration.

cp /tmp/wordpress/wp-config-sample.php /tmp/wordpress/wp-config.php

We may also create an upgrade directory for WordPress to perform software upgrades without running into any trouble.

mkdir /tmp/wordpress/wp-content/upgrades

And finally, move the folder in our temporary directory to our actual working folder.

sudo cp -a /tmp/wordpress/. /var/www/example.com

2. Setting the correct File and Folder permissions

In order for WordPress to have access to modify folders and files, add them, remove them, or similar operations, we have to provide it the necessary capabilities. Run the following commands one by one. As always, do not forget to replace example.com with your directory name.

sudo chown -R www-data:www-data /var/www/example.com
sudo find /var/www/example.com/ -type d -exec chmod 750 {} \;
sudo find /var/www/example.com/ -type f -exec chmod 640 {} \;

3. Secure with Salts

WordPress provides an option to generate some random salt keys to protect the login and cookies. We have to generate some random salts and add it in the wp-config.php file in your websites’s root directory.

Firs’ts of all, go to this URL.

curl -s https://api.wordpress.org/secret-key/1.1/salt/

It will give you an 8 line code, something like this.

define('AUTH_KEY',         'kg2qkinKf:-|umSm.%6fU6y41i)d!i74S(/zQhXYy=Ycn.[d*y(8gtE+%62O.~g)');
define('SECURE_AUTH_KEY',  'q630dCRLh43qXaT/f c.--v<,M0|wWlbW<_}N1R<$-*C1<<mu0{96D[5akPm =I>');
define('LOGGED_IN_KEY',    'gNDMVra[Urp#c&i7 Nr9XP}8]D?iW*JGZ(dEoK^TLnL9p3C(L),32vH-Avq_nyz$');
define('NONCE_KEY',        'nDCU9cwWI.aOOR#+swX2R+CO>NvuRA.r<}-=xQ^,)/jl,EJ^X$,FLQ4la|Y<r%uV');
define('AUTH_SALT',        'wf-alL&/+YT>]&#5fV3o)1&}=MX8uEiQF=`W08<ODM00tpmKj(r9-.#~;,&p.KIa');
define('SECURE_AUTH_SALT', 'skpR-R0k/731R~p&U][%:I!r.3*g]|/-zrRfJfV8<qG1 MWg+Ey*=NXjFmK/{-6/');
define('LOGGED_IN_SALT',   'yP_$l3A%yt Y17@?a@Nl+Buv`l4eI/+b2CH6uc#sA5/d`)C)R+OK|m:)w&D/m-7Z');
define('NONCE_SALT',       'M&UvpKwJm3s@ErU4vH*-6pJjn!!70x)HU4+pHF%Ik|.&|nO~#MVE MS.{ofKZCcg');

Now, open the wp-config.php file.

sudo nano /var/www/example.com/wp-config.php

Find an 8 line section that says,

define('AUTH_KEY',         'put your unique phrase here');
define('SECURE_AUTH_KEY',  'put your unique phrase here');
define('LOGGED_IN_KEY',    'put your unique phrase here');
define('NONCE_KEY',        'put your unique phrase here');
define('AUTH_SALT',        'put your unique phrase here');
define('SECURE_AUTH_SALT', 'put your unique phrase here');
define('LOGGED_IN_SALT',   'put your unique phrase here');
define('NONCE_SALT',       'put your unique phrase here');

Replace it with the code you just generated.

DO NOT use the keys in the example we provided above. It is just an example. You should only use the keys you just generated from that URL.

Do not close the wp-config.php file yet, we have one more thing to do.

4. Connect WordPress to the Database

For WordPress to connect to the databse, we have to provide our database details.

In the wp-config.php file, you will see some lines like below. Change the “example”, “exampleuser”, and “password” to the values you used previously while creating database and the MySQL user.

define('DB_NAME', 'example');

/** MySQL database username */
define('DB_USER', 'exampleuser');

/** MySQL database password */
define('DB_PASSWORD', 'password');

After doing that, save and exit the editor by typing Ctrl + X, then Y and Enter.

5. WordPress Installation

After successfully setting up the webserver, installing all the prerequisites, configuring everything mentioned above, it is time to step into the final procedure, setting up WordPress.

Navigate to your website domain or IP address.

http://www.example.com

You will see that WordPress is asking for you to select a language. Which means, everything is working as expected.

Choose a language of your choice and press Continue.

The next screen will ask you some details related to your WordPress website such as the website title, your email address, a password, etc. Provide them, and click on Install WordPress.

WordPress will now start setting up everything. As soon as everything is ready, you can log in to your WordPress dashboard from http://www.example.com/wp-admin/.

Congratulations. You just installed WordPress on Digital Ocean.

Exit mobile version