How to install WordPress with Nginx on Ubuntu 18.04 – WordPress is still currently the most popular open source blogging platform and CMS platform in the world. As we know that WordPress powers more than a quarter of the world’s websites.
WordPress is based on PHP and MySQL and many features can be extended with plugins and to enhance the appearance of your WordPress website, there are also millions of free and premium themes available. WordPress is the simplest web tool to create your online store, website or blog.
In today’s ubuntu linux tutorial, I will show you how to install WordPress on Ubuntu 18.04 OS with Nginx web server. It’s a fairly straightforward process that takes less than ten minutes to complete.
I’m here to use LEMP Stack with Nginx as web server, free SSL certificate, latest PHP 7.2, and MySQL / MariaDB as database server.
Requirements
Make sure you meet the following requirements before proceeding with the tutorial how to install WordPress on Ubuntu 18.04:
- You have a domain name that points to your server’s public IP. As usual I will use
example.com
. - You are logged into the Ubuntu OS as a user with sudo privileges. Read: How to Create a Sudo User and a Sudo Group on Ubuntu
- You’ve installed Nginx server by following guide here.
- An SSL certificate is installed for your domain. You can install a free Let’s Encrypt SSL certificate by following guide here.
Update package indexes and system packages to the latest versions:
$ sudo apt update
$ sudo apt upgrade
How to Create a MySQL database
WordPress uses a MySQL database to store all of its data. My first step is to create a MySQL database, MySQL user account, and provide access to the database.
If you don’t have MySQL or MariaDB installed on your Ubuntu server you can install it by following one of the guides below:
Login to the MySQL shell by typing the following command and enter the password when prompted:
$ mysql -u root -p
From within the MySQL shell, run the following SQL statement to create a named database wordpress
, a user named wordpressuser
and to give users all the necessary permissions:
mysql> CREATE DATABASE wordpress CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
mysql> GRANT ALL ON wordpress.* TO 'wordpressuser'@'localhost' IDENTIFIED BY 'change-with-strong-password';
mysql> FLUSH PRIVILEGES;
mysql> EXIT;
After running the above command, you will have a new MySQL database and user account, which will be used by your WordPress installation.
How to install PHP
PHP 7.2 which is the default PHP version in Ubuntu 18.04 is fully supported and recommended for WordPress.
To How to install PHP and all necessary PHP extensions, run the following command:
$ sudo apt install php7.2-cli php7.2-fpm php7.2-mysql php7.2-json php7.2-opcache php7.2-mbstring php7.2-xml php7.2-gd php7.2-curl
I installed PHP-FPM because I will be using Nginx as the web server.
The PHP-FPM service will automatically start or start after the installation process is complete.
Download WordPress
Before downloading the WordPress files, however, first create a directory that will hold my WordPress files:
$ sudo mkdir -p /var/www/html/example.com
My next step is to download the latest version of WordPress from the page download WordPress using commands wget
following:
$ cd /tmp
$ wget https://wordpress.org/latest.tar.gz
Once the WordPress package download is complete, extract WordPress and move the extracted files to the document root domain directory:
$ tar xf latest.tar.gz
$ sudo mv /tmp/wordpress/* /var/www/html/example.com/
Finally we need to set the correct permissions so that the web server can have full access to the site files and directories.
Because Nginx and PHP run as www-data
users and groups, to set proper ownership, run the following chown command:
$ sudo chown -R www-data: /var/www/html/example.com
Configuring the Nginx Web Server
Now, you must have Nginx installed with SSL certificate on your system, if not please check this tutorial in the link I have provided in the requirements above.
To create a new server block for my WordPress example, I’m going to use Nginx recipe from the official Nginx site.
Open your text editor and create the following file:
/etc/nginx/sites-available/example.com
# Redirect HTTP -> HTTPS
server {
listen 80;
server_name www.example.com example.com;
include snippets/letsencrypt.conf;
return 301 https://example.com$request_uri;
}
# Redirect WWW -> NON WWW
server {
listen 443 ssl http2;
server_name www.example.com;
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
ssl_trusted_certificate /etc/letsencrypt/live/example.com/chain.pem;
include snippets/ssl.conf;
return 301 https://example.com$request_uri;
}
server {
listen 443 ssl http2;
server_name example.com;
root /var/www/html/example.com;
index index.php;
# SSL parameters
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
ssl_trusted_certificate /etc/letsencrypt/live/example.com/chain.pem;
include snippets/ssl.conf;
include snippets/letsencrypt.conf;
# log files
access_log /var/log/nginx/example.com.access.log;
error_log /var/log/nginx/example.com.error.log;
location = /favicon.ico {
log_not_found off;
access_log off;
}
location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ .php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.2-fpm.sock;
}
location ~* .(js|css|png|jpg|jpeg|gif|ico|svg)$ {
expires max;
log_not_found off;
}
}
Don’t forget to replace example.com
with your WordPress domain and set the correct path to the SSL certificate file. The snippets used in this configuration were created in this guide.
Activate server blocks by creating a symbolic link to a directory site-enabled
:
$ sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
Before restarting the Nginx services, run a test to ensure that there are no syntax errors:
$ sudo nginx -t
If there are no errors, the result will look like this:
Output
$ nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
$ nginx: configuration file /etc/nginx/nginx.conf test is successful
and you canrestart Nginx by typing:
$ sudo systemctl restart nginx
Finalizing the WordPress installation
Now that WordPress is downloaded and the web server configuration is complete, we can complete the installation via the web interface.
Open web browser favorites, type your domain and a screen similar to the following will appear:
Select the language you want to use and click the button Continue.
Next, you will see the following information page, click on the button Let’s go!.
On the next screen, the setup process will ask you to enter connection details to your database. Enter the user details and MySQL database that you have previously created with the click of a button Submit.
Start the installation with the click of a button Run the Installation.
In the next step, you will need to enter a name for your WordPress site and choose a username (for security purposes don’t use “admin” as user but make it more complicated).
The installation will automatically generate a strong password for you with a random combination. Don’t forget to save this password. If you feel the password generated is too complex, you can also set the password yourself.
Enter your email address and choose whether you want to prevent search engines from indexing the site (unchecked).
Click the button Install WordPress and once the installation is complete, you will be taken to a page informing you that WordPress has finished installing. To access your WordPress login form, click the button Log in.
Enter your username and password and click the button Log in.
After you log in, you will be directed to the WordPress admin dashboard.
From here you can start customizing your WordPress installation by installing new WordPress themes and plugins. You can search for a variety complete WordPress tutorial on this blog because this blog has the greatest guide for WordPress.
Conclusion
Congratulations, you have successfully installed WordPress with Nginx server on your Ubuntu 18.04. Furthermore, you can learn about WordPress and this blog centerclick will guide you in learning WordPress from beginner to professional, you only need to find what you need on this blog.