Ubuntu 14.04 deployment of Django projects

Source: Internet
Author: User
Tags virtual environment virtualenv git clone

First, purchase the server

Recommended VULTR server, can also take a ladder, Link: Portal

Operating system recommended Ubuntu 14.04 64-bit

Second, the purchase of domain names

Detailed See Baidu

Third, install the relevant software
#Create a user named Mu[Email protected]:~#useradd-m-s/bin/bash mu#to add a newly created user to a Super permission group[Email protected]:~#usermod-a-g sudo mu#set a password for a new user#Note that there will be no character display when the password is lost, then tap[Email protected]:~#passwd mu#switch to the new user created[Email protected]:~#Su-mu#Switch Success[Email protected]:~$

Update system

[Email protected]:~$ sudo apt-get update[email protected]:~$ sudo apt-get upgrade

Installing Nginx, Pip, virtualenv

[Email protected]:~$ sudo apt-get install Nginx[email protected]:~$ sudo apt-get install git python3 python3-  Pip[email protected]:~$ sudo pip3 install virtualenv

Start Nginx

[Email protected]:~$ sudo service nginx start

In the project configuration file, modify the following:

DEBUG == ['*' '/static/'#  Staticfiles_dirs = (#    os.path.join (base_dir, ' static '),#) # Add the following configuration ' Static ')

To build the installation dependency file, open cmd in the manage.py directory and run the following command

Pip Freeze > Requirements.txt

Push the project to GitHub

A single server may deploy multiple Web sites, and all site code is placed under the sites/directory.

home/mu/    Sites/        tianbaoo.fun/            env            blog-Sky        ackblog/             env            ackblog        awmonline/            env            Awmonlin

The above is a file of three websites, each with its own Env and project

Examples of how to create two tianbaoo.fun below are also similar to the creation method.

[Email protected]:~$ mkdir-p ~/sites/Tianbaoo.fun[email protected]:~$ CD ~/sites/Tianbaoo.fun[email protected]:~/sites/tianbaoo.fun$ virtualenv--python=Python3 Env#Pull Project File[Email protected]:~/sites/tianbaoo.fun$ git clone https://github.com/tianbaoo/blog-Sky.git#activating a virtual environment and installing dependencies[Email protected]:~/sites/tianbaoo.fun$ Source env/bin/Activate (env) [email protected]:~/sites/tianbaoo.fun$ CD blog-sky/(env) [email protected]:~/sites/tianbaoo.fun/blog-sky$ Pip Install-R Requirements.txt#collecting static files(ENV) [Email protected]:~/sites/tianbaoo.fun/blog-sky$ python manage.py collectstatic#Build Database(ENV) [Email protected]:~/sites/tianbaoo.fun/blog-sky$ python manage.py migrate#Create Super User(ENV) [Email protected]:~/sites/tianbaoo.fun/blog-sky$ python manage.py createsuperuser

Four, configuration Nginx

First create a new profile in the server's/etc/nginx/sites-available/directory, the file name I usually set as the domain name

/etc/nginx/sites-available/tianbaoo.funserver {    charset UTF-8;     ;    server_name tianbaoo.fun;      /Static {         /home/mu/sites/tianbaoo.fun/blog-sky/static;     }     / {         proxy_set_header Host $host;         # Tianbaoo.fun in/tmp/tianbaoo.fun is the folder name        in the Sites Directory Proxy_pass http://unix:/tmp/tianbaoo.fun.socket;    }}

Multiple Web site projects, in the above file to write more than a few servers to listen to different ports can be described below.

Establishing a soft connection

(ENV) [Email protected]:~/sites/tianbaoo.fun/blog-sky$ sudo ln-s/etc/nginx/sites-available/tianbaoo.fun/etc/nginx/ Sites-enabled/tianbaoo.fun

Only see the Nginx Welcome page problem:

The default file configuration in the Sites-enabled folder overrides the configuration you have written, causing the configuration to not take effect and deleting the default file as normal

  

Installing Gunicorn

(ENV) [Email protected]:~/sites/tianbaoo.fun/blog-sky$ pip Install Gunicorn

Auto Start Gunicorn

Write a startup script that will automatically boot the Gunicorn when the server restarts. The script is located in the/etc/init/directory, and the script file name must end with. conf:

# /etc/init/gunicorn-tianbaoo.fun.conf start on net-device-/home/mu/sites/tianbaoo.fun/blog-exec . /env/bin/gunicorn--bind Unix:/tmp/tianbaoo.fun.socket blog-sky.wsgi:application

Start Gunicorn with the start command

(ENV) [Email protected]:~/sites/tianbaoo.fun/blog-sky$ sudo start gunicorn-tianbaoo.fun#  later if the code is updated, Just run the following command to restart Nginx and Gunicorn to make the new code effective:sudo service Nginx reloadsudo restart Gunicorn-tianbaoo.fun

How far servers deploying multiple Web sites

We now have three Web site project folders: Tianbaoo.fun, Ackblog, awmonline, folders that correspond to their own virtual environments and actual project files

home/mu/    Sites/        tianbaoo.fun/            env            blog-Sky        ackblog/             env            ackblog        awmonline/            env            Awmonlin

Now that we have successfully run the Blog-sky project in the Tianbaoo.fun folder, we are now going to add more ackblog and Awmonline projects.

Add the Ackblog item to the following first

[Email protected]:~$ mkdir-p ~/sites/Ackblog[email protected]:~$ CD ~/sites/Ackblog[email protected]:~/sites/ackblog$ virtualenv--python=Python3 Env#Pull Project File[Email protected]:~/sites/ackblog$ git clone https://github.com/tianbaoo/Ackblog.git#activating a virtual environment and installing dependencies[Email protected]:~/sites/ackblog$ Source env/bin/Activate (env) [email protected]:~/sites/ackblog$ CD blog-sky/(env) [email protected]:~/sites/ackblog/ackblog$ Pip Install-R Requirements.txt#collecting static files(ENV) [Email protected]:~/sites/ackblog/ackblog$ python manage.py collectstatic#Build Database(ENV) [Email protected]:~/sites/ackblog/ackblog$ python manage.py migrate#Create Super User(ENV) [Email protected]:~/sites/ackblog/ackblog$ python manage.py createsuperuser

In the /etc/nginx/sites-available/tianbaoo.fun File We wrote the first project Blog-sky the server

Now we're also writing a second project in the Ackblog server information

#This file contains three items of serverserver {charset UTF-8; Listen80; server_name207.246.124.116; Location/static {} location/{proxy_set_header Host $host; Proxy_pass http:unix:/tmp/Tianbaoo.fun.socket; }}server {charset UTF-8; Listen6060; server_name207.246.124.116; Location/Static {alias/home/mu/sites/awmonline/awmonline/static; } Location/{proxy_set_header Host $host; Proxy_pass http:unix:/tmp/Awmonline.socket; }}server {charset UTF-8; Listen5050; server_name207.246.124.116; Location/Static {alias/home/mu/sites/ackblog/ackblog/static; } Location/{proxy_set_header Host $host; Proxy_pass http:unix:/tmp/Ackblog.socket; }}
View Code

To install Gunicorn in a Ackblog virtual environment after you have finished adding the server

(ENV) [Email protected]:~/sites/ackblog/ackblog$ pip Install Gunicorn

Set the Ackblog self-starting Gunicorn script

# /etc/init/gunicorn-ackblog.conf start on net-device-/home/mu/sites/ackblog/ackblogexec : /env/bin/gunicorn--bind unix:/tmp/ackblog.socket ackblog.wsgi:application (env) [email protected]:~/sites/ ackblog/ackblog$ sudo start gunicorn-ackblog

If you update the code later, just run the following command to restart Nginx and Gunicorn to make the new code effective:

sudo service nginx reloadsudo restart Gunicorn-ackblog

  

  The third project Awmonlin the process steps and the second step are the same, that is, some parameters are different

[Email protected]:~$ mkdir-p ~/sites/Awmonline[email protected]:~$ CD ~/sites/Awmonline[email protected]:~/sites/awmonline$ virtualenv--python=Python3 Env#Pull Project File[Email protected]:~/sites/awmonline$ git clone https://github.com/tianbaoo/Awmonline.git#activating a virtual environment and installing dependencies[Email protected]:~/sites/awmonline$ Source env/bin/Activate (env) [email protected]:~/sites/awmonline$ CD awmonline/(env) [email protected]:~/sites/awmonline/awmonline$ Pip Install-R Requirements.txt#collecting static files(ENV) [Email protected]:~/sites/awmonline/awmonline$ python manage.py collectstatic#Build Database(ENV) [Email protected]:~/sites/awmonline/awmonline$ python manage.py migrate#Create Super User(ENV) [Email protected]:~/sites/awmonline/awmonline$ python manage.py createsuperuser

In /etc/nginx/sites-available/ In the Tianbaoo.fun file, we wrote the first project. Blog-sky Server and second project Ackblog server

And now we're also writing a third project in the Awmonline server information

server {charset UTF-8; Listen80; server_name207.246.124.116; Location/static {} location/{proxy_set_header Host $host; Proxy_pass http:unix:/tmp/Tianbaoo.fun.socket; }}server {charset UTF-8; Listen6060; server_name207.246.124.116; Location/Static {alias/home/mu/sites/awmonline/awmonline/static; } Location/{proxy_set_header Host $host; Proxy_pass http:unix:/tmp/Awmonline.socket; }}server {charset UTF-8; Listen5050; server_name207.246.124.116; Location/Static {alias/home/mu/sites/ackblog/ackblog/static; } Location/{proxy_set_header Host $host; Proxy_pass http:unix:/tmp/Ackblog.socket; }}
View Code

 To install Gunicorn in a awmonline virtual environment after you have finished adding the server

(ENV) [Email protected]:~/sites/awmonline/awmonline$ pip Install Gunicorn

Set the Awmonline self-starting Gunicorn script

# /etc/init/gunicorn-awmonline.conf start on net-device-/home/mu/sites/awmonline/awmonlinexec : /env/bin/gunicorn--bind unix:/tmp/awmonline.socket AwmOnlin.wsgi:application (env) [email protected]:~/ sites/awmonline/awmonlin$ sudo start gunicorn-awmonline

If you update the code later, just run the following command to restart Nginx and Gunicorn to make the new code effective:

sudo service nginx reloadsudo restart Gunicorn-awmonline

Ubuntu 14.04 deployment of Django projects

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.