Ubuntu 14.04 deploy Django project

1. Purchase a server

  Recommend vultr's server, you can also take a ladder, link: Portal

  Operating system is recommended to choose ubuntu 14.04 64 bit

2. Buy a domain name

  See Baidu for details

3. Install related software

#Create a user named mu 
root@localhost:~ # useradd -m -s /bin/bash mu

#Add the newly created user to the super permission group 
root@localhost:~ # usermod -a -G sudo mu


#Set a password for the new user # Note that there will be no characters displayed when entering the password, then tap 
root@localhost:~ # passwd mu

#Switch to the created new user 
root@localhost:~ # su - mu

#Switch successfully 
mu@localhost:~$

 

   update system

mu@localhost:~$ sudo apt-get update
mu@localhost:~$ sudo apt-get upgrade

  Install Nginx, pip, virtualenv

mu@localhost:~$ sudo apt-get install nginx
mu@localhost:~$ sudo apt-get install git python3 python3-pip
mu@localhost:~$ sudo pip3 install virtualenv

 

   Start Nginx

mu@localhost:~$ sudo service nginx start

 

   Modify the project configuration file as follows:

DEBUG = False
ALLOWED_HOSTS = ['*']
STATIC_URL = ' /static/ ' 
# STATICFILES_DIRS = ( 
#     os.path.join(BASE_DIR,'static'), 
# ) 
#Add the following configuration 
STATIC_ROOT = os.path.join(BASE_DIR, ' static ' )

 

   Generate 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 server may deploy multiple websites, and all website code is placed in the sites/ directory.

home/mu/
    sites/
        tianbaoo.fun/
            env
            blog-sky
        ackblog /
            env
            ackblog
        awmonline/
            env
            AwmOnlin

 

   The above are the files of the three websites, each with its own env and project

   For example how to create tianbaoo.fun The following two are also similar creation methods.

mu@localhost:~$ mkdir -p ~/sites/tianbaoo.fun
mu@localhost:~$ cd ~/sites/tianbaoo.fun
mu@localhost:~/sites/tianbaoo.fun$ virtualenv --python=python3 env

#Pull the project file mu@localhost:~/sites/ tianbaoo.fun 
$ git clone https://github.com/tianbaoo/blog-sky.git

#Activate the virtual environment and install dependencies 
mu@localhost:~/sites/tianbaoo.fun$ source env/bin/ activate
(env) mu@localhost:~/sites/tianbaoo.fun$ cd blog-sky/
(env) mu@localhost:~/sites/tianbaoo.fun/blog-sky$ pip install -r requirements.txt

#Collect static files 
(env) mu@localhost:~/sites/tianbaoo.fun/blog- sky$ python manage.py collectstatic

#Generate database 
(env) mu@localhost:~/sites/tianbaoo.fun/blog- sky$ python manage.py migrate

#Create superuser 
(env) mu@localhost:~/sites/tianbaoo.fun/blog-sky$ python manage.py createsuperuser

 

Fourth, configure Nginx

  First create a new configuration file in the /etc/nginx/sites-available/ directory of the server. I usually set the file name to the domain name. 

/etc/nginx/sites-available/tianbaoo.fun

server {
    charset utf-8;
    listen 80;
    server_name tianbaoo.fun;

    location /static { 
        alias /home/mu/sites/tianbaoo.fun/blog-sky/static; 
    }

    location / { 
        proxy_set_header Host $host;
        # tianbaoo.fun in /tmp/tianbaoo.fun is the folder name 
        proxy_pass in the sites directory http://unix:/tmp/ tianbaoo.fun.socket;
    }
}

 

   When there are multiple website projects, you can write several servers in the above file to listen to different ports, which will be introduced below.

   Establish soft connection

(env) mu@localhost:~/sites/tianbaoo.fun/blog-sky$ sudo ln -s /etc/nginx/sites-available/tianbaoo.fun /etc/nginx/sites-enabled/tianbaoo.fun

 

   Can only see the problem with the Nginx welcome page:

    The configuration in the default default file in the sites-enabled folder overwrites the configuration written by yourself, resulting in the configuration not taking effect. Delete the default file and it will be normal.

  

  install gunicorn

(env) mu@localhost:~/sites/tianbaoo.fun/blog-sky$ pip install gunicorn

 

   Automatically start Gunicorn

   Write a startup script that automatically bootstraps Gunicorn's startup when the server restarts. Scripts are 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-up 
stop on shutdown

respawn

setuid mu
chdir /home/mu/sites/tianbaoo.fun/blog-sky 

exec ../env/bin/gunicorn --bind unix:/tmp/tianbaoo.fun.socket blog-sky.wsgi:application

 

   Start Gunicorn with the start command

(env) mu@localhost:~/sites/tianbaoo.fun/blog-sky$ sudo start gunicorn-tianbaoo.fun

#If the code is updated in the future, just run the following command to restart Nginx and Gunicorn to make the new code take effect: 
sudo service nginx reload
sudo restart gunicorn-tianbaoo.fun

 

 5. Deploy multiple websites on one server

  We now have three website project folders: tianbaoo.fun, ackblog, awmonline, which correspond to their own virtual environment and actual project files

