How to Install WordPress with LEMP On Ubuntu 20.04

How to Install WordPress with LEMP On Ubuntu 20.04


WordPress, one of the most popular content management systems (CMS), allows users to set up flexible blogs and websites using a MySQL backend with PHP processing. WordPress has seen an incredible adoption rate among new and experienced engineers alike. It is a perfect choice for getting a website up and running efficiently. After an initial setup, almost all administration for WordPress websites can take place on its graphical interface — the rich features of WordPress make it great for websites built to scale.
In this tutorial, we'll focus on getting an instance of WordPress set up on a LEMP stack (Linux, Nginx, MySQL, and PHP) for an Ubuntu 20.04 server.


Before you start, please make sure:
When you are finished with the setup, log in to your server as the sudo user to continue.

1   Creating MySQL Databases and Users  

WordPress uses MySQL to manage and store site and user information. Since you already have MySQL installed, the next step is to create a database and a user for WordPress to use.
To get started, log in to the MySQL root (administrative) account. If MySQL is configured to use the auth_socket authentication plugin (which is the default), you can log in to the MySQL administrative account using sudo:
$ sudo mysql
If you have changed the authentication method for MySQL root user to mysql_native_passord, then use the following command instead:
$ mysql -u root -p
You will be prompted for the password you set for the MySQL root account.
Once logged in, create a separate database that WordPress can control. You can name the database whatever you would like. In this guide, we will call it "wordpress". You can create a database for WordPress by entering:
mysql> CREATE DATABASE wordpress DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci;
Next, let’s create a separate MySQL user account that we will use exclusively to operate on our new database. Creating single-purpose databases and accounts is a good idea from a management and security standpoint. We’ll name the user "wpuser" in this guide — feel free to change this if you’d like.
By using the following command, you are going to create an account, set a password, and grant access to the database you created. Remember to set a strong password here:
mysql> CREATE USER'wpuser'@'localhost' IDENTIFIED BY 'password';

mysql> GRANT ALL ON wordpress.* TO 'wpuser'@'localhost';
You now have a database and user account, each made specifically for WordPress. With the database tasks complete, let’s exit out of MySQL by typing:

mysql> EXIT;
When it's done, you will return to the Linux shell.

2   Installing Additional PHP Extensions

When setting up the LEMP stack, it requires a very minimal set of extensions to get PHP to communicate with MySQL.
WordPress and many of its plugins leverage additional PHP extensions, and you’ll use a few more in this tutorial.
Let’s download and install some of the most popular PHP extensions for use with WordPress by typing:           
$ sudo apt update

$ sudo apt install php-curl php-gd php-intl php-mbstring php-soap php-xml php-xmlrpc php-zip
After you finish installing the extensions, restart the PHP-FPM process so that the running PHP processor can leverage the newly installed features:
sudo systemctl restart php8.0-fpm
You now have all of the PHP extensions needed, installed on the server.

3   Configuring Nginx

