Wordpress

Installing and setting up your own Wordpress site on Apache

published on

Whilst I prefer Joomla to Wordpress there are many more people using Wordpress online and so it only seems right to add an article explaining how to install the latest version of Wordpress (version 6.0 at the time of writing) using PHP 8.1, MariaDB and Redis cache.

Start by installing PHP 8.1

The first thing I did when setting up my server for the Wordpress installation is to install PHP 8.1. This speeds up executing PHP scripts as it includes a JIT (or just in time) compiler which PHP 7.4 and below didn't have.

To install PHP, and a few other tools, that might be useful later enter the following commands:

sudo add-apt-repository -y ppa:ondrej/php
sudo apt update
sudo apt install php8.1 php8.1-{cli, fpm, mysql, gd, soap, mbstring, bcmath, common, xml, curl, imagick, bz2, intl, zip, ldap, opcache, exif, imagick, gd, mcrypt, ssh2}
sudo apt install zip unzip ghostscript imagemagick

Now edit the PHP pool configuration file with sudo nano /etc/php/8.1/fpm/pool.d/www.conf and update statements in the file to look like the below. You may need to uncomment (remove the ;) on some lines.

listen = /run/php/php8.1-fpm.sock
listen.owner = www-data
listen.mode = 0660

Once done edit the php.ini file by running sudo nano /etc/php/8.1/fpm/php.ini and updating the following lines to look like the ones below. Make sure to add any lines below that you can't find in the file.

expose_php = off
max_execution_time = 30
max_input_time = 60
memory_limit = 512M
opcache.enable=1
opcache.jit_buffer_size=100M
opcache.jit=1255
opcache.memory_consumption=128
opcache.max_accelerated_files=10000
opcache.revalidate_freq=200

Now restart PHP 8.1 by running sudo systemctl restart php8.1-fpm.

Now install Redis cache

Installing the Redis cache will help to speed up your site by storing some of the data in memory rather than it needing to be read from disk each time.

Enter the following commands to install it

sudo add-apt-repository ppa:redislabs/redis
sudo apt-get update
sudo apt-get install redis-server php-redis
sudo systemctl enable redis-server.service

Make a few changes to the configuration by running sudo nano /etc/redis/redis.confand editing lines similar to the below to read as follows

bind 127.0.0.1 ::1
supervised systemd
maxmemory 256mb
maxmemory-policy allkeys-lru

Lastly, reload the Redis service

sudo systemctl restart redis.service

Install and setup the MariaDB database

I like to install extra software that makes it easy for me to backup my MariaDB databases. Running the following command will achieve this

sudo apt-get install mariadb-server mariadb-backup

Once installed it is important to run sudo mysql_secure_installation to secure the installation. Remember that, initially, their is no MariaDB root password!

Lastly I run sudo nano /etc/mysql/mariadb.conf.d/50-server.cnf to make two changes to the configuration file

bind-address = 127.0.0.1
local-infile = 0

then I restart the service with sudo systemctl restart mariadb.service.

Now I can set up a new database for Wordpress to use. This can be done by running sudo mysql -u root -p and entering the root password you created just above.

Now enter the following lines, making sure to substitute your own database name, user name and password, before entering exit to return to the command prompt.

CREATE DATABASE [wordpress_db] charset=utf8mb4;
CREATE USER 'wordpress_user'@'localhost' IDENTIFIED BY 'my_password';
GRANT ALL PRIVILEGES ON wordpress_db.* to 'wordpress_user'@'localhost';
FLUSH PRIVILEGES;

Installing the Apache web server

In this article we are using the Apache web server. To do so run the following commands. These presume that you are using the Uncomplicated Firewall (UFW). If not then substitute the relevant commands with the ones for the firewall that you are using.

sudo add-apt-repository ppa:ondrej/apache2
sudo apt-get update
sudo apt-get install apache2 apache2-utils
sudo ufw allow 80
sudo ufw allow 443
sudo ufw enable
sudo a2enmod mime http2 rewrite deflate expires headers ssl
sudo apt-get install libapache2-mod-fcgid
sudo a2enmod proxy_fcgi setenvif
sudo a2enconf php8.1-fpm
sudo systemctl restart php8.1-fpm
sudo a2dissite 000-default.conf
sudo systemctl restart apache2

These instructions have also disabled the default site.

Downloading and installing the latest version of Wordpress

The commands you will need to run to download and save the Wordpress files in the correct place can be found below. Make sure to substitute [domain] with your own domain name.

cd ~
wget -c http://wordpress.org/latest.tar.gz
tar -xzvf latest.tar.gz
sudo mkdir /var/www/[domain]
sudo cp -R wordpress/* /var/www/[domain]

Now, edit the sample configuration file to add memory limits by running sudo nano /var/www/[domain]/wp-config-sample.php and adding the following two lines near the top of the file.

define('WP_MEMORY_LIMIT', '512M');
define( 'WP_MAX_MEMORY_LIMIT', '512M' );

Finally, set up ownership and file permssions with

sudo chown -R www-data:www-data /var/www/[domain]
sudo find /var/www/[domain]/ -type d -exec chmod 755 {} \;
sudo find /var/www/[domain]/ -type f -exec chmod 644 {} \;

Create Apache's virtual host

Run sudo nano /etc/apache2/sites-available/[domain].conf, making sure to substitute [domain] for your own domain, and add the following lines to the file

<VirtualHost *:80>
   ServerName mysite.com
   ServerAdmin webmaster@localhost
   DocumentRoot /var/www/[domain]/test
   ErrorLog ${APACHE_LOG_DIR}/error-[domain].log
   CustomLog ${APACHE_LOG_DIR}/access-[domain].log combined
     <FilesMatch "\.php$">
       SetHandler "proxy:unix:/run/php/php8.1-fpm.sock|fcgi://localhost"
     </FilesMatch>
</VirtualHost>

and lastly run the following two commands

sudo a2ensite [domain].conf
sudo systemctl reload apache2

Make your site secure with a certificate

Run the following commands to provide your site with a free SSL / TLS certificate. This will make sure that it can be accessed using https and should set up the necessary redirects in case anybody tries to visit the non - secure version.

sudo apt install snapd
sudo snap install core; sudo snap refresh core
sudo snap install --classic certbot
sudo ln -s /snap/bin/certbot /usr/bin/certbot
sudo certbot -d [domain] --apache

Setup Wordpress

The final step is to visit your domain name in your browser of choice and click through the setup screens you see displayed. Remember to enter the same database details that you used earlier in order that Wordpress can connect to your database.

Once you have logged in to the Wordpress backend make sure to install the Redis Obect Cache plugin and activate it.

Final thoughts

This article covers the basics in getting a fast, performant, version of Wordpress installed. There are other things you can do to develop this further.

Whilst this is a good start to a fast website remember that the server can only help you so much. If you have a slow site then the server will not be able to solve this for you. When creating your site try to keep it as lean as possible.