CMS

Install WordPress with Nginx on Ubuntu 20.04

In this tutorial, we shall show you how to install WordPress with Nginx on Ubuntu 20.04 server. The tutorial will guide you to install and configure Nginx as web server, PHP, MariaDB as a database.

WordPress is a free and open-source content management system written in PHP and paired with a MySQL or MariaDB database.

Install WordPress with Nginx on Ubuntu 20.04

Prerequisite:

1. Keep the server up-to-date:

# apt update -y

# apt upgrade -y

2. Install Nginx

# apt install nginx -y

In case, you enabled UFW firewall and firewall block requests of the apache web server, open a port in the firewall.

# ufw allow 80/tcp

# ufw allow 443/tcp

# ufw reload

Now, let’s verify the Apache installation. Open browser and test default page.

http://[SERVER IP]

3. Install MariaDB

We need to import the public key used by the package management system. We can import it using following command:

# apt-key adv –fetch-keys ‘https://mariadb.org/mariadb_release_signing_key.asc’

Add repository

# add-apt-repository ‘deb [arch=amd64,arm64,ppc64el] https://mirror.nodesdirect.com/mariadb/repo/10.5/ubuntu focal main’

Once the key is imported and the repository added you can install MariaDB 10.5 from the MariaDB repository with:

# apt update -y

# apt install mariadb-server -y

Login into mysql and create a database and user.

# mysql

If you have set root password:

# mysql -u root -p

First, we can create a separate database that WordPress can control.

We can create the database for WordPress by typing:

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

You can replace and call wordpress whatever you like.

Now, create a user and grant the permision of the database we have created eariler.

mysql> GRANT ALL ON wordpress.* TO ‘wordpressuser’@’localhost’ IDENTIFIED BY ‘password’;

Resplace wordpressuser to your username and set password with strong password.

To refect the change run following command:

mysql> FLUSH PRIVILEGES;

Once everything done, EXIT the mysql.

4. Install PHP and configure

# apt install php-fpm php-curl php-gd php-intl php-mbstring php-soap php-xml php-xmlrpc php-zip

Verify the PHP installation:

# php -v

Open php.ini file and look for the parameter that sets cgi.fix_pathinfo. We will be uncommented by removing semi-colon(;) and set to 0.

# vi /etc/php/7.4/fpm/php.ini

cgi.fix_pathinfo=0

Once you have done, save and exit.

Next, restart php-fpm service:

# systemctl restart php7.4-fpm

Configure Nginx server block to use PHP Processor. Here we are using default server block. If you want, you can create new server block.

# vi /etc/nginx/sites-available/default

  • Here we need to add an index.php index directive to allow PHP files to be served when a directory is requested.
  • Modify the server_name directive. Replace localhost with your FQDN domain name or public IP address.
  • Add a try_files directive to make sure Nginx doesn’t pass bad requests to our PHP processor.

The server block looks like:

server {
listen 80 default_server;
listen [::]:80 default_server;

root /var/www/html;

index index.html index.htm index.nginx-debian.html index.php;

server_name _;

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

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;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}

}

Replace the server_domain_name_or_IP to your server IP or website name.

Test the configuration file.

# nginx -t

If there are no errors, restart the Nginx to make the necessary changes.

# systemctl restart nginx

5. Download WordPress

Now, we can download latest version of the WordPress. Use /tmp directory to download and extract WordPress.

# cd /tmp

# curl -LO https://wordpress.org/latest.tar.gz

Extract the compressed file to create the WordPress directory structure:

# tar xzvf latest.tar.gz

Move all the files into our root directory using following command:

# cp -a /tmp/wordpress/. /var/www/html/wordpress

Next, change the wp-config-sample.php config file name to wp-config.php.

# cd /var/www/html/wordpress

# cp wp-config-sample.php wp-config.php

Now, change the ownership to www-data user and group.

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

6. WordPress configuration file

Next, we need to set up main WordPress configuration file. We need to generate WordPress secret key. WordPress provides a secure generator for these values so that you do not have to try to come up with good values on your own.

To grab secure values from the WordPress secret key generator, type:

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

The above command will generate secret key. Copy the secret key and past it in to the WordPress configuration file wp-config.php.

# vi /var/www/html/wordpress/wp-config.php

Find the section that contains the dummy values for those settings.

It will looks like:

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 the command line.

Now, we need to add database name, user and password in the file also we can explicitly set the filesystem method to “direct”.  Like shown below:

. . .

define(‘DB_NAME’, ‘wordpress’);

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

/** MySQL database password */
define(‘DB_PASSWORD’, ‘password’);

. . .

define(‘FS_METHOD’, ‘direct’);

Save and close the file when you are finished.

7. Complete the WordPress Installation

We have finished server side configurations.

In your web browser, navigate to your server’s domain name or public IP address:

http://server_domain_or_IP

Select the language you would like to use and click continue, next set the admin username and password. Once every is set, it will redirect to login page. Enter admin username and password.

Install wordpress on ubutu 20.04

We have successfully installed WordPress.

In this tutorial we have seen how to install WordPress with Nginx on Ubuntu 20.04 server.

Related Articles