Configure Nginx on macOS to serve WordPress website
11 March 2022 (Updated 17 March 2024)
This post assumes you installed Nginx via Homebrew and that its config lives in /opt/homebrew/etc/nginx. Other installation methods may place the config files elsewhere.
Clone repo
Suppose you have a standard WordPress project like the one in this repo. Clone your repo.
cd ~/sites # or wherever
git clone https://github.com/sajadtorkamani/wordpress-example-project
Create Nginx config
touch /opt/homebrew/etc/nginx/sites-available/wordpress-example-project.test.conf
Set its contents to:
server {
listen 80;
server_name wordpress-example-project.test www.wordpress-example-project.test;
root /Users/sajad/sites/sajadtorkamani/;
client_max_body_size 5M; # allows file uploads up to 5 megabytes
index index.php;
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
include fastcgi_params;
fastcgi_pass 127.0.0.1:9000; # Assuming PHP-FPM is listening on port 9000
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
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|svg)$ {
expires max;
log_not_found off;
}
}
Activate config.
ln -s /opt/homebrew/etc/nginx/sites-available/wordpress-example-project.test.conf /opt/homebrew/etc/nginx/sites-enabled
Test config.
nginx -t
Reload Nginx.
nginx -s reload
Update /etc/hosts and add the following entry.
127.0.0.1 wordpress-example-project.test
Verify configuration
Navigate to http://wordpress-example-project.test and you should see the default WordPress installation page.
Sources
References (1)
Posts that link to this one.