home/mu/
    sites/
        tianbaoo.fun/
            env
            blog-sky
        ackblog /
            env
            ackblog
        awmonline/
            env
            AwmOnlin

 

  We have now successfully run the blog-sky project in the tianbaoo.fun folder, and now we need to add more ackblog and AwmOnline projects.

  Add the ackblog project below

mu@localhost:~$ mkdir -p ~/sites/ackblog
mu@localhost:~$ cd ~/sites/ackblog
mu@localhost:~/sites/ackblog$ virtualenv --python=python3 env

#Pull the project file mu@localhost:~/sites/ ackblog 
$ git clone https://github.com/tianbaoo/ackblog.git

#Activate the virtual environment and install dependencies 
mu@localhost:~/sites/ackblog$ source env/bin/ activate
(env) mu@localhost:~/sites/ackblog$ cd blog-sky/
(env) mu@localhost:~/sites/ackblog/ackblog$ pip install -r requirements.txt

# 收集静态文件
(env) mu@localhost:~/sites/ackblog/ackblog$ python manage.py collectstatic

# 生成数据库
(env) mu@localhost:~/sites/ackblog/ackblog$ python manage.py migrate

# 创建超级用户
(env) mu@localhost:~/sites/ackblog/ackblog$ python manage.py createsuperuser

 

  在 /etc/nginx/sites-available/tianbaoo.fun文件里我们写了第一个项目blog-sky的server

  现在我们同样在里面写入第二个项目ackblog的server信息

# 这个文件里包含了三个项目的server
server {
    charset utf-8;
    listen 80;
    server_name 207.246.124.116;

    location /static {
    }

    location / {
        proxy_set_header Host $host;
        proxy_pass http://unix:/tmp/tianbaoo.fun.socket;
    }
}
server {
    charset utf-8;
    listen 6060;
    server_name 207.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;
    listen 5050;
    server_name 207.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

 

  添加完server之后要在ackblog的虚拟环境中安装gunicorn

(env) mu@localhost:~/sites/ackblog/ackblog$ pip install gunicorn

  设置ackblog的自启动gunicorn脚本

# /etc/init/gunicorn-ackblog.conf

start on net-device-up 
stop on shutdown

respawn 

setuid mu 
chdir /home/mu/sites/ackblog/ackblog

exec ../env/bin/gunicorn --bind unix:/tmp/ackblog.socket ackblog.wsgi:application 

(env) mu@localhost:~/sites/ackblog/ackblog$ sudo start gunicorn-ackblog

  以后如果更新了代码,只要运行下面的命令重启一下 Nginx 和 Gunicorn 就可以使新的代码生效了:

sudo service nginx reload
sudo restart gunicorn-ackblog

  

  第三个项目awmonlin过程步骤和第二个的步骤是一样的,就是有些参数不同

mu@localhost:~$ mkdir -p ~/sites/awmonline
mu@localhost:~$ cd ~/sites/awmonline
mu@localhost:~/sites/awmonline$ virtualenv --python=python3 env

# 拉取项目文件
mu@localhost:~/sites/awmonline$ git clone https://github.com/tianbaoo/AwmOnline.git

# 激活虚拟环境并安装依赖
mu@localhost:~/sites/awmonline$ source env/bin/activate
(env) mu@localhost:~/sites/awmonline$ cd AwmOnline/
(env) mu@localhost:~/sites/awmonline/AwmOnline$ pip install -r requirements.txt

# 收集静态文件
(env) mu@localhost:~/sites/awmonline/AwmOnline$ python manage.py collectstatic

# 生成数据库
(env) mu@localhost:~/sites/awmonline/AwmOnline$ python manage.py migrate

# 创建超级用户
(env) mu@localhost:~/sites/awmonline/AwmOnline$ python manage.py createsuperuser

 

  在 /etc/nginx/sites-available/tianbaoo.fun文件里我们写了第一个项目blog-sky的server和第二个项目ackblog的server

  现在我们同样在里面写入第三个项目AwmOnline的server信息

server {
    charset utf-8;
    listen 80;
    server_name 207.246.124.116;

    location /static {
    }

    location / {
        proxy_set_header Host $host;
        proxy_pass http://unix:/tmp/tianbaoo.fun.socket;
    }
}
server {
    charset utf-8;
    listen 6060;
    server_name 207.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;
    listen 5050;
    server_name 207.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

 

  添加完server之后要在awmonline的虚拟环境中安装gunicorn

(env) mu@localhost:~/sites/awmonline/AwmOnline$ pip install gunicorn

 

  设置awmonline的自启动gunicorn脚本

# /etc/init/gunicorn-awmonline.conf

start on net-device-up 
stop on shutdown

respawn 

setuid mu 
chdir /home/mu/sites/awmonline/AwmOnlin

exec ../env/bin/gunicorn --bind unix:/tmp/awmonline.socket AwmOnlin.wsgi:application 

(env) mu@localhost:~/sites/awmonline/AwmOnlin$ sudo start gunicorn-awmonline

 

  以后如果更新了代码,只要运行下面的命令重启一下 Nginx 和 Gunicorn 就可以使新的代码生效了:

sudo service nginx reload
sudo restart gunicorn-awmonline

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325162806&siteId=291194637