Are you looking for a detailed guide to install WordPress with Nginx on Ubuntu 22.04/24.04? This article will provide everything you need to set up your own WordPress website quickly and easily.
Prerequisites
This guide focuses on installing WordPress, but before we begin, ensure you have the following:
✅ A Linux system (Ubuntu or any other Linux distribution).
✅ Successfully installed Nginx, PHP, and MySQL or MariaDB.
✅ Root access or a user with sudo privileges.
✅ A stable internet connection to download the required packages.
If you’re ready, let’s proceed with the installation! 🚀
Steps to Install WordPress
Step 1: Download and Prepare WordPress Source Code
💡 Note: If you’re new to this, don’t worry! The way we structure directories will be beneficial if you manage multiple projects in the future. The basic idea is simple: place the WordPress source code inside a specific directory.
1. Download WordPress Source Code
You can download WordPress from the official website: 🔗 Download WordPress
Or, use the wget
command to download it directly from the terminal:
sudo apt install wget cd ~/Downloads wget https://wordpress.org/latest.tar.gz
2. Extract the WordPress Archive
Once the download is complete, extract the latest.tar.gz
file:
tar -xvzf latest.tar.gz
After extraction, you will see a folder named wordpress, which contains all the WordPress source files. For easier management, rename this folder to httpdocs:
mv wordpress httpdocs
3. Create a Directory for the Website
Typically, websites are stored in /var/www/
. You can create a directory for your website using the command below (replace yourdomain.com
with your actual domain name):
sudo mkdir -p /var/www/vhosts/yourdomain.com/
4. Move the WordPress Files to the Website Directory
Now, move the entire httpdocs directory into /var/www/vhosts/yourdomain.com/
. You can either use drag and drop or run:
sudo mv httpdocs /var/www/vhosts/yourdomain.com/
At this point, your WordPress files are ready for the next steps! 🚀
Step 2: Configure Nginx for WordPress
💡 Tip for Localhost Installation
If you don’t have a domain and just want to run WordPress on your local machine (localhost
), you can create a virtual domain (e.g., yourdomain.com
). Follow these steps:
1️⃣ Open the /etc/hosts
file for editing:
sudo nano /etc/hosts
2️⃣ Add this line at the end to map your virtual domain to localhost
:
127.0.0.1 yourdomain.com
3️⃣ Save and exit (Press Ctrl + S, then Ctrl + X).
🔧 Create an Nginx Configuration File for WordPress
Nginx requires a dedicated configuration file for your WordPress website. Create and edit the file using:
sudo nano /etc/nginx/sites-available/yourdomain.com.conf
Now, add the following configuration:
server { listen 80; listen [::]:80; server_name yourdomain.com www.yourdomain.com; return 301 https://yourdomain.com$request_uri; } server { listen 443 ssl http2; listen [::]:443; server_name yourdomain.com www.yourdomain.com; #ssl_certificate /etc/letsencrypt/live/itsmeit.co/fullchain.pem; #ssl_certificate_key /etc/letsencrypt/live/itsmeit.co/privkey.pem; # Path to document root root /var/www/vhosts/yourdomain.com/httpdocs; # File to be used as index index index.php index.html; # Overrides logs defined in nginx.conf, allows per site logs. access_log /var/log/nginx/yourdomain.com.vhost.log; error_log /var/log/nginx/yourdomain.com.vhost.err; location ~ .php$ { try_files $uri /index.php =404; fastcgi_pass unix:/run/php/php8.3-fpm.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; fastcgi_read_timeout 1200; } location / { try_files $uri $uri/ /index.php?$args; } # Rewrite robots.txt rewrite ^/robots.txt$ /index.php last; }
🔹 Important Notes:
- fastcgi_pass is using the PHP-FPM 8.3 socket. If you’re using a different version, adjust the socket path accordingly (e.g.,
php8.2-fpm.sock
). - Update server_name and root with your actual domain name and directory path.
I’ve commented out the two lines #ssl_certificate, which are for the SSL certificate. The website will still run on HTTPS, but there will be a red warning. You can follow the guide to install a free SSL certificate using Certbot [here]. Note that SSL can only be installed on a real VPS, not on localhost. However, there’s a trick: after successfully installing SSL on the VPS, you can download the fullchain.pem and privkey.pem files, and it will work just like it does on the actual server.
🚀 Enable the Configuration and Restart Nginx
After editing the configuration file, you need to enable it and restart Nginx:
sudo ln -s /etc/nginx/sites-available/yourdomain.com.conf /etc/nginx/sites-enabled/ sudo service nginx restart
Now, Nginx is ready to serve your WordPress website! 🎉
Step 3: Create the Database and User
To store data, WordPress needs a database and a MySQL user with proper permissions.
🔹 Log in to MySQL
First, access MySQL using the following command:
sudo mysql
📌 Note: If MySQL requires a root password, use the -p
option when logging in.
sudo mysql -u root -p
🔹 Create the Database and User
Once logged in, execute the following commands to:
✅ Create a new database
✅ Create a MySQL user
✅ Grant necessary permissions
CREATE DATABASE database_name; CREATE USER "username"@"localhost" IDENTIFIED BY "password"; GRANT ALL PRIVILEGES ON database_name.* TO username@localhost IDENTIFIED BY "password"; FLUSH PRIVILEGES; q;
💡 Important Notes:
- Replace
database_name
,username
, andpassword
with actual values. - Avoid using weak passwords to enhance security.
- Run
FLUSH PRIVILEGES;
to apply the changes.
At this point, your database is set up, and you’re ready to connect WordPress to it! 🚀
Step 4: Set Up Your WordPress Website
Now that everything is configured, it’s time to complete the WordPress setup via the web interface.
🔹 Access the WordPress Installation Page
Open your browser and go to the domain you configured (e.g., http://yourdomain.com
).
Click “Let’s go” to start the setup process.
Enter the database details created in Step 3:
✅ Database Name: Your database name
✅ Username: Your MySQL username
✅ Password: Your MySQL password

📌 Note: If you encounter a database connection error, double-check your credentials and try again.
🔹 Configure Your Website Details
On the next screen, enter the following details:
✅ Site Title: Your website’s name
✅ Username: Admin login for WordPress
✅ Password: Secure password for WordPress admin
✅ Email: Your admin email address

📌 Important:
- Save your login details as you will need them to manage your website.
- If you don’t want Google to index your site yet, select “Discourage search engines from indexing this site.”
🎉 Complete the Installation
Once you’ve filled in all the details, click “Install WordPress.” If everything is correct, you’ll see a success message.
Now, log in to the WordPress admin panel using:
👉 http://yourdomain.com/wp-admin
Your WordPress website is now running on Nginx! 🚀
Troubleshooting Common Issues
1️⃣ File Permission Issues: 403 Forbidden or Permission Denied
✅ Solution: Run these commands to set the correct ownership and permissions:
sudo chown -R $USER:www-data /var/www/vhosts/* sudo find /var/www/vhosts/ -type f -exec chmod 644 {} + sudo find /var/www/vhosts/ -type d -exec chmod 755 {} +
2️⃣ Nginx Configuration Errors
❌ Issue: Restarting Nginx fails after editing the configuration file.
✅ Solution: Run:
sudo nginx -t
This will display detailed errors. Fix the issues and test again until Nginx restarts successfully.
You have successfully installed WordPress with Nginx on Ubuntu 22.04! 🚀 If you followed these steps correctly, your website should be up and running smoothly.
Good luck with your WordPress journey! 🎉
References: