Ubuntu 20.04 WordPress with Apache installation

WordPress has remained one of the best and easiest ways to get a sleek site up and running ever since its inception in 2003. As a matter of fact, current trends only show its popularity continuing to increase. WordPress is simple to use, and even hosting it yourself isn’t that hard, as we’ll prove to you in this article.

In this guide, we’ll show you how to get your site hosted with WordPress on Ubuntu 20.04 Focal Fossa. We’ll use Apache as our HTTP server, and also install PHP and MySQL since WordPress requires them in order to function. Once those packages are installed, we’ll go over the configuration of Apache and MySQL, including initial setup of a database and user, before installing WordPress itself. Towards the end, we’ll also show you how to configure optional SSL, in case you’d like your site to use HTTPS.

If you’re more familiar with or happen to prefer Nginx over Apache, we’ve written a separate guide for Ubuntu 20.04 WordPress installation on Nginx.

In this tutorial you will learn:

  • How to install and configure Apache
  • How to install and configure MariaDB for MySQL
  • How to setup a MySQL user and database for WordPress
  • How to download and install WordPress
  • How to configure SSL for your WordPress site

WordPress website running on Ubuntu 20.04 with Apache

WordPress website running on Ubuntu 20.04 with Apache

Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Installed or upgraded Ubuntu 20.04 Focal Fossa
Software WordPress, Apache, PHP, MariaDB (MySQL)
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user

Install Apache, PHP, and MySQL

The first thing we’ll need to do is prep our Ubuntu 20.04 computer with the proper software packages. WordPress can only run if we provide it with an HTTP server, PHP and its related modules, and a MySQL database. Open a terminal and type the following couple of commands to install Apache, PHP, and MariaDB, which is an open source fork of MySQL:

$ sudo apt update
$ sudo apt install apache2 php libapache2-mod-php mariadb-server mariadb-client php-mysql

Configure MySQL



One of the first things we should do is get our WordPress database ready. In order to do that, we first need to do some initial configuration of MySQL. To get started, execute the following command in terminal:

$ sudo mysql_secure_installation

Leave the first response blank and press enter. You should reply with y (yes) to the rest of the prompts, and configure a root password when prompted to do so. This setup only takes a moment to complete.

The initial setup of MySQL with mysql_secure_installation

The initial setup of MySQL with mysql_secure_installation

Although the above configuration will easily suffice for our WordPress site, you can read our guide on MySQL installation on Ubuntu 20.04 if you’re curious enough to dive a little deeper.

Create a database for WordPress

WordPress stores all of its post and page content, among other information, inside of MySQL. We will need to configure a MySQL user and database for WordPress to access with the following steps:

  1. Open up MySQL with the root user:
    $ sudo mysql
    
  2. Create a new database for WordPress:
    MariaDB [(none)]> CREATE DATABASE wordpress_db;
    
  3. Next, we need to create a new user that WordPress can use to access the database we just created. Replace the my_password text below with a secure password (and write it down somewhere for later):
    MariaDB [(none)]> CREATE USER 'wordpress_user'@'localhost' IDENTIFIED BY 'my_password';
    
  4. Then, give the WordPress user full permissions on the WordPress database:
    MariaDB [(none)]> GRANT ALL PRIVILEGES ON wordpress_db.* to wordpress_user@'localhost';
    
  5. Lastly, save the changes you’ve made to user permissions and exit MariaDB:
    MariaDB [(none)]> FLUSH PRIVILEGES;
    MariaDB [(none)]> exit
    
Configuring MySQL database and user for WordPress

Configuring MySQL database and user for WordPress

Configure Apache

Apache should already be installed and running at this point, and that can be verified by opening a browser and navigating to loopback address 127.0.0.1 on your system.

Default Apache page, indicating that our website is accessible

Default Apache page, indicating that our website is accessible

Although Apache is hosting our site (or lack of one) already, it’s best practice to configure a new Apache site file for our WordPress install. This will allow you more flexibility in the future if you want to host multiple websites or make changes to where the WordPress directory is installed, etc.



  1. Copy the default Apache configuration into a new file with the following command:
    $ sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/wordpress.conf
    
  2. Then, use nano or whatever text editor you prefer to open the file:
    $ sudo nano /etc/apache2/sites-available/wordpress.conf
    
  3. Change the DocumentRoot to where we plan to install WordPress. The directory below is our suggestion.
    DocumentRoot /var/www/wordpress
    
  4. Create a setting for ServerName and enter your website’s domain name. If you don’t have one, leave it as localhost.
    ServerName your-site.com
    
  5. Create an alias for the www prefix as well. This isn’t necessary if you are just using localhost.
    ServerAlias www.your-site.com
    

    This is how your config file should look when you’re done. Note that we commented out the alias line in our config since we are only hosting locally.

    Filling out the DocumentRoot and ServerName values in Apache site file

    Filling out the DocumentRoot and ServerName values in Apache site file

  6. Save your changes and exit the file. Then, enable the site in Apache and disable the default site.
    $ sudo a2ensite wordpress.conf
    $ sudo a2dissite 000-default.conf
    
  7. Finally, reload Apache for the new changes to take effect.
    $ sudo systemctl reload apache2
    

