Use PHP while using Django with Nginx in the same block

I have a Django with Nginx configuration which is working fine, here is my configuration:

  1. server {
  2. listen 80;
  3. server_name;
  4. location / {
  5. proxy_pass;
  6. proxy_set_header Host $host;
  7. proxy_set_header X-Real-IP $remote_addr;
  8. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  9. proxy_set_header X-Forwarded-Proto $scheme;
  10. }
  11. location /static/ {
  12. alias /path/to/your/static/files/;
  13. }
  14. location /media/ {
  15. alias /path/to/your/media/files/;
  16. }
  17. }

I need to add a custom php script on the same domain however it’s not working. For testing purposes, I’ve created a <?php phpinfo(); file but it’s not working.

In the config file I’ve shown above I’ve added

location /custom-php {
    alias /var/www/html/info.php

When I attempt to open the file, the url loads however I get a pop-up asking me where to download the file.

How to resolve this please?

Submit an answer

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!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Site Moderator
Site Moderator badge
April 15, 2023
Accepted Answer


It seems like you don’t have PHP installed on your Droplet and your system doesn’t know what to do with the .php file and it just lets you download it. Additionally, to have Nginx serve your PHP file properly, you need to have a PHP processor like PHP-FPM (FastCGI Process Manager) installed and configured to work with Nginx. Here’s how you can set it up:

  1. Install PHP-FPM:

For Ubuntu/Debian:

  1. sudo apt-get update
  2. sudo apt-get install php-fpm

For CentOS/RHEL:

  1. sudo yum install epel-release
  2. sudo yum install php-fpm
  1. Start and enable PHP-FPM

For Ubuntu/Debian:

  1. sudo systemctl start php7.4-fpm
  2. sudo systemctl enable php7.4-fpm

For CentOS/RHEL:

  1. sudo systemctl start php-fpm
  2. sudo systemctl enable php-fpm
  1. Update your Nginx configuration to process PHP files using PHP-FPM. Replace the location block you have with the following:
location /custom-php{
    root /var/www/;
    try_files $uri /deploy.php$is_args$args;

    location ~ \.php$ {
        include fastcgi_params;
        fastcgi_pass unix:/run/php/php7.4-fpm.sock; # For Ubuntu/Debian
        # fastcgi_pass; # For CentOS/RHEL (uncomment this line and comment out the above line)
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_index index.php;
  1. Restart Nginx to apply the changes:
sudo systemctl restart nginx

Now, when you visit, it should display the PHP file as a webpage. Note that if you’re using a different version of PHP, you’ll need to adjust the version numbers in the paths accordingly.

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel