How To Install WordPress With Lemp/Nginx On Ubuntu 20.04

Install Wordpress With Lemp On Ubuntu 20.04

About WordPress

In the list of content management system (CMS), WordPress is widely renowned since it allows users to setup flexible blogs and websites using MySQL and PHP processing. This CMS has great adoption rate due to flexible availability of plugins and easy to configure. Once its initial setup is done, it’s further administration can can easily be accomplished through its GUI (Graphical User Interface).

So, we are here going to discuss about how to install an instance of WordPress on Lemp/Nginx on Ubuntu 20.04 server. (Using MySQL and PHP)

Prerequisites

Since we are going to install wordpress on ubuntu 20.04, obviously you require to have access over ubuntu 20.04 server. Do make sure you have enough privileges as well to install WordPress successully.

Once you done with all above steps, you can follow the steps discussed further to install WordPress on Ubuntu 20.04 with LEMP stack.

Step 1: Create a MySQL Database and User

Since the WordPress uses MySQL to manage and store all its information, creating MySQL database for your site is necessary at first. To do so, log in to your MySQL account with root access. Below are the commands to log in to your MySQL account.

sudo mysql (If MySQL is configured to use auth_socket authentication plugin, which is default.)

or

mysql -u root -p (If you have previously changed the authentication method to use a secure password for your MySQL root account)

In the second case, you will be asked to enter your password, just enter the assigned root password to log in to MySQL account.

Once done, you need to create a separate database for your new WordPress installation, for an instance, we used “servonode” as newly created wordpress database in commands. Below is the command to create a database.

mysql> CREATE DATABASE servonode DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci;

Now, enter the command below to create a MySQL user account, set a password and grant all access to just created database. In our example, we have used “servonodeuser” as the new user.

mysql> GRANT ALL ON wordpress.* TO 'wordpressuser'@'localhost';

you have now successfully created database and a user for your WordPress installation. Now, just enter the command below to log-out MySQL server.

mysql> EXIT;

Step 2: Install Essential PHP Extensions

While LEMP stack is setup, it needs a very minimal extensions to make PHP communicating with MySQL. But, When WordPress is installed, however its plugins may require some additional essential PHP extensions too. So, you need to download and install some of the popular PHP extensions in addition.

Just download and install those PHP extensions with the set of commands below.

sudo apt update
sudo apt install php-curl php-gd php-intl php-mbstring php-soap php-xml php-xmlrpc php-zip

Note: Some plugins may require more set of extensions and it should be installed in addition. To check the same in your own case, check the plugin’s documentation page to find which extensions are required in addition.

When all essential extensions are installed successfully, it needs to restart PHP-FPM to allow PHP processor to leverage new extensions. Below is the command to restart PHP-FPM.

sudo systemctl restart php7.4-fpm

Step 3: Configure Nginx Block

Now, it’s time to create Nginx server block files or server configuration file for your site, which is located at etc/nginx/sites-available/ directory. In our example, we have created the server block as etc/nginx/sites-available/servonode.conf, and you can rename the same according to your own site requirements.

Also, we are using var/www/servonode as the root directory under which the WordPress installation will be done. You can also change the root location and directory name according to your need in your own server block.

To open your new site’s server block file or create a new one, run the following command.

sudo nano /etc/nginx/sites-available/servonode.conf

Just copy the code content below and paste it into the configuration file and save it. Do not forget to replace the highlighted value with your own domain and directory root location.

