By affyboy
The site runs Vue.JS as the root APP with a Django API
I’m wanting to run wordpress at mydomain.com/blog – as a sub folder
I have followed the DigitalOcean install document for PHP and MySQL (Nginx was already installed). Apache is not used and not installed
The site will server txt or HTML files eg https://mydomain.com/blog/readme.html but if I try to access https://mydomain.com/blog/test.php or https://mydomain.com/blog/index.php it just downloads and does not execute the php.
I have checked the nginx server blocks and tested php is loaded - and reloaded everything - rebooted server etc – but still no luck.
Here is my server block
server {
server_name mydomain.com www.mydomain.com 123.456.789.123;
root /var/www/html/vue/mydomain/dist;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
alias /home/mydomain/src;
}
location / {
try_files $uri $uri/ /index.html /index.php;
}
location ^~ /rest-auth/ {
include proxy_params;
proxy_pass http://unix:/run/gunicorn.sock;
}
location ^~ /api/ {
include proxy_params;
proxy_pass http://unix:/run/gunicorn.sock;
}
location ~ /\.ht {
deny all;
}
location ^~ /admin {
include proxy_params;
proxy_pass http://unix:/run/gunicorn.sock;
}
location ^~ /blog {
alias /var/www/html/blog;
index index.php index.html index.htm;
try_files $uri =404;
}
# For security reasons, set php settings at root level.
# This prevents root level php files from showing as plain text.
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
}
error_log /var/log/nginx/mydomain-error.log;
access_log /var/log/nginx/mydomain-access.log;
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/mydomain.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/mydomain.com/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}
server {
if ($host = www.mydomain.com) {
return 301 https://$host$request_uri;
} # managed by Certbot
if ($host = mydomain.com) {
return 301 https://$host$request_uri;
} # managed by Certbot
listen 80;
server_name mydomain.com www.mydomain.com 123.456.789.123;
return 404; # managed by Certbot
}
Thanks for any help it would be so appreciated – taken 6 hours so far and tried so many things and read lots of articles but nothing works – thank you.
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
Hi @affyboy,
It’s possible this is an issue with your PHP-FPM config. Check it out, see if it’s working properly, you can configure it listen to a port and redirect the requests there just to make sure everything is being executed as expected.
The issue lies in the Nginx configuration for handling PHP files within the /blog subfolder. Specifically, the PHP files in /blog are not being passed to PHP-FPM for processing. Instead, they are served as static files, which causes the browser to download them.
Here’s how you can adjust your Nginx server block to ensure PHP files under /blog are executed correctly:
server {
server_name mydomain.com www.mydomain.com 123.456.789.123;
root /var/www/html/vue/mydomain/dist;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
alias /home/mydomain/src;
}
location / {
try_files $uri $uri/ /index.html /index.php;
}
location ^~ /rest-auth/ {
include proxy_params;
proxy_pass http://unix:/run/gunicorn.sock;
}
location ^~ /api/ {
include proxy_params;
proxy_pass http://unix:/run/gunicorn.sock;
}
location ~ /\.ht {
deny all;
}
location ^~ /admin {
include proxy_params;
proxy_pass http://unix:/run/gunicorn.sock;
}
location ^~ /blog {
root /var/www/html; # Ensure correct root for the blog
index index.php index.html index.htm;
try_files $uri $uri/ /index.php;
# Pass PHP files to PHP-FPM
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
# PHP settings at the root level
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
error_log /var/log/nginx/mydomain-error.log;
access_log /var/log/nginx/mydomain-access.log;
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/mydomain.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/mydomain.com/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}
server {
if ($host = www.mydomain.com) {
return 301 https://$host$request_uri;
} # managed by Certbot
if ($host = mydomain.com) {
return 301 https://$host$request_uri;
} # managed by Certbot
listen 80;
server_name mydomain.com www.mydomain.com 123.456.789.123;
return 404; # managed by Certbot
}
Updated /blog Location Block:
/blog are handled by PHP-FPM.root as /var/www/html since the alias directive caused issues with relative paths.fastcgi_param Configuration:
SCRIPT_FILENAME to pass the correct file path to PHP-FPM.Fallback for /blog/index.php:
try_files $uri $uri/ /index.php; to ensure requests fall back to index.php.After making these changes, restart Nginx to apply the configuration:
sudo nginx -t # Test configuration for errors
sudo systemctl reload nginx
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.