By Brendo Ross
I have a Django with Nginx configuration which is working fine, here is my configuration:
- server {
- listen 80;
- server_name my.hiddendomain.com;
-
- location / {
- proxy_pass http://127.0.0.1:8000;
- proxy_set_header Host $host;
- proxy_set_header X-Real-IP $remote_addr;
- proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
- proxy_set_header X-Forwarded-Proto $scheme;
- }
-
- location /static/ {
- alias /path/to/your/static/files/;
- }
-
- location /media/ {
- alias /path/to/your/media/files/;
- }
- }
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?
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!
Accepted Answer
Hello,
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:
For Ubuntu/Debian:
- sudo apt-get update
- sudo apt-get install php-fpm
For CentOS/RHEL:
- sudo yum install epel-release
- sudo yum install php-fpm
For Ubuntu/Debian:
- sudo systemctl start php7.4-fpm
- sudo systemctl enable php7.4-fpm
For CentOS/RHEL:
- sudo systemctl start php-fpm
- sudo systemctl enable php-fpm
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 127.0.0.1:9000; # 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;
}
}
sudo systemctl restart nginx
Now, when you visit my.hiddendomain.com/custom-php
, 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.
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.