server {
listen 80;
listen [::]:80;
root /var/www/servonode;
index index.php index.html index.htm;
server_name example.com www.example.com;

client_max_body_size 100M;

location / {
try_files $uri $uri/ /index.php?$args; 
}

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

Save the modified file and exit.

Once the file is saved, run the command below to check your server block for systax errors.

sudo nginx -t

If you see the above command results in error, check the server block file once again to fix issues. If not, just reload and restart Nginx with commands below.

sudo systemctl reload nginx
sudo service nginx restart

How To Secure Your Site With Free SSL With Nginx

Step 4: Download WordPress and Extract Its Files In Root WP Directory

Since your server block is configured in Ubuntu 20.04, it’s time now to download and set up WordPress. To ensure safety measures, it’s highly recommended to download the latest version of WordPress from its official website. Follow the instructions discussed ahead.

At first change to /tmp directory and download the WordPress compressed release by typing below commands. Then extract the downloaded file and move it into a new WordPress root directory (servonode is taken here as example).

cd /tmp && wget https://wordpress.org/latest.tar.gz
tar -zxvf latest.tar.gz
cp /tmp/wordpress/wp-config-sample.php /tmp/wordpress/wp-config.php
sudo cp -a /tmp/wordpress/. /var/www/servonode

Now, it’s necessary to assign the file’s ownership to www-data user and group. This user and group is actually what the Nginx will need to read and write WordPress files, so as it can server the website and do automatic updates as well. To assign the ownership, run the command below.

sudo chown -R www-data:www-data /var/www/servonode

All WordPress files are now successfully saved in root directory assigned for WordPress installation. Now it’s required to complete additional configuration, which is discussed in the step 5.

Step 5: Setting up WordPress configuration file

In this step, you will require to adjust some secret keys to offer some security to your WordPress installation. WordPress offer users a secure generator for these values and surely you won’t have those values with your own. To grab the secure values for your new WordPress installation with Wp secret key generator, run the following command.

curl -s https://api.wordpress.org/secret-key/1.1/salt/

The newly generated keys will appear like what is mentioned in the example below.

Note: It’s recommended for you to generate new unique key each time when installing new WordPress. You should not copy and paste the following code as it’s just for instance.

Output

define('AUTH_KEY', '1jl/vqfs<XhdXoAPz9 DO NOT COPY THESE VALUES c_j{iwqD^<+c9.k<J@4H');
define('SECURE_AUTH_KEY', 'E2N-h2]Dcvp+aS/p7X DO NOT COPY THESE VALUES {Ka(f;rv?Pxf})CgLi-3');
define('LOGGED_IN_KEY', 'W(50,{W^,OPB%PB<JF DO NOT COPY THESE VALUES 2;y&,2m%3]R6DUth[;88');
define('NONCE_KEY', 'll,4UC)7ua+8<!4VM+ DO NOT COPY THESE VALUES #`DXF+[$atzM7 o^-C7g');
define('AUTH_SALT', 'koMrurzOA+|L_lG}kf DO NOT COPY THESE VALUES 07VC*Lj*lD&?3w!BT#-');
define('SECURE_AUTH_SALT', 'p32*p,]z%LZ+pAu:VY DO NOT COPY THESE VALUES C-?y+K0DK_+F|0h{!_xY');
define('LOGGED_IN_SALT', 'i^/G2W7!-1H2OQ+t$3 DO NOT COPY THESE VALUES t6**bRVFSD[Hi])-qS`|');
define('NONCE_SALT', 'Q6]U:K?j4L%Z]}h^q7 DO NOT COPY THESE VALUES 1% ^qUswWgn+6&xqHN&%');

These values are required to be pasted directly in WordPress configuration file (wp-config.php) to set secure keys. Just copy the output you received in your case.

Then, open the wp-config.php file.

sudo nano /var/www/servonode/wp-config.php

In the configuration file, scroll down to look for section that includes the dummy values for key values. It will appear like what is mentioned below within wp-config.php file.

. . .

define('AUTH_KEY', 'put your unique phrase here');
define('SECURE_AUTH_KEY', 'put your unique phrase here');
define('LOGGED_IN_KEY', 'put your unique phrase here');
define('NONCE_KEY', 'put your unique phrase here');
define('AUTH_SALT', 'put your unique phrase here');
define('SECURE_AUTH_SALT', 'put your unique phrase here');
define('LOGGED_IN_SALT', 'put your unique phrase here');
define('NONCE_SALT', 'put your unique phrase here');

. . .

In the section, delete and replace those lines with newly generated one which you have copied just above.

Further, you need to alter some more values in configuration file like database name, database user name, and password which was created earlier in MySQL. Just replace the highlighted values as shown below.

. . .

define( 'DB_NAME', 'wordpress' );

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

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

. . .

Once you pasted your own values, save and close the file.

Step 6: Complete WordPress installation through Web Interface

Since you have successfully completed all configuration on server side, now you need to finish the installation through Web Interface.

Just open your server’s domain name or IP address in web browser.

http://server_domain_or_IP

in the next step, you will be asked to select your preferred language. Select accorrding to your requirement and pressn Continue button.

Now, you will be directed to main setup page where you will need to enter your WordPress site’s title, username, password, and admin email details. You can also check the checkbox in the bottom to discourage search engine from indexing your site, if required. Once done, click on Install WordPress button.

After the installation is done, you will be directed to a page showing username and password as hidden along with option to log in to WordPress website.

Click on log in button, and enter your username and password to login, and the Dashboard panel will appear like one shown below.

Finally, you have successfully installed WordPress on LEMP/Nginx on Ubuntu 20.04 server. Further, you can start to design and complete your site’s preferences like setting theme, permalinks, etc. Hope this article has helped you to install WordPress easily.