How to Install WordPress with Nginx on Ubuntu Linux

|

|

The post provides a comprehensive tutorial on how to install the WordPress content management system (CMS) on Ubuntu Linux using an Nginx HTTP web server. It covers the installation of Nginx, MariaDB database server, and PHP modules, before detailing how to secure the site with Let’s Encrypt free SSL certificates. Further instructions guide users through…

This brief post shows students and new users how to install the WordPress content management system (CMS) on Ubuntu Linux with an Nginx HTTP web server.

This post also has a link to set up free Let’s Encrypt SSL certificates to secure your WordPress website.

Installing WordPress with Nginx on Ubuntu Linux is a great way to set up a website or blog. Nginx is a popular web server known for its speed, reliability, and scalability.

This makes it an excellent choice for hosting WordPress sites. Ubuntu Linux is a popular operating system widely used for web hosting. It is stable, secure, and easy to use, making it an ideal platform for hosting WordPress sites.

Installing WordPress with Nginx on Ubuntu Linux provides a powerful and flexible platform for hosting websites and blogs.

For more about WordPress, please check its homepage

To get started with installing WordPress on Ubuntu Linux, follow the steps below:

How to install Nginx on Ubuntu Linux

As mentioned above, we will run WordPress using the Nginx web server. WordPress requires a web server to function, and Nginx is one of the most popular open-source web servers available today.

To install Nginx on Ubuntu, run the commands below:

sudo apt update
sudo apt install nginx

After installing Nginx, the commands below can stop, start, and enable Nginx services to start every time your server starts up.

sudo systemctl stop nginx.service
sudo systemctl start nginx.service
sudo systemctl enable nginx.service

To test whether Nginx is installed and functioning, open your web browser and browse to the server’s IP address or hostname.

http://localhost

If you see the above page in your browser, Nginx works as expected.

How to install MariaDB on Ubuntu Linux

A database server is required for WordPress to function. WordPress stores its content in a database, and MariaDB is probably the best database server to run WordPress.

MariaDB is fast and secure and is the default server for almost all Linux servers. To install MariaDB, run the commands below:

sudo apt install mariadb-server
sudo apt install mariadb-client

After installing MariaDB, the commands below can stop, start, and enable MariaDB services to start up when the server boots.

sudo systemctl stop mariadb.service
sudo systemctl start mariadb.service
sudo systemctl enable mariadb.service

Next, run the commands below to secure the database server with a root password if you were not prompted to do so during the installation.

sudo mysql_secure_installation

When prompted, use the guide below to answer:

If you've just installed MariaDB, and haven't set the root password yet, you should just press enter here.

Enter current password for root (enter for none): PRESS ENTER

Switch to unix_socket authentication [Y/n] n

Change the root password? [Y/n] n

Remove anonymous users? [Y/n] y

Disallow root login remotely? [Y/n] y

Remove test database and access to it? [Y/n] y

Reload privilege tables now? [Y/n] y

All done!

To verify and validate that MariaDB is installed and working, log in to the database console using the commands below:

sudo mysql -u root -p

You should automatically be logged in to the database server since we initiated the login request as root. Only the root can log in from the server console without a password.

The server was successfully installed if you see a similar screen.

How to install PHP-FPM on Ubuntu Linux

As mentioned above, we’re installing PHP on Ubuntu since WordPress requires it. PHP packages are added to Ubuntu repositories. The versions of the repositories might not be the latest. If you need to install the latest versions, you’ll need to add a third-party PPA repository.

Run the commands below to a third-party repository with the latest versions of PHP.

sudo apt-get install software-properties-common
sudo add-apt-repository ppa:ondrej/php

At the time of this writing, the latest PHP version is 8.0.

sudo apt update

Next, run the commands below to install PHP 8.0 and related modules.

sudo apt install php8.0-fpm php8.0-common php8.0-mysql php8.0-gmp php8.0-curl php8.0-intl php8.0-mbstring php8.0-xmlrpc php8.0-gd php8.0-xml php8.0-cli php8.0-zip

Once PHP is installed, the commands below can start, stop, and enable PHP-FPM services to start when the server boots automatically.

sudo systemctl stop php8.0-fpm
sudo systemctl start php8.0-fpm
sudo systemctl enable php8.0-fpm

Next, you’ll want to change some great PHP configuration settings with WordPress. Run the commands below to open the PHP default configuration file.

sudo nano /etc/php/8.0/fpm/php.ini

Then, change the line settings to something like the lines below. Save your changes and exit.

file_uploads = On
allow_url_fopen = On
short_open_tag = On
memory_limit = 256M
cgi.fix_pathinfo = 0
upload_max_filesize = 100M
max_execution_time = 360
date.timezone = America/Chicago

How to create a WordPress database on Ubuntu

At this point, we’re ready to create a WordPress database. As mentioned above, WordPress uses databases to store its content.

To create a database for WordPress, run the commands below:

sudo mysql -u root -p

Then, create a database called wpdb

CREATE DATABASE wpdb;

Next, create a database user called wpdbuser and set a password

CREATE USER 'wpdbuser'@'localhost' IDENTIFIED BY 'new_password_here';

Then, grant the user full access to the database.

GRANT ALL ON wpdb.* TO 'wpdbuser'@'localhost' WITH GRANT OPTION;

Finally, save your changes and exit.

FLUSH PRIVILEGES;
EXIT;

How to download WordPress

We’re ready to download WordPress and begin configuring it. First, run the commands below to download the latest version of WordPress from its repository.

Next, extract the downloaded content into a new folder called wordpress.

cd /tmp
wget https://wordpress.org/latest.tar.gz
tar -xvzf latest.tar.gz
sudo mv wordpress /var/www/wordpress

