Configure Nginx on macOS to serve WordPress website
11 March 2022 (Updated 9 May 2022)
On this page
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 /usr/local/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/wordpress/wordpress-example-project/;
index index.php;
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?$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 ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
expires max;
log_not_found off;
}
}
Activate config.
ln -s /usr/local/etc/nginx/sites-available/wordpress-example-project.test.conf /usr/local/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
Tagged:
Nginx