In this educational article, we are going through the process of services installation for WordPress instance. In the previous article, we have set up the operating system, now we are proceeding to services setup and configuration.
ip: 167.71.34.152 (replace it with your server ip)
OS: Ubuntu 23.10 (GNU/Linux 6.5.0-9-generic x86_64)
User: bg-user
Login to the server
MacBook-Pro-ID:~ ivan$ ssh [email protected] -i bluegrid.io-edu
Install Apache web server
Update system:
bg-user@bluegrid:~$ sudo apt update
Install Apache service:
bg-user@bluegrid:~$ sudo apt install apache2
Make sure it automatically starts on the system start:
bg-user@bluegrid:~$ sudo systemctl enable apache2.service
Install DB
My choice of db engine is mariadb
so I’ll use it as an example:
bg-user@bluegrid:~$ sudo apt install mariadb-server
bg-user@bluegrid:~$ sudo service mariadb restart
Make sure Mariadb starts on system boot-up:
bg-user@bluegrid:~$ sudo systemctl enable mariadb.service
Create database user
Login to MySQL
bg-user@bluegrid:~$ sudo mariadb
Example prompt:
It’s good to have one user that will be the admin of the MariaDB server and then we’ll create one for the purpose of connecting the WordPress website with the database.
Create an admin user
Delete it if already exists:
MariaDB [(none)]> drop user if exists admin;
Create a user “admin”:
MariaDB [(none)]> create user admin identified by 'bluepassword';
Grant necessary privileges:
MariaDB [(none)]> grant all on *.* to 'admin' with grant option;
Apply changes:
MariaDB [(none)]> flush privileges;
Now let’s create a database our WordPress website will use:
MariaDB [(none)]> create database bluedb;
We can make sure it’s created by running:
MariaDB [(none)]> show databases;
We can see that “bluedb
” is there.
Create a user that will be limited to accessing this database only:
MariaDB [(none)]> create user 'bg-wp'@'localhost' identified by 'bgpassword';
Give it access to bluedb:
MariaDB [(none)]> grant all on bluedb.* to 'bg-wp'@'localhost';
Apply changes:
MariaDB [(none)]> flush privileges;
If we want to make sure this user has no access to other databases, we can log out from MariaDB and log back in with the bg-wp
user, and run the show databases command:
As we can see, just the bluedb
can be seen.
Install PHP
bg-user@bluegrid:~$ sudo apt install php libapache2-mod-php php-mysql
bg-user@bluegrid:~$ sudo service apache2 restart
File system preparations
a) Find a location for your website’s files on the server:
The directory that Apache will treat as public files storge from where the website will be served can be found in the apache2.conf and it’s usually: /var/www/html. Run this command to find out where the website files are expected by the default vhost file:
bg-user@bluegrid:~$ cat /etc/apache2/sites-enabled/000-default.conf | grep DocumentRoot
– For me, it’s /var/www/html
–
Important note: /etc/apache2/sites-enabled/000-default.conf
is a symbolic link (a shortcut) to /etc/apache2/sites-available/000-default.conf
. This is a good practice to quickly disable vhost by simply removing the link from sites-enabled
without deleting the vhost file from sites-available
.
b) Confirm that php and apache2 are working together properly:
Since we know our files are at /var/www/html
(in my case at least) we can run a quick test and see how Apache responds to our request for the php file.
- Create
index.php
file in/var/www/html with the
following contents:<?php
echo “Works!”;
?> - Test the response using this command:
bg-user@bluegrid:~$ curl http://localhost/index.php
Response:
Now delete it so we don’t leave any clutter for the website files we’ll have there:bg-user@bluegrid:~$ sudo rm /var/www/html/index.php
c) Define locations for access and error log files:
By default, apache keeps log files at /var/log/apache2/access.log
and /var/log/apache2/error.log
. What we want is to separate the access log for our website from other potential apps or websites we might have on this server:
- First, let’s see the log formats Apache has by default (we can edit if we need more custom log format):
bg-user@bluegrid:~$ grep LogFormat /etc/apache2/apache2.conf
The result should look something like this:
LogFormat "%v:%p %h %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"" vhost_combined
LogFormat "%h %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"" combined
LogFormat "%h %l %u %t \"%r\" %>s %O" common
LogFormat "%{Referer}i -> %U" referer
LogFormat "%{User-agent}i" agent
- We’ll use “
combined
” format and add it to the vhost file/etc/apache2/sites-enabled/000-default.conf
as follows:
CustomLog /var/log/apache2/bg-access.log combined
Restart Apache to apply the change:
bg-user@bluegrid:~$ sudo service apache2 restart
To test the configuration run this command:
bg-user@bluegrid:~$ sudo tail -f /var/log/apache2/bg-access.log
And open your newly installed WordPress website in your browser. The result should be multiple lines on the screen while the tail command is running:
d) Define index.php
as a DirectoryIndex so that by default (without specifying) server will serve index.php
instead of index.html
(which is the default setting):
Open /etc/apache2/sites-enabled/000-default.conf
and add these lines in the vhost file:
<Directory />
DirectoryIndex index.php
</Directory>
Finally
Restart Apache to apply the change:
bg-user@bluegrid:~$ sudo service apache2 restart
Now that we have all the necessary services installed and configured, we can proceed to install WordPress.