Introduction
This tutorial will guide you to a self-hosted WordPress website on an Ubuntu server.
No complicated prerequisites are necessary, information presented in a self-contained manner.
Basic Linux knowledge is assumed.
Prerequisites:
-
a new Ubuntu server with root access
-
a domain pointing to your server (
A
DNS record created with an IP address of your server)- your DNS hosting provider should have a guide how to do that
DNS record should be as follows, where
10.0.0.1
is an IP of your server.Type Name Value A @ 10.0.0.1 -
SMTP ports (25 and 587) should not be blocked by your hosting provider. If they are blocked, try to find instructions to unblock them.
Example terminology:
These are just examples, replace them with your own values.
-
Public IP address of your server:
10.0.0.1
-
Domain of your WordPress website:
example.com
-
New user that will be created on the server:
holu
Step 1 - Configuring the server
Step 1.1 - Checking DNS
To proceed you need to check that your domain points to the server. We will do it by using ping command.
Execute the following command on your local computer (replace example.com
with your domain).
ping example.com
You will get the output similar to this:
PING example.com (10.0.0.1) 56(84) bytes of data.
64 bytes from 10.0.0.1: icmp_seq=1 ttl=64 time=0.017 ms
64 bytes from 10.0.0.1: icmp_seq=2 ttl=64 time=0.035 ms
64 bytes from 10.0.0.1: icmp_seq=3 ttl=64 time=0.065 ms
--- example.com ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2049ms
rtt min/avg/max/mdev = 0.017/0.039/0.065/0.019 ms
In place of 10.0.0.1
you should see the IP address of your server.
If it's not the case, you will need to wait some time until DNS records will be updated.
In the meantime, you can follow the tutorial up to Step 3.4.
Step 1.2 - Updating the system
You should update your system.
-
Check for updates:
apt update
-
Perform the update, press y to agree with updates if necessary.
apt upgrade
Step 1.3 - Creating a user
You should create a regular system user to use with your WordPress instance.
Replace holu
with a desired username.
adduser holu
You will be prompted for a password. Everything else can be skipped by pressing Enter.
Note!
It's recommended to use an SSH key for authentication instead of a password. Follow the Setting up an SSH key tutorial to learn how to do it.
Now you need to add the user to the sudo group, to be able to run commands with the superuser privileges.
usermod -aG sudo holu
Note!
You can optionally change the two configuration options. First the root user can be disabled. After you have added the new user to the sudo group, you can use it to connect to your server over SSH and perform administrative tasks. Second if you have configured an SSH key for the new user, you can disable password authentication in the OpenSSH server. How to change both configuration options is explained here.
From now on we will use the newly created user, run the following command:
- replace
holu
with your username.
su holu
Your shell prompt will be changed to something like:
holu@ubuntu:~$
You can prefix your commands with sudo
to run them as a root user. You will be asked for your password that is chosen in the adduser
command above.
Step 1.4 - Configuring a time zone
You need to change the server time zone to your time zone. The web server logs will be written using this time zone.
To do that use the command:
sudo dpkg-reconfigure tzdata
Use Up and Down arrows to navigate and Enter to make a choice.
Step 1.5 - Creating a directory for WordPress
We need to create the directory where the WordPress website will be installed, for example:
sudo mkdir /var/www/wordpress
Give the permissions to the regular user:
- replace
holu
with your username
sudo chown holu:holu /var/www/wordpress
Step 2 - Installing required software
Step 2.1 - Using a package manager
Before we can proceed with WordPress installation, we need to install the required software packages.
sudo apt update && sudo apt install nginx mariadb-server mariadb-client \
php-fpm php-cli php-zip php-xml php-mysql php-json php-curl php-imagick \
certbot python3-certbot-nginx
Here is a short description of each package and why it's required.
Package(s) | Description |
---|---|
nginx |
The web server that accepts HTTP requests from a browser. |
mariadb-server |
The database server, used to store and query the WordPress database. |
mariadb-client |
The database client which is used to connect to the database server from the command line. |
php-fpm |
The nginx web server will pass php files to FPM for execution. It will run WordPress core, themes and plugins. |
php-cli |
The PHP interpreter which is used to execute PHP scripts from the command line. Used to run WP-CLI utility which is described below. |
zip, xml, mysql,json, curl, imagick |
The PHP extensions which are required to run WordPress. |
certbot, python3-certbot-nginx |
Packages for HTTPS support. |
Step 2.2 - Checking the PHP version installed
We will need the PHP version in the future steps. Run the command:
php -v
You should see output similar to this:
PHP 7.4.3 (cli) (built: Oct 6 2020 15:47:56) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
with Zend OPcache v7.4.3, Copyright (c), by Zend Technologies
only the first two numbers relevant here (major and minor version), 7.4
in my case.
Step 2.3 - Installing WP-CLI
WP-CLI provides many useful functions to work with WordPress from command line.
-
You need to execute the following commands to install WP-CLI:
cd curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar chmod +x wp-cli.phar sudo mv wp-cli.phar /usr/local/bin/wp
-
The
wp
command is now available in the shell. -
WP-CLI can be updated by repeating step 1 if necessary.
Step 3 - Configuring required software
Step 3.1 - Configuring PHP
-
Open the configuration file with a text editor, replace
7.4
with your PHP version from Step 2.2:sudo nano /etc/php/7.4/fpm/pool.d/www.conf
-
Find these lines:
user = www-data group = www-data
-
Replace
www-data
with the username from Step 1.3:user = holu group = holu
-
Press Ctrl+S to save and Ctrl+X to exit
-
We need to reload the configuration file to apply our changes, replace
7.4
with your PHP version.sudo systemctl stop php7.4-fpm sudo systemctl start php7.4-fpm
Step 3.2 - Configuring nginx
-
Create the configuration file:
sudo nano /etc/nginx/sites-available/wordpress
-
Paste this content:
server { listen 80; server_name example.com; root /var/www/wordpress; index index.php; location / { try_files $uri $uri/ /index.php?$args; } location ~ \.php$ { include snippets/fastcgi-php.conf; fastcgi_pass unix:/run/php/php7.4-fpm.sock; } }
-
Edit the content:
- replace
example.com
with your domain - replace
/var/www/wordpress
with the directory created in Step 1.5 - on a line
fastcgi_pass unix:/run/php/php7.4-fpm.sock;
replace7.4
with your PHP version
- replace
-
Save the file and close it
-
Enable the configuration file:
sudo ln -s /etc/nginx/sites-available/wordpress \ /etc/nginx/sites-enabled/wordpress
-
Reload:
sudo systemctl reload nginx
Step 3.3 - Configuring MariaDB
-
Type
sudo mysql
to open the database shell as a root user. -
Create the database account for the system user, replace
holu
with your username from Step 1.3.CREATE USER 'holu'@'localhost' IDENTIFIED VIA unix_socket;
-
You need to choose the database name for your WordPress website. Let's use
wordpress_db
as an example. The user created in the previous step needs permissions to work with a database, the database will be created later on in Step 4:GRANT ALL ON wordpress_db.* TO 'holu'@'localhost';
-
Type
\q
to close the shell.
If you want to work with your database, open it in the mysql
shell with mysql wordpress_db
, without sudo
prefix.
Step 3.4 - Configuring HTTPS support
-
Execute the following command, replace
example.com
with your domain:sudo certbot -d example.com
-
Provide your email and agree with terms of service
-
When prompted to redirect requests or not you should type 2 to redirect them and press Enter.
Step 3.5 - Configuring the email notifications
You need to install and configure the mail server to receive the email notifications from your WordPress website.
Prerequisites:
-
You need to create the following DNS record for your WordPress domain.
Type Name Value TXT @ v=spf1 a -all This basically says that only the server where your WordPress website is hosted is allowed to send emails on behalf of your domain. Many popular email services will reject emails without this configuration.
To install the postfix
mail server run the following command:
sudo apt update && sudo apt install postfix
You will be asked to choose the mail server configuration, use arrows to select "Internet Site" and press Enter.
On the next screen you need to configure the "mail name". Use the domain name of your WordPress website for which you have configured the TXT
record above.
Now you need to edit the postfix
configuration file. Open it in the text editor:
sudo nano /etc/postfix/main.cf
Make the following adjustments:
-
Change
inet_protocols
configuration option toinet_protocols = ipv4
.This is needed because Gmail will reject the emails if you send them over IPv6.
-
Change
inet_interfaces
configuration option toinet_interfaces = loopback-only
.This will disable incoming emails. WordPress website needs only outgoing emails to be working to send you notifications.
-
Change
myhostname
configuration option tomyhostname = example.com
.Where
example.com
is a domain of your WordPress website for which you have configured theTXT
record above.
Save the file and restart postfix
:
sudo systemctl stop postfix
sudo systemctl start postfix
If you have problems with receiving emails from your website, try to take a look at the /var/log/mail.log
log file. Additionally take a look in your email spam folder.
Step 4 - Installing WordPress
-
Change the current working directory to the WordPress directory, created in Step 1.5:
cd /var/www/wordpress
-
Download WordPress:
wp core download
-
This command will create the
wp-config.php
file where the WordPress configuration is stored.- replace
wordpress_db
with a database name from Step 3.3 - replace
holu
with the user created in Step 1.3
wp config create --dbname=wordpress_db --dbuser=holu --prompt=
- replace
-
Create the database:
wp db create
-
Install WordPress:
- replace
example.com
with your domain - replace
holu
with a desired WordPress username, this can be different from your system username - replace
holu@example.com
with your email
wp core install --url=https://example.com --title="WordPress Blog" \ --admin_user=holu --admin_email=holu@example.com
- replace
-
WP-CLI will generate and output your WordPress password to the terminal. Additionally, you will receive email notification that a new WordPress website has been created.
-
Go to
https://example.com/wp-admin
to log in to your WordPress website. -
Finally you should see the WordPress Admin Dashboard:
Conclusion
Congratulations! You should have a working WordPress website.
I hope that the information presented was useful to you.