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
|Category||Requirements, Conventions or Software Version Used|
|Software||WordPress, Apache, PHP, MariaDB, SSL certificate|
|Other||Privileged access to your Linux system as root or via the
# – requires given linux commands to be executed with root privileges either directly as a root user or by use of
$ – 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.
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.
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:
- Start by opening up MySQL with the root user:
$ sudo mysql
- 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;
- 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_userand our password
my_password. Replace the
my_passwordtext below with a secure password (and write it down somewhere for later):
MariaDB [(none)]> CREATE USER 'wordpress_user'@'localhost' IDENTIFIED BY 'my_password';
- Then, give the WordPress user full permissions on the WordPress database:
MariaDB [(none)]> GRANT ALL PRIVILEGES ON wordpress_db.* to wordpress_user@'localhost';
- Lastly, save the changes you’ve made to user permissions and exit MariaDB:
MariaDB [(none)]> FLUSH PRIVILEGES; MariaDB [(none)]> exit
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.
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.
- 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
- Then, use nano or your favorite text editor you prefer to open this file:
$ sudo nano /etc/apache2/sites-available/wordpress.conf
- Change the
DocumentRootsetting to where we plan to install WordPress. The directory below is our suggestion.
- Create a setting for
ServerNameand enter your website’s fully qualified domain name. If you don’t have one, leave it as localhost.
- Create an alias for the
wwwprefix as well. This isn’t necessary if you are just using localhost.
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.
- 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
- 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.
- First, use wget to download the latest version of WordPress:
$ wget -O /tmp/wordpress.tar.gz https://wordpress.org/latest.tar.gz
- Extract the tar archive into your WordPress site directory:
$ sudo tar -xzvf /tmp/wordpress.tar.gz -C /var/www
- Be sure to configure proper permissions on the directory and all its files.
$ sudo chown -R www-data.www-data /var/www/wordpress
- Now we can get started with configuring WordPress. Open your internet browser and navigate either to the localhost address
127.0.0.1or 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.
- 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.
- 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.
- 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.
- WordPress installation is now complete! You can click on the “log in” button to get started creating content.
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).
Your WordPress site should now be accessible from
http://127.0.0.1 or your fully qualified domain name.
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.
- 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
- 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
- Change the
DocumentRootvalue to where you installed WordPress earlier. Then, change the
SSLCertificateKeyFilevalues 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
- 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
- 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:
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.
- To setup SSL encryption using Let’s Encrypt, install the certbot utility with the following command.
$ sudo apt install certbot python3-certbot-apache
- 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.
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.