Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I am new to configuring servers and i'm having issues getting php to work with my django app using UWSGI on nginx. Django works great using uwsgi and php works great when I remove the django site conf, but when i use django and php together php files don't work (browser just downloads them), any ideas (please be specific i'm new to this stuff)? I open to suggestions, getting kinda desperate here. Thanks

Here is my django-site conf file for nginx:

# mysite_nginx.conf
####################

# the upstream component nginx needs to connect to
upstream django {
# server unix:///home/appdeploy/uwsgi-tutorial/mysite/mysite.sock; # for a file socket
server 127.0.0.1:8001; # for a web port socket (we'll use this first)
}

# configuration of the server
server {
# the port your site will be served on
listen      80;
# the domain name it will serve for
server_name 173.10.235.37; # substitute your machine's IP address or FQDN
charset     utf-8;

# max upload size
client_max_body_size 75M;   # adjust to taste

# Django media
location /media  {
    alias /home/appdeploy/v1/website/media;
  # your Django project's media files - amend as required
}

location /static {
    alias /home/appdeploy/v1/website/static;
  # your Django project's static files - amend as required
}

# Finally, send all non-media requests to the Django server.
location / {
    uwsgi_pass  django;
    include     /home/appdeploy/v1/website/uwsgi_params; # the uwsgi_params file you             installed
}
error_page  404              /404.html;
location = /404.html {
  root /home/appdeploy/v1; ## or whereever You put it - will not be needed if 404.html    is where index.html is placed
}

}

Here is my default conf file running as well (with the php fast cgi):

# default.conf

server {
listen       80;
server_name  localhost;

#charset koi8-r;
#access_log  /var/log/nginx/log/host.access.log  main;

location / {
    root   /home/appdeploy/v1/website;
    index  index.html index.htm index.php;
}

#error_page  404              /404.html;

# redirect server error pages to the static page /50x.html
#
error_page   500 502 503 504  /50x.html;
location = /50x.html {
    root   /usr/share/nginx/html;
}

# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
#    proxy_pass   http://127.0.0.1;
#}

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
#    root           html;
    fastcgi_pass   127.0.0.1:9000;
    fastcgi_index  index.php;
    fastcgi_param  SCRIPT_FILENAME  /home/appdeploy/v1/website$fastcgi_script_name;
    include        fastcgi_params;
}

# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
#    deny  all;
#}
}
share|improve this question

2 Answers 2

up vote 1 down vote accepted

From what I see in your configuration, the PHP is no longer managed which cause the page to be downloaded.

On the working one, there is a PHP interpreter define here:

location ~ \.php$ {
#    root           html;
    fastcgi_pass   127.0.0.1:9000;
    fastcgi_index  index.php;
    fastcgi_param  SCRIPT_FILENAME  /home/appdeploy/v1/website$fastcgi_script_name;
    include        fastcgi_params;
}

On the new one, there is no specific parameter for php pages. Only for the default root:

location / {
    uwsgi_pass  django;
    include     /home/appdeploy/v1/website/uwsgi_params; # the uwsgi_params file you             installed
}

If you try this configuration instead, it should work:

    location / {
    #uwsgi_pass  django;
    fastcgi_pass   127.0.0.1:9000;
    include     /home/appdeploy/v1/website/uwsgi_params; # the uwsgi_params file you             installed
}

(make sure the fastcgi process listen on port 9000. If it uses a socket, put the socket path instead of 127.0.0.1:9000). Let us know if everything works with this.

Part 2

How is your phpcgi process managed, via file (socket) or web port socket?

I missed something in your post, the nginx configuration;

upstream django {
# server unix:///home/appdeploy/uwsgi-tutorial/mysite/mysite.sock; # for a file socket
server 127.0.0.1:8001; # for a web port socket (we'll use this first)
}

Do a lsof -i:8001, if something is listening, it should be the fastcgi process.

If yes, coul you try with this configuration (add it if it does not appear in the nginx configuration)?

location ~ \.php$ {
fastcgi_pass   127.0.0.1:8001;
fastcgi_index  index.php; 
# or your index name
fastcgi_param  SCRIPT_FILENAME  /home/appdeploy/v1/website$fastcgi_script_name;
include        fastcgi_params;
}

If not, your server may use cgi socket. You can check if the socket file exist:

ps aufx | grep mysite.sock

In this case, uncomment the first line in the upstream and comment the second line:

upstream django {
server unix:///home/appdeploy/uwsgi-tutorial/mysite/mysite.sock; # for a file socket
# server 127.0.0.1:8001; # for a web port socket (we'll use this first)
}

And modify the previous php block for:

location ~ \.php$ {
fastcgi_pass   django;
fastcgi_index  index.php; 
# or your index name
fastcgi_param  SCRIPT_FILENAME  /home/appdeploy/v1/website$fastcgi_script_name;
include        fastcgi_params;
}

This sockets must be started, check if they are in the /etc/init.d/ folder

Let me know if that worked.

share|improve this answer
    
Thank you very much JB for your detailed, prompt response, unfortunately it didn't work for me, django and php won't load, for some reason i don't get a 505/404 but just a blank page, and php files still download. I have tried many possible configurations to try to get this working but same result. –  dylpickle Feb 24 at 2:28
    
Check my last edition –  Neocid Feb 27 at 19:03
    
Thank you!! Sorry I have been out of the country, your advise worked for me. –  dylpickle May 14 at 0:41

I think the problem here is with your server_name configuration entries, which specify the domain name that should be matched to use the particular configuration.

The default configuration with PHP applies only to localhost, while the Django configuration applies to the IP address you have specified.

I assume you want to run PHP / Django on the same virtual host / website. Then you need to apply the PHP location block in the same config as the Django config, and then make sure your server_name setting is either commented out or that it matches your website domain.

Then you have to disable the other configuration.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.