跳过正文

Debian 6 + nginx + uwsgi to run Django website

Tech Debian NGINX UWSGI Django
浮世絵空事
作者
浮世絵空事
所谓灵魂 几行代码

On Host
#

Crate debian-archive-keyring
#

apt-get install debian-archive-keyring
apt-get update
apt-get upgraded

Install some tools
#

apt-get install vim
apt-get install emacs

Add new user
#

adduser username

Install sudo tool
#

apt-get install sudo

Change /etc/sudoers
#

vim /etc/sudoers
username ALL=(ALL) NOPASSWD: ALL

Please use this setting carefully

Block root ssh login, change ssh port
#

vim /etc/ssh/sshd_config
Port number
PermitRootLogin no

Restart
#

shutdown -r now

On Host
#

Setting ssh login
#

ssh-copy-id username@host -p port

Login
#

ssh username@host -p port

On VPS
#

Install tools
#

sudo apt-get install gcc g++ make libssl-dev python-dev
sudo apt-get install locate
sudo updatedb

Download sqlite3
#

make & install
#

Download Python-2.7
#

make & install
#

Install Python tools
#

wget --no-check-certificate https://bootstrap.pypa.io/ez_setup.py
sudo python ez_setup.py --insecure
sudo easy_install pip
sudo pip install django

Make a test for django
#

django-admin.py startproject mysite
python manage.py runserver 0.0.0.0:8001

Setting up Django and your web server with uWSGI and nginx

Install uwsgi
#

sudo pip install uwsgi

Basic test

Install nginx
#

wget -O key http://nginx.org/keys/nginx_signing.key && sudo apt-key add key && sudo rm -f key

Append the appropriate stanza to /etc/apt/sources.list.
#

deb http://nginx.org/packages/debian/ squeeze nginx
deb-src http://nginx.org/packages/debian/ squeeze nginx
deb http://nginx.org/packages/debian/ wheezy nginx
deb-src http://nginx.org/packages/debian/ wheezy nginx

and run these command

sudo apt-get update

sudo apt-get install nginx

Test nginx
#

sudo service nginx start

Some config files
#

mysite_uwsgi.ini
#
# mysite_uwsgi.ini file
[uwsgi]

# Django-related settings
# the base directory (full path)
chdir           =  /full/path/to/Django/project
# Django's wsgi file (dir.file ex:/mysite/wsgi.py)
module          = mysite.wsgi
# the virtualenv (full path)
# home            = /path/to/virtualenv

# process-related settings
# master
master          = true
# maximum number of worker processes
processes       = 10
# the socket (use the full path to be safe
socket          = /full/path/to/mysite.sock
# ... with appropriate permissions- may be needed
chmod-socket    = 666
# clear environment on exit
vacuum          = true
mysite_nginx.conf
#
# mysite_nginx.conf

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

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

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

    # Django media
    location /media  {
  	 	# your Django project's media files- amend as required
        alias /full/path/to/Django/mysite/media;
    }

    # Django static
    location /static {
        alias /full/path/to/Django/mysite/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     uwsgi_params; # the uwsgi_params file you installed
    }
}

Add to startup
#

vim /etc/rc.local

Text:

/full/path/to/uwsgi --ini /full/path/to/uwsgi.ini