How to Install WordPress with Nginx in Ubuntu 22.04

In this tutorial, i am going to show you how to install WordPress with nginx in ubuntu 22.04.

How to Install WordPress with Nginx in Ubuntu 22.04

Use the below given steps to install WordPress with Nginx in ubuntu 22.04:

  • Step 1 – Install and Configure LEMP with Nginx
  • Step 2 – Create Database For WordPress
  • Step 3 – Download WordPress
  • Step 4 – Configure Nginx For WordPress
  • Step 5 – Configure WordPress
  • Step 6 – Complete installing WordPress using GUI

Step 1 – Install and Configure LEMP with Nginx

Install and configure Lemp on ubuntu 22.04 with nginx. if do not know, read the following guide to install and configure LEMP on ubuntu with nginx :-How to Install LEMP Stack Nginx, MySQL, PHP on Ubuntu 22.04.

Step 2 – Create Database For WordPress

Run the following command on the terminal to login into the database server:

mysql -u root -p

Run the following command on terminal to create a database for WordPress:

CREATE DATABASE wordpress;
CREATE USER 'wpuser'@'localhost' IDENTIFIED BY 'EnterStrongPassword';
GRANT ALL ON wordpress.* TO 'wpuser'@'localhost';
FLUSH PRIVILEGES;
exit

And replace the “EnterStrongPassword” with your own database password, which you want to keep.

Step 3 – Download WordPress

Run the following command on terminal to download WordPress on Ubuntu with Nginx:

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

Run the following command on terminal to extract the downloaded WordPress tar file:

tar -zxvf latest.tar.gz --strip-components=1

Run the following command on terminal to remove the archive:

rm -f latest.tar.gz

And use the following command to update permissions:

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

Step 4 – Configure Nginx For WordPress

Run the following command on terminal to create an Nginx configuration file:

nano /etc/nginx/sites-available/example.com

And paste the following (after updating example.com to your domain)

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

    client_max_body_size 500M;

    location / {
        try_files $uri $uri/ /index.php?$args;
    }
	
    location = /favicon.ico {
        log_not_found off;
        access_log off;
    }

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

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

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

And enable the configuration file:

ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/

Disable the default configuration file if you’re not using it:

rm -f /etc/nginx/sites-enabled/default

Run the following command to test and see if everything’s ok with Nginx:

nginx -t

Now restart Nginx and PHP-FPM for the changes to take effect

systemctl restart nginx.service
systemctl restart php8.1-fpm.service

Step 5 – Configure WordPress

There’s a default wp-config file that we need to edit. So execute the following command on command lien to rename the file:

mv /var/www/html/wp-config-sample.php /var/www/html/wp-config.php

Open it:

nano /var/www/html/wp-config.php

And add the following lines with your database information:

define('DB_NAME', 'wordpress');
define('DB_USER', 'wpuser');
define('DB_PASSWORD', 'EnterYourDatabasePassword');

Save and close the file.

For security reasons, you should update the security keys in your wp-config file.

Open the wp-config.php file again:

nano /var/www/html/wp-config.php

And update the following lines with the ones you generated:

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

Save and close the file.

Step 6 – Complete installing WordPress using GUI

Open the browser and hit this URL :- https://example.com and follow the steps to finish installing WordPress.

Conclusion

In this tutorial, i have shown to you how to install WordPress with Nginx in ubuntu 22.04.

More Tutorials

Leave a Comment