How to Install WordPress with a LEMP Stack on Ubuntu 18.04

In this article we will guide you through the steps of installing the latest version of WordPress on an Ubuntu 18.04 VPS, including the installation of Nginx web server, PHP and MySQL database server.

WordPress is a free and open-source CMS based on the PHP and MySQL programming languages. Used by millions of people, it is the world’s most popular content management systems (or CMS for short). With features such as in-depth theming, thousands of plug-ins, and a huge community, WordPress is probably the most user-friendly CMS you can choose. The installation is pretty easy, too – all you need is to closely follow the steps in this tutorial. Let’s get started.

Prerequisites:

  •  PHP version 7.2 or newer
  •  MySQL version 5.6 or greater OR MariaDB version 10.0 or greater.
  •  Nginx web server
  •  an Ubuntu 18.04 VPS
  •  A system user with root privileges, or access to the root account

Step 1: Log in and Update the Server

Login to your Ubuntu 18.04 server via SSH as the root user:

ssh root@IP_Address -p Port_number

where ‘IP_Address‘ and ‘Port_number‘ are the actual IP address of your server and the SSH port.

Then run the following commands to make sure that all packages are updated to their latest releases.

apt update && apt upgrade

Step 2: Install Nginx web server

WordPress needs a web server with PHP support to run and render the web pages dynamically. For this, we will install and use the Nginx web server. It is available in the official Ubuntu 18.04 repositories and it can be easily installed using the following command:

apt -y install nginx

After the installation is completed, start Nginx and enable it to start automatically after a reboot:

systemctl start nginx
systemctl enable nginx

You can check if the web server is running with this command:

systemctl status nginx

The output should look something like this:

nginx.service - A high performance web server and a reverse proxy server
   Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
   Active: active (running) 
     Docs: man:nginx(8)
 Main PID: 21137 (nginx)
    Tasks: 3 (limit: 2320)
   CGroup: /system.slice/nginx.service
           ââ21137 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
           ââ21138 nginx: worker process
           ââ21139 nginx: worker process

You can also verify if Nginx is running by accessing http://Server_IP_Address in your preferred web browser.

Step 3: Install and Configure PHP

WordPress is built on PHP, so we have to install PHP as well as several PHP extensions required by WordPress in order for it to function properly:

apt install -y php php-common php-mbstring php-gd php-intl php-xml php-mysql php-mcrypt php-fpm

Now, open the PHP configuration file, find and uncomment the ‘cgi.fix_pathinfo’ line. Open the file with your preferred text editor – we’ll be using Nano:

nano /etc/php/7.2/fpm/php.ini

Then uncomment this line:

cgi.fix_pathinfo=0

Save the configuration file and exit the editor. Then restart the PHP-FPM service for the changes to take effect:

systemctl restart php7.2-fpm.service

Step 4: Install MySQL Server and Create a Database

WordPress needs an empty MySQL database to store its data such as posts, post types, user profiles, etc. So the next step will be to install the MySQL server and create a user and database for the WordPress installation.

MySQL server can be installed using the following command:

apt install mysql-server

During the installation of the database server, you will be prompted to set a password for the MySQL root user.

After the installation completes, you can check the version of the installed MySQL database server:

mysql -V
mysql  Ver 14.14 Distrib 5.7.24, for Linux (x86_64) using  EditLine wrapper

Additionally, you can run the mysql_secure_installation post-installation script to strengthen the security of the database server. We recommend answering with the following answers:

Set root password? [Y/n] Y
Remove anonymous users? [Y/n] Y
Disallow root login remotely? [Y/n] Y
Remove test database and access to it? [Y/n] Y
Reload privilege tables now? [Y/n] Y

Next, log in to the MySQL server as user root and create a new MySQL user and database

mysql> CREATE DATABASE wordpress;
mysql> GRANT ALL PRIVILEGES ON wordpress.* TO 'wpuser'@'localhost' IDENTIFIED BY 'PASSWORD';
mysql> FLUSH PRIVILEGES;
mysql> exit;

Don’t forget to replace ‘PASSWORD’ with an actual, strong password.

Step 5: Download and Install WordPress

The latest release of WordPress can be downloaded from their official website. At the moment of writing this article it is version 5.2.2. It can be easily downloaded on your Ubuntu 18.04 server using the following command

wget https://wordpress.org/latest.zip

Once the downloading of the WordPress archive is completed, unpack it to the document root directory of your server

unzip latest.zip -d /var/www/html/

This will create a new ‘wordpress’ directory which will contain all files of the WordPress installation.

chown -R www-data:www-data /var/www/html/wordpress

Step 6: Configure Nginx

Next, in order to be able to access the WordPress site with your domain name, we will have to create an Nginx virtual block for the domain name:

server {
        listen	80;

        server_name yourdomain.com www.yourdomain.com;
        root /var/www/html/wordpress;
        index index.php index.html index.htm;

	    location / {
	        try_files $uri @index_php;
	    }

	    location = /favicon.ico {
	        log_not_found off;
	        access_log off;
	    }
	
	    location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
	        expires max;
	        log_not_found off;
	    }	

	    location = /robots.txt {
	        allow all;
	        log_not_found off;
	        access_log off;
	    }

 	   location ~ \.php$ {
 	        include snippets/fastcgi-php.conf;
 	        fastcgi_pass unix:/run/php/php7.2-fpm.sock;
 	        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
 	        include fastcgi_params;
 	   }
}

NOTE: Don’t forget to replace yourdomain.com with your unique registered domain name.

Enable the Nginx virtual block:

ln -s /etc/nginx/sites-available/yourdomain.tld /etc/nginx/sites-enabled/

You can test if everything is properly configured using the following command:

nginx -t

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

Once you confirm that everything is OK, restart Nginx for the changes to take effect:

systemctl restart nginx.service

Step 7: Complete the WordPress installation

Once all steps above are done, the installation from the command line is completed. You can open your favorite web browser and point it to http://yourdomain.com. You should get the following screen:

Select your language and follow the installation wizard to complete the WordPress installation and create your administrative account. Congratulations, you now have WordPressed installed on Ubuntu 18.04 with a LEMP stack.


Of course, if you are one of our WordPress Hosting customers, you don’t have to Install WordPress with LEMP Stack on Ubuntu 18.04, simply ask our admins, sit back and relax. Our admins will Install Install WordPress with LEMP Stack on your Ubuntu 18.04 VPS for you immediately.

PS. If you liked this post on how to install WordPress with LEMP Stack on Ubuntu 18.04, please share it with your friends on the social networks using the buttons below or simply leave a comment in the comments section. Thanks.

An updated version is available here: How to Install WordPress with LEMP on Ubuntu 20.04.

Leave a Comment