Installing WordPress on Ubuntu 22.04 remote instance

This post is describing how to install WordPress on a remote instance linux machine end to end, in our example we are using a Ubuntu instance hosted in https://www.oracle.com/cloud, which is offering always-free linux instances on their cloud.

Creating Ubuntu instance and SSH access

You can follow this tutorial https://docs.oracle.com/en-us/iaas/Content/GSG/Reference/overviewworkflow.htm to create a free instance on Oracle Cloud, and once you have your Ubuntu Instance running and have downloaded the private key, you will be able to access the server through SSH.

To avoid “permissions too open” error, you will need to change the key permissions and make the key read-writable only by you

sudo chmod 600 ssh-private-key.key

You will also need to find the public ip assigned to your linux instance before trying to connect

ssh -i ssh-private-key.key ubuntu@your-public-ip

You should be able to access the instance after successful connection

Installing Apache2 and PHP dependencies

sudo apt update && sudo apt upgrade

After updating the Ubuntu packages you can install the server PHP dependencies.

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

Installing Mysql and creating WordPress database

sudo apt install mysql-server

You can run this command to certify that Mysql is up and running

sudo systemctl status mysql.service

If service is not started yet, you can start it manually

sudo systemctl start mysql.service

Open Mysql and change root password

sudo mysql
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'new-password';
exit

Now you should be able to log in using password, you can type the command bellow and insert your password

mysql -u root -p

To secure your Mysql service, it’s recommended to run the security script (out ot Mysql terminal)

sudo mysql_secure_installation

Access Mysql terminal again and create WordPress database

create database wordpress;

Create an user for this database

create user wuser@localhost identified by 'your-password';

Grant privileges to the new user created

grant select, insert, update, delete, create, drop, alter on wordpress.* to wordpress@localhost;
FLUSH PRIVILEGES;
quit

Installing WordPress

To install WordPress we need to create a folder which will be accessible by Apache2

sudo mkdir -p /var/www/html/public_html/blog

www-data is the user which Apache2 will be using

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

Run the bellow curl command to download and unzip WordPress

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

You can also create a Git repository from blog folder, doing that you won’t lose any work from now on, you can find more info in https://git-scm.com/docs/git-init

Create a copy and rename wp-config-sample.php file

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

Now you need to edit this file on terminal and replace the placeholders with the WordPress database info recently created

sudo -u www-data nano /var/www/html/public_html/blog/wordpress/wp-config.php
define( 'DB_NAME', 'database_name_here' );

/** Database username */
define( 'DB_USER', 'username_here' );

/** Database password */
define( 'DB_PASSWORD', 'password_here' );

Its also recommend to replace the authentication unique keys and salts, you can hit this api: https://api.wordpress.org/secret-key/1.1/salt/ to get the keys and replace in the same file

define( 'AUTH_KEY',         'put your unique phrase here' );
define( 'SECURE_AUTH_KEY',  'put your unique phrase here' );
define( 'LOGGED_IN_KEY',    'put your unique phrase here' );
define( 'NONCE_KEY',        'put your unique phrase here' );
define( 'AUTH_SALT',        'put your unique phrase here' );
define( 'SECURE_AUTH_SALT', 'put your unique phrase here' );

Save and exit (CRTL+X and then Y)

Installing and configuring Apache2 HTTP Server

sudo apt install apache2
sudo service apache2 status

After installing and hit the status command you should see the service running

Make Ubuntu’s firewall start accepting connections from HTTP

sudo iptables -I INPUT 6 -m state --state NEW -p tcp --dport 80 -j ACCEPT
sudo netfilter-persistent save

Now you should be able to access the default Apache2 page through you browser using the instance public ip like http://public-ip

Enabling PHP from user directory pages

sudo nano /etc/apache2/mods-enabled/php8.1.conf

Comment out these lines

<IfModule mod_php8.1.c>
<FilesMatch "\.ph(p3?|tml)$">
SetHandler application/x-httpd-php
</FilesMatch>
<FilesMatch "\.phps$">
SetHandler application/x-httpd-php-source
</FilesMatch>
# To re-enable php in user directories comment the following lines
# (from <IfModule ...> to </IfModule>.) Do NOT set it to On as it
# prevents .htaccess files from disabling it.
# <IfModule mod_userdir.c>
# <Directory /home/*/public_html>
# php_admin_value engine Off
# </Directory>
#</IfModule>
</IfModule>

Save and exit (CRTL+X and then Y)

Now that we have Apache2 up, running and is also HTTP acessible, we need to create the WordPress configuration in Apache2

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

Add this content into the newly created file

<VirtualHost *:80>
    DocumentRoot /var/www/html/public_html/blog/wordpress
    <Directory /var/www/html/public_html/blog/wordpress>
        Options FollowSymLinks
        AllowOverride Limit Options FileInfo
        DirectoryIndex index.php
        Require all granted
    </Directory>
    <Directory /var/www/html/public_html/blog/wordpress/wp-content>
        Options FollowSymLinks
        Require all granted
    </Directory>
</VirtualHost>

Save and exit (CRTL+X and then Y)

Enable the site and URL rewriting with

sudo a2ensite wordpress
sudo a2enmod rewrite

Disable the default site that we’ve seen previously and reloading the Apache2 server to reflect all the changes

sudo a2dissite 000-default
sudo service apache2 reload

Configuring WordPress

Now that you have everything in place, when opening http://public-ip in your browser, you will be redirected to WordPress installation page, and you just need to proceed with the required information to have a free WordPress website set up.