How to Install WordPress with Nginx on Ubuntu Server (18.04 / 20.04)

How to Install WordPress with Nginx on Ubuntu Server step by step tutorial.  Secure access to WordPress by using an Nginx Web Server.

What is WordPress?

WordPress is a free, open source and one of the most popular content management systems on the internet. Compared to other CMS like Drupal or Joomla, WordPress is used by 60% of websites that are currently online. WordPress allows you to create a simple portfolio website, blog and make the most of eCommerce websites. It is very popular because of its ease of use, simple installation, pluggable and customizable.

WordPress can run on both Apache and Nginx web servers. Compared to Apache web servers, Nginx can handle thousands of simultaneous connections in a fixed memory footprint. Nginx server also deals with static files better, with built in, easily tuned caching controls. This will increase the WordPress website speed and serve more traffic.

Follow this post below to learn how to install WordPress with Nginx on Ubuntu 20.04 server.

How to Install WordPress with Nginx on Ubuntu Server

Install LEMP Server

LEMP is an open source web application stack used for deploying PHP based applications on the internet. In this section, we will show you how to install LEMP (Nginx, MariaDB, and PHP) on Ubuntu 20.04 server.

Install Nginx

First, install the Nginx web server by running the following command:

				
					apt-get install nginx -y
				
			

Once the Nginx is installed, start the Nginx service and enable it to start at system reboot:

				
					systemctl start nginx
systemctl enable nginx
				
			

Install PHP

WordPress is a PHP based application. So you will need to install PHP and other required extensions to your server. You can install all of them by running the following command:

				
					apt-get install php php-cli php-fpm php-mysql php-json php-opcache php-mbstring php-xml php-gd php-curl -y
				
			

Once all the packages are installed, start the PHP-FPM service and enable it to start at system reboot:

				
					systemctl start php7.4-fpm
systemctl enable php7.4-fpm
				
			

Install MariaDB Database

WordPress uses MySQL or MariaDB as a database backend. Here, we will use MariaDB as a database backend. You can install it by running the following command:

				
					apt-get install mariadb-server -y
				
			

Once the MariaDB is installed, start the MariaDB service and enable it to start at system reboot:

				
					systemctl start mariadb
systemctl enable mariadb
				
			

Create a WordPress Database

Next, you will need to create a database and user for WordPress. First, log in to the MariaDB with the following command:

				
					mysql
				
			

Once you are log in, create a database and user with the following command:

				
					MariaDB [(none)]> CREATE DATABASE wordpressdb;
MariaDB [(none)]> GRANT ALL ON wordpressdb.* TO 'wordpressuser'@'localhost' IDENTIFIED BY 'securepassword' WITH GRANT OPTION;
				
			

Next, flush the privileges and exit from the MariaDB with the following command:

				
					MariaDB [(none)]> FLUSH PRIVILEGES;
MariaDB [(none)]> EXIT;
				
			

Download WordPress

Next, change the directory to the Nginx webroot and download the latest version of WordPress using the following command:

				
					cd /var/www/html
wget https://wordpress.org/latest.tar.gz
				
			

Once the download is completed, extract the downloaded file with the following command:

				
					tar -xvzf latest.tar.gz
				
			

Please change the ownership and permission of the WordPress directory:

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

Rename the WordPress default configuration file using the command below:

				
					cd wordpress
cp wp-config-sample.php wp-config.php
				
			

Edit the WordPress configuration and define your database settings:

				
					nano wp-config.php
				
			

Change the following lines:

				
					/** The name of the database for WordPress */
define( 'DB_NAME', 'wordpressdb' );

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

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

/** MySQL hostname */
define( 'DB_HOST', 'localhost' );

/** Database charset to use in creating database tables. */
define( 'DB_CHARSET', 'utf8' );
				
			

Save and close the file when you are finished.

Create an Nginx Virtual Host COnfiguration File

Next, you will need to create an Nginx virtual host configuration file to serve WordPress over the internet. You can create it using the following command:

				
					nano /etc/nginx/conf.d/wordpress.conf
				
			

Add the following lines:

				
					server {
listen 80;
root /var/www/html/wordpress;
index index.php index.html;
server_name wordpress.example.com;

access_log /var/log/nginx/wordpress.access.log;
error_log /var/log/nginx/wordpress.error.log;

location / {
try_files $uri $uri/ =404;
}

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

location ~ /\.ht {
deny all;
}

location = /favicon.ico {
log_not_found off;
access_log off;
}

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

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

Save and close the file then verify the Nginx configuration using the command below:

				
					nginx -t
				
			

If everything is fine, you will get the following output:

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

Finally, restart the Nginx service to apply the changes:

				
					systemctl restart nginx
				
			

To check the Nginx status, run the following command:

				
					systemctl status nginx
				
			

You should see the following output:

				
					● 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) since Wed 2021-11-10 06:55:35 UTC; 6s ago
