This knowledgebase will detail how to install WordPress on Ubuntu flavours with Apache, MariaDB and PHP8.1 (LAMP Stack). WordPress is globally recognized as the leading Content Management System (CMS). Currently, it is estimated that over 33% of websites worldwide utilize WordPress to power their platforms. The recent introduction of PHP8.1 into the Ubuntu repository further enhances WordPress compatibility, allowing it to seamlessly operate on this updated version of PHP.
Step 1: Download WordPress
SSH into your Ubuntu server and update existing software.
sudo apt update && sudo apt upgrade
Then at the command line, type in wget
followed by the direct download link to download WordPress to your Ubuntu server.
wget https://wordpress.org/latest.zip
Next, extract the archive to the /var/www/
directory with unzip.
sudo apt install unzip
sudo mkdir -p /var/www/
sudo unzip latest.zip -d /var/www/
The -d
option specifies the target directory. WordPress web files will be extracted to /var/www/wordpress
. We can rename this directory like below, so it’s easy for us to identify each directory. Replace example.com with your real domain name.
sudo mv /var/www/wordpress /var/www/example.com
Step 2: Create a Database and User for WordPress Site
Log into MariaDB shell as root with the following command.
sudo mariadb -u root
or
sudo mysql -u root
Once you are logged in, create a database for WordPress using the following command. I named it wordpress, but you can use whatever name you like such as your site name. (Don’t leave out the semicolon.)
create database wordpress;
Then enter the command below to create a database user for WordPress. The below command also grants all privileges of WordPress database to the user. Replace user and your-password with your preferred username and password.
grant all privileges on wordpress.* to user@localhost identified by 'your-password';
Flush the privileges table for the changes to take effect and then exit out of MariaDB shell.
flush privileges;
exit;
Step 3: Configure WordPress
Go to your WordPress directory.
cd /var/www/example.com/
Copy the sample configuration file and rename it to wp-config.php.
sudo cp wp-config-sample.php wp-config.php
Now edit the new config file with a command-line text editor like Nano.
sudo nano wp-config.php
Find the following lines and replace the bold texts with the database name, username and password you created in the previous step.
/** The name of the database for WordPress */
define('DB_NAME', 'database_name_here');
/** MySQL database username */
define('DB_USER', 'username_here');
/** MySQL database password */
define('DB_PASSWORD', 'password_here');
Save and close the file. To save the file in Nano text editor, press Ctrl+O, then press Enter to confirm. Next, press Ctrl+X to exit.
We also need to set the Apache user (www-data) as the owner of the WordPress site directory using the following command.
sudo chown www-data:www-data /var/www/example.com/ -R
Step 4: Create an Apache Virtual Host file for WordPress
Run the following command to create a virtual host file for your WordPress site in the /etc/apache2/sites-available/
directory.
sudo nano /etc/apache2/sites-available/example.com.conf
Put the following texts into the file. Replace the bold texts with your own domain name. Don’t forget to create A records for your domain name in your DNS manager.
<VirtualHost *:80>
ServerName www.example.com
ServerAlias example.com
DocumentRoot /var/www/example.com
#This enables .htaccess file, which is needed for WordPress Permalink to work.
<Directory "/var/www/example.com">
AllowOverride All
</Directory>
ErrorLog ${APACHE_LOG_DIR}/example.com.error.log
CustomLog ${APACHE_LOG_DIR}/example.com.access.log combined
</VirtualHost>
Save and close the file. Then test configurations.
sudo apache2ctl configtest
If you see “Syntax OK
”, then enable this virtual host.
sudo a2ensite example.com.conf
And reload Apache for the changes to take effect.
sudo systemctl reload apache2
Enter your domain name in browser address bar.
example.com
or
example.com/wp-admin/install.php
You shall see the WordPress installation wizard. Select a language. If the installation wizard isn’t displayed, then you probably need to install some PHP extensions.
sudo apt install php8.1-mbstring php8.1-xml php8.1-mysql php8.1-common php8.1-gd php8.1-bcmath php8.1-json php8.1-cli php8.1-curl php8.1-zip
Then reload Apache and the wizard should now be displayed.
sudo systemctl reload apache2
Before entering your sensitive information in the setup wizard, it’s recommended to enable HTTPS to prevent traffic hijacking.
Step 5: Enabling HTTPS
To encrypt the HTTP traffic, we can enable HTTPS by installing a free TLS certificate issued from Let’s Encrypt. Run the following command to install Let’s Encrypt client (certbot) on Ubuntu 20.04 server.
sudo apt install certbot python3-certbot-apache
And run this command to obtain and install TLS certificate.
sudo certbot --apache --agree-tos --redirect --hsts --uir --staple-ocsp --email you@example.com -d yourdomain.com,www.yourdomain.com
Where:
--apache: Use the Apache plugin.
--agree-tos: Agree to terms of service.
--redirect: Force HTTPS by 301 redirect.
--hsts: Add the Strict-Transport-Security header to every HTTP response. Forcing browser to always use TLS for the domain. Defends against SSL/TLS Stripping.
--uir: Add the “Content-Security-Policy: upgrade-insecure-requests” header to every HTTP response.
--staple-ocsp: Enables OCSP Stapling. A valid OCSP response is stapled to the certificate that the server offers during TLS.
--email: Email used for registration and recovery contact.
-d flag is followed by a list of domain names, separated by comma. You can add up to 100 domain names.
The certificate should now be obtained and automatically installed.
Now if you reload the WordPress setup wizard, you can see that HTTP is automatically redirected to HTTPS connection.
Step 6: Finish the Installation with the Setup Wizard
Create an admin account and click the Install WordPress button.
And now your new WordPress site is installed.