Ozgur Akcali
Ozgur Akcali

Reputation: 5482

Nginx + Django + Phpmyadmin Configuration

I've migrated my server to amazon ec2, and trying to set up the following environment there:

Nginx in the front serving static content, passing to django for dynamic content. I also would like to use phpmyadmin in this setting.

I am not a server admin, so I simply followed a few tutorials to make nginx and django up and running. But I've been working for two days now trying to hook phpmyadmin to this setup, with no avail. I am sending my current server configuration now, how can I serve phpmyadmin here?

server {
    listen   80;
    server_name localhost;

    access_log /opt/django/logs/nginx/vc_access.log;
    error_log  /opt/django/logs/nginx/vc_error.log;

    # no security problem here, since / is always passed to upstream
    root /opt/django/;
    # serve directly - analogous for static/staticfiles
    location /media/ {
        # if asset versioning is used
        if ($query_string) {
            expires max;
        }
    }
    location /admin/media/ {
        # this changes depending on your python version
        root /path/to/test/lib/python2.7/site-packages/django/contrib;
    }
    location /static/ {
        # if asset versioning is used
        if ($query_string) {
            expires max;
        }
    }
    location / {
        proxy_pass_header Server;
        proxy_set_header Host $http_host;
        proxy_redirect off;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Scheme $scheme;
        proxy_connect_timeout 10;
        proxy_read_timeout 10;
        proxy_pass http://localhost:8000/;
    }
    # what to serve if upstream is not available or crashes
    error_page 500 502 503 504 /media/50x.html;
}

Upvotes: 3

Views: 1626

Answers (1)

Calvin Cheng
Calvin Cheng

Reputation: 36506

This question should rightly belong to http://serverfault.com

Nevertheless, the first thing you ought to do is to configure a separate subdomain for your phpmyadmin for ease of administration.

So there will be two apps running with nginx as reverse proxy, one nginx server for your above django app and another server (also known as virtualhost) for your phpmyadmin with a configuration similar to this:-

server {
         server_name     phpmyadmin.<domain.tld>;
         access_log      /srv/http/<domain>/logs/phpmyadmin.access.log;
         error_log       /srv/http/<domain.tld>/logs/phpmyadmin.error.log;

         location / {
                 root    /srv/http/<domain.tld>/public_html/phpmyadmin;
                 index   index.html index.htm index.php;
         }

         location ~ \.php$ {
                 root            /srv/http/<domain.tld>/public_html/phpmyadmin;
                 fastcgi_pass    unix:/var/run/php-fpm/php-fpm.sock;
                 fastcgi_index   index.php;
                 fastcgi_param   SCRIPT_FILENAME  /srv/http/<domain.tld>/public_html/phpmyadmin/$fastcgi_script_name;
                 include         fastcgi_params;
         }
 }

Each of your server configuration can point at different domain names via the server_name configuration. In this example, server_name phpmyadmin.<domain.tld>;

Here's an example taken from http://wiki.nginx.org/ServerBlockExample

http {
  index index.html;

  server {
    server_name www.domain1.com;
    access_log logs/domain1.access.log main;

    root /var/www/domain1.com/htdocs;
  }

  server {
    server_name www.domain2.com;
    access_log  logs/domain2.access.log main;

    root /var/www/domain2.com/htdocs;
  }
}

As you can see, there are two declarations of server inside the large http brackets. Each declaration of the server should contain the configuration you have for django and another for the configuration of phpmyadmin.

2 "virtual hosts" ("server" instances) taken care by nginx.

Upvotes: 1

Related Questions