Then, run the command below to allow the www-data user to own the new WordPress directory.

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

How to configure Nginx for WordPress

We have downloaded WordPress content into a new folder called WordPress. Now, let’s configure Nginx to create a new server block for our WordPress website. You can create as many server blocks with Nginx as you want.

To do that, run the commands below to create a new configuration file called WordPress.conf in the /etc/nginx/sites-available/ directory to host our WordPress server block.

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

In the file, copy and paste the content below into the file and save.

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

    client_max_body_size 100M;
    autoindex off;
    location / {
        try_files $uri $uri/ /index.php?$args;
    }

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

Save the file and exit.

After saving the file above, run the commands below to enable the new file that contains our WordPress server block. Restart Nginx after that.

sudo ln -s /etc/nginx/sites-available/wordpress.conf /etc/nginx/sites-enabled/
sudo systemctl restart nginx.service

At this stage, WordPress is ready and can be launched by going to the server’s IP or hostname.

http://localhost

However, we want to protect our server with Let’s Encrypt free SSL certificates. So, continue below to learn how to generate a Let’s Encrypt SSL certificate for websites.

How to set up Let’s Encrypt for WordPress.

We have written a great post on generating and managing Let’s Encrypt SSL certificates for the Nginx web server. You can use that post to apply it here for your WordPress website.

To read the post on how to generate Let’s Encrypt SSL certificates for a website, click on the link below:

How to Setup Let’s Encrypt on Ubuntu Linux with Nginx – Website for Students

If you successfully generate a Let’s Encrypt SSL certificate, you should reopen the server block for our WordPress website by running the commands below.

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

The new WordPress server block configurations should look similar to the line below. Take notes of the highlighted lines.

  • The first server block listens on port 80. It contains a 301 redirect to redirect HTTP to HTTPS.
  • The second server block listens on port 443. It contains a 301 redirect to redirect www to the non-www domain.
server {
    listen 80;
    listen [::]:80;
    root /var/www/wordpress;
    index  index.php index.html index.htm;
    server_name  example.com www.example.com;

    include snippets/well-known.conf;

    return 301 https://$host$request_uri;
}

server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    root /var/www/wordpress;
    index  index.php index.html index.htm;
    server_name www.example.com;
   
    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
    ssl_trusted_certificate /etc/letsencrypt/live/example.com/chain.pem;

    ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3;
    ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384;

    add_header Strict-Transport-Security "max-age=31536000;  includeSubDomains";
    
    include snippets/well-known.conf;

    return 301 https://example.com$request_uri;
}

server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    root /var/www/wordpress;
    index  index.php index.html index.htm;
    server_name example.com;

    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
    ssl_trusted_certificate /etc/letsencrypt/live/example.com/chain.pem;

    ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3;
    ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384;

    ssl_prefer_server_ciphers on;
    ssl_session_cache shared:SSL:50m;
    ssl_session_timeout 1d;
    ssl_session_tickets off;

    ssl_stapling on;
    ssl_stapling_verify on;
    resolver 8.8.8.8 8.8.4.4 valid=300s;
    resolver_timeout 30s;

    ssl_dhparam /etc/ssl/certs/dhparam.pem;

    add_header Strict-Transport-Security "max-age=31536000;  includeSubDomains";
    add_header X-Frame-Options SAMEORIGIN;
    add_header X-Content-Type-Options nosniff;
    
    include snippets/well-known.conf;

    client_max_body_size 100M;

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

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

Save the file above, then restart Nginx and PHP using the commands below.

sudo systemctl reload nginx
sudo systemctl reload php8.0-fpm

Finally, if everything went as planned, you should be able to start the WordPress setup wizard by browsing to the server hostname or IP address over HTTPS.

https://example.com/

A WordPress setup wizard should appear. Follow the wizard to complete the setup.

You will need to know the following items before proceeding. Use the database connection info you created above.

  • Database name
  • Database Username
  • Database password
  • Database host
  • Table prefix (if you want to run more than one WordPress in a single database)

The wizard will use the database information to create a wp-config.php file in the WordPress root folder.

If this automatic file creation doesn’t work for any reason, don’t worry. All this does is fill in the database information to a configuration file. You may also simply open wp-config-sample.php in a text editor, fill in your information, and save it as wp-config.php.

Next, type in the database connection info and click Submit

After that, click the Run the Installation button to have WordPress complete the setup.

Next, create the WordPress site name and the backend admin account, then click Install WordPress.

When you’re done, WordPress should be installed and ready to use.

That should do it!

Conclusion:

This post showed you how to install WordPress on Ubuntu Linux with a link to set up Let’sEncrypt. Please use the comment form below if you find any errors above or have something to add.

Like this:



3 responses to “How to Install WordPress with Nginx on Ubuntu Linux”

  1. Lobo Avatar
    Lobo

    I can’t get https to work, I managed to create the certificate, modified the nginx conf to take it into account, but I cannot load any https page while http is working. Any ideas?

  2. zig Avatar
    zig

    Why so complicated? I remember you had a post with a very simple installation tutorial to follow… I use those two commands below for the SSL setup and it works without problems:

    I simply use:
    >>sudo apt install certbot python3-certbot-nginx
    >>sudo certbot –nginx -d example.com

    Most important it updates automatically. All the other details in this article: https://www.digitalocean.com/community/tutorials/how-to-secure-nginx-with-let-s-encrypt-on-ubuntu-20-04

  3. Deborah Avatar
    Deborah

    Thanks for the post. I installed everything successfully, now when I open my IP and or domain it returns ERROR_TIMEOUT.

    Should I use my IP when setting up mariadb user? i.e: user@IPaddress?

    Im on an instance with remote access.

Leave a Reply to Deborah Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.