Download and install WordPress

Now that we have our HTTP server configured and PHP and MySQL are ready to go, we can move on to the installation of WordPress itself.

  1. First, use wget to download the latest version of WordPress:
    $ wget -O /tmp/wordpress.tar.gz https://wordpress.org/latest.tar.gz
    
  2. Extract the tar archive into your WordPress site directory:
    $ sudo tar -xzvf /tmp/wordpress.tar.gz -C /var/www
    
  3. Be sure to give the Apache user ownership of the site directory:
    $ sudo chown -R www-data.www-data /var/www/wordpress
    
  4. Now we can get started with configuring WordPress. Open your internet browser and navigate either to the localhost address 127.0.0.1 or your fully qualified domain name if you set one up. You should be greeted by the WordPress setup wizard. Click “Let’s go” to get started.

    Initial WordPress setup wizard

    Initial WordPress setup wizard

  5. Next, enter the database information that you configured earlier. The last two boxes (database host and table prefix) can be left at their default values. Click “Submit” when you’re finished.

    Fill out the MySQL database information we configured earlier

    Fill out the MySQL database information we configured earlier

  6. WordPress will attempt to make a connection with the database and let you know if it was successful. Assuming it was, click “Run the installation” to continue.

    WordPress has successfully connected to our MySQL database

    WordPress has successfully connected to our MySQL database

  7. The next screen will ask you for some general information about your new site. After you finish filling this out, click “install WordPress” at the bottom of the screen to finalize the installation.

    Fill out your site title, username, password, and email

    Fill out your site title, username, password, and email

  8. WordPress installation is now complete! You can click on the “log in” button to get started creating content.

    WordPress has installed successfully. Click log in to find the admin menu

    WordPress has installed successfully. Click log in to find the admin menu



Note that to get back to the WordPress admin panel in the future, you can always use the URL http://127.0.0.1/wp-admin (or replacing 127.0.0.1 with your fully qualified domain name).

WordPress admin menu

WordPress admin menu

Your WordPress site should now be accessible from http://127.0.0.1 or your fully qualified domain name.

Our WordPress site is now up and running

Our WordPress site is now up and running

Optional SSL configuration

We’re finished configuring our WordPress site, but right now it’s using HTTP instead of HTTPS. With a lot of the web moving exclusively to HTTPS, you may want to consider it for your site as well, even though it’s not strictly necessary. In this section of the guide, we’ll show you how to enable SSL on your website with a self signed certificate.

  1. Type the following command in terminal to generate a self signed certificate. You’ll be prompted with a few general questions. Be sure to fill out the “common name” field with either your website’s IP address or fully qualified domain name.
    $ sudo openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout /etc/ssl/private/apache-selfsigned.key -out /etc/ssl/certs/apache-selfsigned.crt
    

    Creating a self signed SSL certificate

    Creating a self signed SSL certificate

  2. Next, we need to make some changes to our Apache site configuration. Open the default SSL configuration file with nano or another text editor:
    $ sudo nano /etc/apache2/sites-available/default-ssl.conf
    
  3. Change the DocumentRoot value to where you installed WordPress earlier. Then, change the SSLCertificateFile and SSLCertificateKeyFile values to where we saved our SSL files. See the screenshot below for reference.
    DocumentRoot /var/www/wordpress
    SSLCertificateFile /etc/ssl/certs/apache-selfsigned.crt
    SSLCertificateKeyFile /etc/ssl/private/apache-selfsigned.key
    
    Change the DocumentRoot and SSL certificate values inside the SSL site file of Apache

    Change the DocumentRoot and SSL certificate values inside the SSL site file of Apache

  4. Save your changes and exit the file. Then, enable the SSL module for Apache and restart for the changes to take effect:
    $ sudo a2enmod ssl
    $ sudo systemctl restart apache2
    
  5. Finally, enable the SSL site we configured and reload Apache:
    $ sudo a2ensite default-ssl
    $ sudo systemctl reload apache
    

    All done. Your WordPress site is now capable of using SSL encryption:

    HTTPS is now enabled on our WordPress site

    HTTPS is now enabled on our WordPress site

Conclusion

This guide has shown you how to install top notch components to run a WordPress website on Ubuntu 20.04 Focal Fossa. WordPress is an awesome content management system with virtually endless configuration. It’s so simple that someone without any HTML, CSS, or PHP coding experience can have a great looking website. Be sure to browse through the WordPress menus to see all the customization power you have at your fingertips.