WordPress Installation on Ubuntu Linux with Apache and MySQL

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 Linux. We’ll use Apache as our HTTP server, and also install PHP and MariaDB (an open source implementation of MySQL) since WordPress requires them in order to function. This assortment of packages is commonly referred to as a LAMP stack (Linux, Apache, MySQL, PHP). 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 a self signed SSL certificate, or get a free one from Let’s Encrypt, which enables your site to utilize HTTPS.

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 a self signed SSL certificate for your WordPress site
  • How to configure a free SSL certificate from Let’s Encrypt

WordPress admin menu

WordPress admin menu

Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Ubuntu Linux
Software WordPress, Apache, PHP, MariaDB, SSL certificate
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 system 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 php-curl php-xml php-mbstring php-imagick php-zip php-gd

This is a lot of packages to install, and yes, they are all required. Without some of the extra PHP modules, you will not get a perfect score in the “Site Health Status” section of the WordPress administrator dashboard.

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 Install and Configure MySQL Workbench on Ubuntu Linux 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. Start by opening up MySQL with the root user:


    $ sudo mysql
    
  2. Create a new database for WordPress. In this example, we will call ours wordpress_db, but you can use whatever name you’d like.
    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. For this example, we’ll make our username wordpress_user and our password my_password. 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 or just localhost 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 virtual host 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 your favorite text editor you prefer to open this file:
    $ sudo nano /etc/apache2/sites-available/wordpress.conf
    
  3. Change the DocumentRoot setting 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 fully qualified 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.

  6. Filling out the DocumentRoot and ServerName values in Apache virtual host file

    Filling out the DocumentRoot and ServerName values in Apache virtual host file

  7. 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
    
  8. 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 configure proper permissions on the directory and all its files.
    $ 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.
  5. Initial WordPress setup wizard

    Initial WordPress setup wizard

  6. 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.


  7. Fill out the MySQL database information we configured earlier

    Fill out the MySQL database information we configured earlier

  8. 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.
  9. WordPress has successfully connected to our MySQL database

    WordPress has successfully connected to our MySQL database



  10. 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.
  11. Fill out your site title, username, password, and email

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

  12. WordPress installation is now complete! You can click on the “log in” button to get started creating content.
  13. 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

Configure a self signed SSL certificate

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
    


  2. Creating a self signed SSL certificate

    Creating a self signed SSL certificate

  3. 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
    
  4. 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
    
  5. Change the DocumentRoot and SSL certificate values inside the SSL virtual host file of Apache

    Change the DocumentRoot and SSL certificate values inside the SSL virtual host file of Apache

  6. 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
    


  7. Finally, enable the SSL site we configured and reload Apache:
    $ sudo a2ensite default-ssl
    $ sudo systemctl reload apache2
    

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

Configure a free SSL certificate from Let’s Encrypt

Let’s Encrypt is a free service that provides websites with SSL certificates. If you’ve come this far and have set up your WordPress site on Ubuntu, it’s only a few more steps to get SSL encryption configured, which will prevent man in the middle attacks, helps your page’s SEO, and browsers like Firefox won’t warn users that your site is insecure.

This process is very easy and call all be done from the Ubuntu command line. Follow along with the steps below to finish setting up your website with an SSL certificate from Let’s Encrypt.

  1. To setup SSL encryption using Let’s Encrypt, install the certbot utility with the following command.
    $ sudo apt install certbot python3-certbot-apache
    
  2. Configure the SSL certificate by executing the following command and going through the prompts that pop up. The last question will ask you if you want to redirect HTTP requests straight to HTTPS. It’s recommended that you opt for this. Obviously, for this to work the domain must point correctly to our publicly accessible server IP.
    $ sudo certbot --apache
    

That’s all there is to it. The certbot utility does almost all the legwork for us, and will make all the necessary changes to your Apache virtual host files. It will also keep your SSL certificate active, by renewing it whenever it’s about to expire.

Closing Thoughts

This guide has shown you how to install top notch components to run a WordPress website on Ubuntu Linux. 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.