Docs: man:nginx(8)
Process: 18475 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
Process: 18482 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
Main PID: 18490 (nginx)
Tasks: 2 (limit: 2353)
Memory: 2.6M
CGroup: /system.slice/nginx.service
├─18490 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
└─18491 nginx: worker process

Nov 10 06:55:35 ubuntu2004 systemd[1]: Starting A high performance web server and a reverse proxy server...
Nov 10 06:55:35 ubuntu2004 systemd[1]: Started A high performance web server and a reverse proxy server.
				
			

Access WordPress Installation

At this point, WordPress is installed with Nginx. Now, open your web browser and access the WordPress installation using the URL http://wordpress.example.com. You will be redirected to the following screen:

Select your language and click on the Continue button. You should see the Site Information screen:

Provide your website information and click on the Install WordPress button. Once the installation has been completed, you should get the following screen:

Click on the Login button. You should see the WordPress login screen:

Provide your WordPress admin username, password and click on the Login button. You should see the WordPress dashboard in the following screen:

Enable SSL on WordPress

It is also recommended to install the Let’s Encrypt SSL SSL on the WordPress website. To do so, you will need to install the Certbot client to your server. You can install it using the following command:

				
					apt-get install certbot python3-certbot-nginx -y
				
			

Once installed, run the following command to download and install the SSL on the WordPress website.

				
					certbot --nginx -d wordpress.example.com
				
			

You will be asked to provide a valid email address and accept the term of service as shown below:

				
					Saving debug log to /var/log/letsencrypt/letsencrypt.log
Plugins selected: Authenticator nginx, Installer nginx
Enter email address (used for urgent renewal and security notices) (Enter 'c' to
cancel): hitjethva@gmail.com

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Please read the Terms of Service at
https://letsencrypt.org/documents/LE-SA-v1.2-November-15-2017.pdf. You must
agree in order to register with the ACME server at
https://acme-v02.api.letsencrypt.org/directory
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(A)gree/(C)ancel: A

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Would you be willing to share your email address with the Electronic Frontier
Foundation, a founding partner of the Let's Encrypt project and the non-profit
organization that develops Certbot? We'd like to send you email about our work
encrypting the web, EFF news, campaigns, and ways to support digital freedom.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(Y)es/(N)o: Y
Obtaining a new certificate
Performing the following challenges:
http-01 challenge for wordpress.example.com
Waiting for verification...
Cleaning up challenges
Deploying Certificate to VirtualHost /etc/nginx/conf.d/wordpress.conf

				
			

Next, choose whether or not to redirect HTTP traffic to HTTPS as shown bellow:

				
					- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1: No redirect - Make no further changes to the webserver configuration.
2: Redirect - Make all requests redirect to secure HTTPS access. Choose this for
new sites, or if you're confident your site works on HTTPS. You can undo this
change by editing your web server's configuration.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Select the appropriate number [1-2] then [enter] (press 'c' to cancel): 2

				
			

Type 2 and hit Enter to finish the installation. You should see the following output:

				
					Redirecting all traffic on port 80 to ssl in /etc/nginx/conf.d/wordpress.conf

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Congratulations! You have successfully enabled https://wordpress.example.com

You should test your configuration at:
https://www.ssllabs.com/ssltest/analyze.html?d=wordpress.example.com
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

IMPORTANT NOTES:
- Congratulations! Your certificate and chain have been saved at:
/etc/letsencrypt/live/wordpress.example.com/fullchain.pem
Your key file has been saved at:
/etc/letsencrypt/live/wordpress.example.com/privkey.pem
Your cert will expire on 2022-02-10. To obtain a new or tweaked
version of this certificate in the future, simply run certbot again
with the "certonly" option. To non-interactively renew *all* of
your certificates, run "certbot renew"
- Your account credentials have been saved in your Certbot
configuration directory at /etc/letsencrypt. You should make a
secure backup of this folder now. This configuration directory will
also contain certificates and private keys obtained by Certbot so
making regular backups of this folder is ideal.
- If you like Certbot, please consider supporting our work by:

Donating to ISRG / Let's Encrypt: https://letsencrypt.org/donate
Donating to EFF: https://eff.org/donate-le

- We were unable to subscribe you the EFF mailing list because your
e-mail address appears to be invalid. You can try again later by
visiting https://act.eff.org.

				
			

You can now access your WordPress site securely using the URL https://wordpress.example.com

Great you have followed all the steps and learned how to Install WordPress with Nginx on Ubuntu Server.

How to Install WordPress with Nginx on Ubuntu Server Conclusion

In the above post, we explained how to install WordPress with Nginx on Ubuntu. You can now start creating your blog or website from the WordPress dashboard.

Avatar for Hitesh Jethva
Hitesh Jethva

I am a fan of open source technology and have more than 10 years of experience working with Linux and Open Source technologies. I am one of the Linux technical writers for Cloud Infrastructure Services.

0 0 votes
Article Rating
Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x