Install WordPress on Ubuntu 22.04 LTS

Getting started with the LAMP stack and phpMyAdmin

The fastest way to get started with WordPress is to install it on Apache server. For that, we need the LAMP stack: Linux, Apache, MySQL, and PHP. Don’t worry, even if you’re not very tech savvy or much of a programmer, installation is lighting fast. I happen to run this site on Ubuntu Server (it’s just Ubuntu without the UI), but this will work on standard Ubuntu with access to the terminal. If you simply cannot wait, visit my github page and find the WordPress script. Simply copy it to your home directory and make it executable with:

sudo chmod +x install-wordpress.sh

And then run it with:

sudo ./install-wordpress.sh

Don’t forget to read the README and change the default username and password! Otherwise, if you get hung up or are comfortable with a step-by-step install, keep reading.

Since Ubuntu uses “apt” (advanced package tool) for package management, we need to update our repository and upgrade our packages before we get started.

sudo apt update && sudo apt upgrade -y

I use -y because I know I already want to install it, but if you prefer to acknowledge the installation before proceeding, it’s perfectly fine to leave it off. Next, you’ll install Apache server and the dependencies you’re going to need.

sudo apt install apache2 ghostscript libapache2-mod-php mysql-server php php-bcmath php-curl php-imagick php-intl php-json php-mbstring php-mysql php-xml php-zip -y

Once Apache and related dependencies are finished installing, navigate to http://localhost or the IP address where you installed WordPress to verify that the default Apache page is showing.

Next we’re going to make the directory where we’ll host the wordpress files

sudo mkdir -p /var/www/html

Change the directory’s ownership to the user www-data

sudo chown www-data: /var/www/html

And then curl the zip files from the official wordpress page and unzip them into the directory we created.

curl https://wordpress.org/latest.tar.gz | sudo -u www-data tar zx -C /var/www/html

When we installed Apache, it created a default configuration in the /etc/apache2/sites-available directory. That’s where we want to create our wordpress configuration.

sudo touch /etc/apache2/sites-available/wordpress.conf

After that we’ll use the tee command to drop text into our configuration file. We’re only using port 80 for now because it’s HTTP; once we install an SSL certificate from Let’s Encrypt, it will automatically create a configuration file for HTTPS (port 443) that we can further modify later. For now, copy and paste the following into your terminal:

sudo tee /etc/apache2/sites-available/wordpress.conf << EOF
<VirtualHost *:80>
    DocumentRoot /var/www/html/wordpress
    <Directory /var/www/html/wordpress>
        Options FollowSymLinks
        AllowOverride Limit Options FileInfo
        DirectoryIndex index.php
        Require all granted
    </Directory>
    <Directory /var/www/html/wordpress/wp-content>
        Options FollowSymLinks
        Require all granted
    </Directory>
</VirtualHost>
EOF

Now let’s enable the wordpress site, disable the default Apache site, and enable rewrites.

sudo a2ensite wordpress
sudo a2dissite 000-default
sudo a2enmod rewrite

A2ensite and a2dissite stand for “Apache2 enable site” and “Apache2 disable site,” respectively; as you’ve probably guessed, a2enmod means “Apache2 enable modification.”

You can use MariaDB here fore the database that stores all your posts, but for simplicity we’re going to use MySQL (which we installed at the beginning). First you need to create the database, which we’ll call “wordpress.” Log into your MySQL server.

sudo mysql -u root 

Then create the database.

CREATE DATABASE wordpress;

Next you’ll create a user and select a password. NOTE: CHANGE THE DEFAULT USERNAME AND PASSWORD AFTER INITIAL SETUP. You can also substitute your own desired username and password in these commands to avoid doing it later.

CREATE USER admin@localhost IDENTIFIED BY 'defaultpassword';

After creating the user, we need to give the user privileges to the database.

GRANT ALL PRIVILEGES ON *.* TO admin@localhost;

Next you’ll flush privileges:

FLUSH PRIVILEGES;

Now start the MySQL database.

sudo service mysql start

You can verify that status of the server with

sudo systemctl status mysql

The WordPress files we unzipped includes a sample configuration that we’ll copy into the configuration we’ll actually use.

sudo -u www-data cp /var/www/html/wordpress/wp-config-sample.php /var/www/html/wordpress/wp-config.php

Now we need to edit the configuration file and connect our database to our WordPress installation. You can do this one of two ways: edit directly with nano, or use sed (“stream edit”) to edit the file. You can certainly use nano, but since I have the commands listed below, it’ll be faster to copy and paste. Leave database_name_here, username_here, and password_here alone, but change the db, user, and password name if you’ve changed them when you created your database user and password; otherwise, you can leave as is and change them later.

sudo -u www-data sed -i 's/database_name_here/wordpress/' /var/www/html/wordpress/wp-config.php
sudo -u www-data sed -i 's/username_here/admin/' /var/www/html/wordpress/wp-config.php
sudo -u www-data sed -i 's/password_here/defaultpassword/' /var/www/html/wordpress/wp-config.php

After you’ve done that, you need to generate WordPress keys from the official API.

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

Copy and paste the values from from the link above into the default values you find in your /var/www/html/wordpress/wp-config.php file. It’s okay: they’re random. You can hit refresh to generate new keys as many times as you want. Just don’t mix and match – copy and paste directly.

We’re almost done! We’ve got the base files installed, but who likes accessing an SQL database from the command line? Let’s install a graphical user interface (GUI) for ease of use.

sudo apt install phpmyadmin php-mbstring php-zip php-gd php-json php-curl -y

Finally we’ll enable a modification for hardening the website later, and restart Apache.

sudo phpenmod mbstring
sudo a2enmod headers
sudo systemctl restart apache2

Congratulations! You’ve successfully installed WordPress on Apache, and you’ve enabled phpMyAdmin as a GUI for your SQL database!

Navigate to http://localhost or the IP address you’ve installed WordPress on to finish the installation. Follow the prompts and create a username and password (different from your SQL database password).

Note: the latest version of phpMyAdmin doesn’t set up the root file structure for you automatically anymore (for some weird reason). That means if you try to navigate to http://localhost/phpmyadmin, WordPress thinks you’re trying to access a blog page and you get a 404 error. There are a couple ways of doing this, but the easiest way is probably to copy the /etc/phpmyadmin/apache.conf file into the /etc/apache2/sites-available folder and name the file phpmyadmin.conf. Then refresh Apache and you should have access to it.

In the next post we’ll cover basic website setup and SSL certificate installation before moving onto website hardening and web application firewalls.