WordPress is without doubt the most popular content management software in the world at the moment. It is estimated that more than 27 million live websites on the Internet have been created using WordPress. Even Allthings.how is created using WordPress!

As you might know, WordPress needs a web server setup along with a database management system and obviously a PHP engine on the computer on which it is to be installed. Such a stack of software is commonly required by content management software, and is often abbreviated as LAMP (Linux, Apache, MySQL, PHP/Perl/Python) or WAMP (Windows, Apache, MySQL, PHP/Perl/Python). In this article we will see how to install WordPress using the LEMP (Linux, Nginx, MySQL, PHP) stack on a Ubuntu system.

Installing LEMP Stack and WordPress

To install the stack, run:

sudo apt update
sudo apt install nginx mysql-server mysql-client php php-fpm php-mysql

Note: For older Ubuntu versions (version 14.04 and below), you need to use apt-get instead of apt.

The package php-fpm is optional, but highly recommended. It installs the PHP Fast CGI Process Manager, which is used to optimize performance of the backend. User may also choose the popular open source replacement MariaDB, instead of MySQL, which needs exactly same configuration as MySQL.

To install WordPress, we simply need to download and unzip it. We unzip it in folder /var/www/html, which is the default root folder for web servers in Linux.

cd /var/www/html
sudo wget http://wordpress.org/latest.zip
sudo unzip latest.zip
cd wordpress

Configure Nginx for WordPress

Right now, for the sake of simplicity, we want to point the domain 127.0.0.1 (localhost) to our WordPress installation. For production installations, user needs to use the hostname or IP Address of the system in the Nginx configuration.

First, create a new file /etc/nginx/sites-available/localhost using vim or any editor of your choice:

sudo vim /etc/nginx/sites-available/localhost

Next, enter the following Nginx configuration in the file:

server {
        listen 80;
        listen [::]:80;
        root /var/www/html/wordpress;
        index index.php;
        server_name 127.0.0.1;
        location / {
                try_files $uri $uri/ =404;
        }
        location ~ \.php$ {
                fastcgi_pass unix:/run/php/php7.3-fpm.sock;
                fastcgi_index index.php;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                include fastcgi_params;
        }
 }

Things you must modify in the configuration file above:

  • server_name: Change it to your domain name.
  • PHP FPM version: The line fastcgi_pass unix:/run/php/php7.0-fpm.sock should be changed based on the PHP FPM version (i.e., PHP version, as Ubuntu repository updates the PHP and PHP FPM to the same version). To do this, run php -v, to see the version. Then, for example, if the version is 7.4, change the above line to fastcgi_pass unix:/run/php/php7.4-fpm.sock

Basically, here we are configuring Nginx to direct the requests to 127.0.0.1 to our root WordPress folder. We specify the index file (WordPress index file is index.php) and some PHP FPM parameters. For full explanation on Nginx Configuration file directives, take a look at the Nginx Documentation.

Press Escape to go to vim command mode, then type :wq to save and exit the file.

Next, we have to create a symbolic link for this file in Nginx Sites Enabled folder:

sudo ln -s /etc/nginx/sites-available/localhost /etc/nginx/sites-enabled

Configure MySQL for WordPress

Open MySQL command prompt using:

mysql -u root -p

A default root user is created by MySQL during installation, with password same as the system root password, and with MySQL admin level privileges. You can use another user if you have already created another MySQL user, however, make sure the user has permissions to create database.

On MySQL prompt, type the following SQL to create a new database for our WordPress installation:

mysql> CREATE DATABASE databasename;

☝ Change databasename in the command above to your preference.

Then, create a username and password in the database which we’ll use the wp_config file later in guide.

mysql> GRANT ALL PRIVILEGES ON databasename.* TO "wordpressusername"@"localhost"
-> IDENTIFIED BY "password";

☝ Change wordpressusername and password to your preference, and databasename to what you set in the previous command.

Finally, run the flush command and then exit the MySQL prompt.

mysql> FLUSH PRIVILEGES;
mysql> EXIT

Now, go to WordPress root folder. We have to establish MySQL connection in WordPress configuration file:

cd /var/www/html/wordpress

Create WordPress configuration file by copying the sample configuration file:

sudo cp wp-config-sample.php wp-config.php

Open the configuration file in vim or any editor of your choice:

sudo vim wp-config.php

Change the PHP variables DB_NAME, DB_USER, DB_PASSWORD in the file:

define( 'DB_NAME', 'databasename' );

/** MySQL database username */
define( 'DB_USER', 'wordpressusername' );

/** MySQL database password */
define( 'DB_PASSWORD', 'password' );

Press Escape to go to vim command line mode. Type :wq and press Enter to save and exit the file.

Final Setup

Modify the directory permissions on wordpress folder to enable access to it from browser.

sudo chmod -R 755 .

Permission 755 means all permissions for the directory owner, read and execute permissions for owner’s user group, and read and execute permissions for other users. For details on the meaning of the permissions, refer to man page on chmod (man chmod).

Finally, restart Nginx for the new configuration to take place:

sudo service nginx restart

Open your website domain name (as configured in server_name in the Nginx configuration file ) in a web browser to check if WordPress is working. It should redirect you to WordPress initial setup screen.

Enter the details and finish your WordPress setup.