WordPress is an extremely popular content management system (CMS) for websites. Its popularity and ubiquity really cannot be overstated, as it powers a staggering 35% of websites. It’s an easy way to get a website online, and it offers a lot in the way of customization.
Ubuntu 20.04 Focal Fossa and Nginx are the perfect combination to run your WordPress site. Using these utilities will give you a very powerful, efficient, and stable website. Best of all, all this software is completely free and the setup isn’t very difficult. Follow along with our steps below to see how to get your WordPress site up and running on Ubuntu 20.04 with Nginx.
If you’re more familiar with or happen to prefer Apache over Nginx, we’ve written a separate guide for Ubuntu 20.04 WordPress installation on Apache.
In this tutorial you will learn:
- How to install and configure Nginx
- How to install and configure MariaDB for MySQL
- How to setup a MySQL database for WordPress
- How to download and install WordPress
- How to configure SSL for your WordPress site
Category | Requirements, Conventions or Software Version Used |
---|---|
System | Installed Ubuntu 20.04 or upgraded Ubuntu 20.04 Focal Fossa |
Software | WordPress, Nginx, 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 Nginx, PHP, and MySQL
Before installing WordPress, our Ubuntu 20.04 system will need three main components to run it: Nginx, PHP, and MySQL. Nginx is for our web server, PHP is to display dynamic content, and MariaDB (an open source fork of MySQL) is for our database. You can install these packages by opening a terminal and typing the following two commands:
$ sudo apt update $ sudo apt install nginx mariadb-server mariadb-client php-fpm php-mysql
Configure MySQL
MySQL requires a little bit of setting up before we can start creating a database. Let’s first run through the initial security setup. Type the following command in terminal:
$ sudo mysql_secure_installation
You’ll be asked to set a root password for MySQL, and then a few security question. You can answer y
(yes) to all the questions, and then setup will complete.
We cover more in depth MySQL configuration in our MySQL installation on Ubuntu 20.04 article, though the configuration above is really all you’ll need to do.
Create a database for WordPress
Your WordPress site will need one database to store all the user information, post content, etc. Follow these steps to get your database and MySQL user ready:
- Start MySQL as the root user:
$ sudo mysql
- Create a new database for WordPress:
MariaDB [(none)]> CREATE DATABASE wordpress_db;
- Next, create a new database user for WordPress. The
my_password
text below should be replaced with your desired (secure) password:MariaDB [(none)]> CREATE USER 'wordpress_user'@'localhost' IDENTIFIED BY 'my_password';
- Now, we need to give our WordPress user full permissions on the WordPress database:
MariaDB [(none)]> GRANT ALL PRIVILEGES ON wordpress_db.* to wordpress_user@'localhost';
- Finally, save the changes we’ve made to user permissions and exit the database:
MariaDB [(none)]> FLUSH PRIVILEGES; MariaDB [(none)]> exit
Configure Nginx
Before we can download WordPress, we’ll need to properly configure Nginx to host our website. We’re going to call our website “wordpress” within all of Nginx’s configuration, but you can choose another name if you’d like.
First, create a configuration file under the /etc/nginx/sites-available
directory using nano or your favorite text editor:
$ sudo nano /etc/nginx/sites-available/wordpress
You can paste the following content into your newly created file, which is a pretty standard Nginx configuration.
server {
listen 80;
listen [::]:80;
root /var/www/wordpress;
index index.php;
server_name 127.0.0.1;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
}
}
There are a few lines in here that you may need to change. Line 9 should contain your domain name in place of 127.0.0.1
, unless you don’t have a domain name. Line 17 should be updated with the version number of your installed PHP. To check your PHP version, execute the php --version
command in terminal.
Once your configuration edits are made, you can save your changes to this file and close it. The last steps in the Nginx setup are to delete the default site, enable your website, and restart Nginx to make the changes take effect:
$ sudo rm /etc/nginx/sites-enabled/default $ sudo ln -s /etc/nginx/sites-available/wordpress /etc/nginx/sites-enabled/wordpress $ sudo systemctl restart nginx
Download and install WordPress
Now that all of the prerequisites have been satisfied, we can finally move on to installing WordPress itself.
- First, download the latest version of WordPress using
wget
:$ wget -O /tmp/wordpress.tar.gz https://wordpress.org/latest.tar.gz
- Unzip the downloaded WordPress archive into the site directory:
$ sudo tar -xzvf /tmp/wordpress.tar.gz -C /var/www
- Change the ownership of the site directory:
$ sudo chown -R www-data.www-data /var/www/wordpress
- Open your internet browser and navigate to
127.0.0.1
or your fully qualified domain name. You’ll be greeted by the WordPress setup wizard. Click the “Let’s go” button to get started with the configuration. - The next screen asks us for information about our database configuration. Enter the values you set earlier, and then click “Submit.” The last two boxes (database host and table prefix) can be left at their default values.
- WordPress should give confirmation that it’s able to communicate with the MySQL database. Click “Run the installation” to begin installing WordPress.
- Now you’ll be required to fill out some general information about your new website: its name, admin username, password, etc. Fill this out and then click “install WordPress” at the bottom.
- Installation should be complete and you can login to your new website to start creating content!
You can always access the admin panel of WordPress by navigating to http://127.0.0.1/wp-admin
(or replacing 127.0.0.1
with your fully qualified domain name).
The changes you make in the admin panel will be reflected on the website:
Optional SSL configuration
Before wrapping up, we’ll also show you how to enable SSL on your new WordPress site. This is purely optional, since your site will function perfectly fine without it, but it does offer additional security and give users a warm, fuzzy feeling seeing the padlock next to your domain name in the URL bar of their browser.
- Start by generating a new self-signed certificate with the following command and answering the few questions you’re prompted with. For the “common name,” either enter your website’s IP address or fully qualified domain name:
$ sudo openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout /etc/ssl/private/nginx-selfsigned.key -out /etc/ssl/certs/nginx-selfsigned.crt
- Next, create a new SSL configuration file in the following location:
$ sudo nano /etc/nginx/snippets/self-signed.conf
- In this file, enter the following two lines, before saving changes and exiting the file:
ssl_certificate /etc/ssl/certs/nginx-selfsigned.crt; ssl_certificate_key /etc/ssl/private/nginx-selfsigned.key;
- Then, we need to create another configuration snippet:$ sudo nano /etc/nginx/snippets/ssl-params.conf
- Enter the following contents into this file, then save and exit it. Note that since we are using a self-signed certificate, Line 9 and Line 10, which relate to SSL stapling, have been commented out. If you’re not using a self-signed certificate, uncomment those two lines.
ssl_protocols TLSv1.2; ssl_prefer_server_ciphers on; ssl_dhparam /etc/ssl/certs/dhparam.pem; ssl_ciphers ECDHE-RSA-AES256-GCM-SHA512:DHE-RSA-AES256-GCM-SHA512:ECDHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES256-> ssl_ecdh_curve secp384r1; ssl_session_timeout 10m; ssl_session_cache shared:SSL:10m; ssl_session_tickets off; # ssl_stapling on; # ssl_stapling_verify on; resolver 8.8.8.8 8.8.4.4 valid=300s; resolver_timeout 5s; add_header X-Frame-Options DENY; add_header X-Content-Type-Options nosniff; add_header X-XSS-Protection "1; mode=block";
- Next, run the following command to generate the
dhparam.pem
file:$ sudo openssl dhparam -out /etc/ssl/certs/dhparam.pem 2048
- Now that the SSL configuration is complete, we need to configure Nginx to use SSL. Open up the Nginx configuration file we created for our WordPress site earlier:
$ sudo nano /etc/nginx/sites-available/wordpress
- Within the
server
block, we need to add the following four lines:listen 443 ssl; listen [::]:443 ssl; include snippets/self-signed.conf; include snippets/ssl-params.conf;
- Save your changes to that file before closing it, and then restart Nginx:
$ sudo systemctl restart nginx
Your WordPress site will now be capable of using SSL encryption:
Conclusion
WordPress is used by millions, from fortune 500 companies to small time bloggers. It uses top of the line components and runs wonderfully on Ubuntu 20.04 Focal Fossa – a combo that’s truly hard to beat.
In this article, we saw how to install and configure Nginx, PHP, and MySQL in order to run a WordPress website. Although the configuration takes a little time, it’s well worth it. Nginx is faster than other web servers, and WordPress gives you a slick website right out of the box.