Configure Nginx on Ubuntu 22.04 to serve WordPress website
11 March 2022 (Updated 27 May 2024)
Clone Git repo on server
SSH into your Ubuntu server and clone the Git repo into a desired location. Let’s assume:
- Git repo is git@github.com:sajadtorkamani/wordpress-deployer-example.git
- Repo location in filesystem is
/home/sajad/sites/wordpress-deployer-example
# /home/sajad/sites/wordpress-deployer-example
git clone git@github.com:sajadtorkamani/wordpress-deployer-example.git
Create Nginx config
sudo vim /etc/nginx/sites-available/wordpress-deployer-example.sajadtorkamani.com
Set its contents to:
server {
listen 80;
server_name wordpress-deployer-example.sajadtorkamani.com www.wordpress-deployer-example.sajadtorkamani.com;
root /home/sajad/sites/wordpress-deployer-example/;
index index.php index.html;
location = /favicon.ico {
log_not_found off;
access_log off;
}
location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}
location / {
try_files $uri $uri/ /index.php?$query_string;
}
error_page 404 /index.php;
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php8.0-fpm.sock; # Change as needed
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
}
location ~ /\.ht {
deny all;
}
}
This config assumes you’ve already configured your DNS records to point wordpress-deployer-example.sajadtorkamani.com
to your server’s public IP address. Also note that if using a long server name like in the above config, you’ll want to edit the Nginx config .
Activate config.
sudo ln -s /etc/nginx/sites-available/wordpress-deployer-example.sajadtorkamani.com /etc/nginx/sites-enabled
Reload nginx.
sudo nginx -s reload
Verify configuration
Now, visit wordpress-deployer-example.sajadtorkamani.com and you should see the WordPress website.
Thanks for your comment 🙏. Once it's approved, it will appear here.
Leave a comment