Nginx – Django (uWSGI) and php (fastcgi) NGINX

djangonginxPHPphp-fpmuwsgi

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;
#}
}

Best Answer

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.