Next, let’s make a few adjustments to our Nginx server block files. Based on the prerequisite tutorials, you should have a configuration file for your site in the /etc/nginx/sites-available/ directory configured to respond to your server’s domain name or IP address and protected by a TLS/SSL certificate. We’ll use /etc/nginx/sites-available/wordpress as an example here. Please make sure to replace the path with your configuration file.
Additionally, we will use /var/www/wordpress as the root directory of our WordPress install in this guide. Again, you should use the web root specified in your configuration.
Open your site’s server block file with sudo privileges:
$ sudo nano /etc/nginx/sites-available/wordpress
Within the main server block, let’s add a few location blocks. Start by creating exact-matching location blocks for requests to /favicon.ico and /robots.txt, both of which you do not want to log requests for.
Use a regular expression location to match any requests for static files. We will then turn off the logging for these requests and will mark them as highly cacheable since these are typically expensive resources to serve. You can adjust this static files list to contain any other file extensions your site may use:
server {
. . .

location = /favicon.ico { log_not_found off; access_log off; }
location = /robots.txt { log_not_found off; access_log off; allow all; }
location ~* \.(css|gif|ico|jpeg|jpg|js|png)$ {
expires max;
log_not_found off;
. . .
Inside of the existing location / block, let’s adjust the try_files list. Comment out the default setting by prepending the line with a pound sign (#) and then add the highlighted line. This way, instead of returning a 404 error as the default option, control is passed to the index.php file with the request arguments.
This should look something like this:
​server {
. . .
location / {
#try_files $uri $uri/ =404;
try_files $uri $uri/ /index.php$is_args$args;
. . .
When you are finished, save and close the file.
Now, let’s check our configuration for syntax errors by typing:
sudo nginx -t
If no errors were reported, reload Nginx by typing:
$ sudo systemctl reload nginx
Next, let’s download and set up WordPress.

4   Downloading WordPress

For security reasons, it is always recommended to get the latest version of WordPress directly from the project’s website.
Change into a writable directory and then download the compressed release by typing:
$ cd /tmp
This changes your directory to the temporary folder. Then, enter the following command to download the latest version of WordPress in a compressed file:
$ curl -LO
Extract the compressed file to create the WordPress directory structure:
$ tar xzvf latest.tar.gz
You will be moving these files into our document root momentarily, but before you do, let’s copy over the sample configuration file to the filename that WordPress actually reads:
$ cp /tmp/wordpress/wp-config-sample.php /tmp/wordpress/wp-config.php
Now, let’s copy the entire contents of the directory into our document root. We’re using the -a flag to make sure our permissions are maintained and a dot at the end of our source directory to indicate that everything within the directory should be copied (including hidden files):
$ sudo cp -a /tmp/wordpress/. /var/www/wordpress
Now that our files are in place, you’ll assign ownership to the www-data user and group. This is the user and group that Nginx runs as, and Nginx will need to be able to read and write WordPress files to serve the website and perform automatic updates:
$ sudo chown -R www-data:www-data /var/www/wordpress
Files are now in the server’s document root and have the correct ownership, but you still need to complete some additional configuration.

5   Setting up the WordPress Configuration File

Next, let’s make some changes to the main WordPress configuration file.
When you open the file, you’ll start by adjusting some secret keys to provide some security for our installation. WordPress provides a secure generator for these values so that you don’t have to come up with values on your own. These are only used internally, so it won’t hurt usability to have complex, secure values here.
To grab secure values from the WordPress secret key generator, type:
curl -s
You will get back unique values that look something like this:
administrator@ANGELICA:/tmp$ curl -s
define('AUTH_KEY',         'mM FYJ/C`*#BS?Hu67j>*fou}B)[sTtt(WO}8*8c52<^Dh(O7|l=s3xbf9J9h)Br');
define('SECURE_AUTH_KEY',  '@iS`;GA!I`5gI=;Osr[(.gE|tf1R1A5*tNQ2%HSazV0Y}U:B+_n5Ve/H_u(q0?q]');
define('LOGGED_IN_KEY',    'HvPu4+OGJW&4T)*%Hh1zD.wh,LqNnBqvMe|)D?}lMd[$<%>qa_@&4KmIqP5/e(OW');
define('NONCE_KEY',        '5E+VU`l{a`~1-7j9IGal.wXY=)ngSNL9z@Ud7]x`qwKrg9*9!#G<UEiq>t^D(37g');
define('AUTH_SALT',        'rR8%iQS_jqKyOS%9t@^#;dgh6+U1B.-9}gdw$+t]4@guOb:e>.l;;K%h)+<p%-KQ');
define('SECURE_AUTH_SALT', 'G!);y<ZO(8VMB^`dM@aMY^qmgH&h@&*~lkiRao ~7MRM(Q*$XLwVtter^0rAJQa=');
define('LOGGED_IN_SALT',   '}[&k7e+wd#UXbS2s|hKfT5X5x`iU^{vZ69B 8wjHxuAyittF3;E4&-^,yCml=0Y;');
define('NONCE_SALT',       'TPu1j_($k)@qXes(VM5Nh*%.nypNOr} noFj,N^8_+*xMccP82&Opt:yHx*N#JTv');
These are configuration lines that you can paste directly into your configuration file to set secure keys. Copy the output you received now. Now, open the WordPress configuration file:
$ sudo nano /var/www/wordpress/wp-config.php
Find the section that contains the dummy values for those settings. It will look something like this:
. . .

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');

. . .
Delete those lines and paste in the values you copied from last step:
Next, let’s modify some of the database connection settings at the beginning of the file. You’ll have to adjust the database name, the database user, and the associated password that was configured within MySQL.
The other change you should make is to set the method that WordPress uses to write to the filesystem. Since you’ve given the web server permission to write where it needs to, you can explicitly set the filesystem method to “direct”. Failure to set this with our current settings would result in WordPress prompting for FTP credentials when we perform some actions. Add this setting below the database connection settings, or anywhere else in the file:
. . .

define( 'DB_NAME', 'wordpress' );

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

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

. . .

define( 'FS_METHOD', 'direct' );
Save and close the file when you’re done.

6   Completing the Installation Through the Web Interface

Now that the server configuration is complete, you can finish up the installation through WordPress’ web interface.
In your web browser, navigate to your server’s domain name or public IP address:
Select the language you would like to use:
Next, you will come to the main setup page.
Select a name for your WordPress site and choose a username (it is recommended not to choose something like “admin” for security purposes). A strong password is generated automatically. Save this password or select an alternative strong password.
Enter your email address and select whether you want to discourage search engines from indexing your site:
When you click ahead, you will be taken to a page that prompts you to log in:
Once you log in, you will be taken to the WordPress administration dashboard:
Right-click on Visit Site, and select open in new tab. Your site will open in a new tab.
The default home page of the latest version 5.9.3 of WordPress looks like the following.


WordPress should be installed and ready to use! Once you’ve installed WordPress, you can follow the rest of the guide on how to make a WordPress website. It will take you through other important steps including choosing a theme, installing must-have plugins, adding content, and more.

Add Feedback