Nginx + uWSGI deploy django blog project

uwsig installation test

Installation uwsgi

sudo python -m pip install uwsgi

note:

1) installed in the system environment, the non-virtual environment
2) using the corresponding version Installation python
3) developing the first installation package python

Uwsgi test is normal

New test.py files in home / ubuntu directory, as follows:

def application(env, start_response):
    start_response('200 OK', [('Content-Type','text/html')])
    print("hello world")
    return [b"Hello World"]

Terminal run:

sudo uwsgi --http :8000 --wsgi-file /home/ubuntu/test.py

Browser and enter http: // IP: 8001 can print out the installation is successful

Note: You need to open port 8001 before the normal access

uwsgi http protocol write access django site

Run the following command can test their projects

sudo uwsgi --http :8001 --chdir 项目路径 --虚拟运行环境路径 --module mysite.wsgi:application

Browser and enter http: // IP: 8001 can be a normal visit

Virtual operating environment to build

1. python install a virtual environment (virtualenv):

sudo python -m pip install virtualenv

2. Create a virtual operating environment mysite_env

进入home目录:        cd /home
创建虚拟运行环境:     virtualenv mysite_env

3. virtual runtime environment commonly used commands

启动虚拟环境: source mysite_env/bin/activate
退出虚拟环境: deactivate

4. The local environment of the installation depends mysqlclient

sudo apt-get install python3-dev libmysqlclient-dev

5. Enter the virtual environment to install Python packages

# 切换root账号
sudo su root

# 启动虚拟运行环境
source mysit_env

# 通过pip安装博客项目的Python依赖包
pip install django-ckeditor==5.4.0
pip install Django==2.0
pip install Pillow==5.0.0
pip install pytz==2017.3
pip install mysqlclient

installation and configuration nginx

Install nginx

If apache installed, you first have to shut down apache service (apache2ctl stop)

# 更新软件仓库
sudo apt-get update        
 
# 安装nginx
sudo apt-get install nginx

Remove default

sudo rm -rf /etc/nginx/sites-enabled/default

Enter sites-available create a new configuration

cd /etc/nginx/sites-available/
sudo vim mysite.conf

Configure the following:

server {
    listen 80;
    server_name mysite;
    charset utf-8;                                # 编码格式
 
    client_max_body_size 75M;                     # 最大文件大小限制  
 
    location /favicon.ico {
        alias /home/mysite/favicon.ico;           # favicon.ico路径     
    }
 
    location /static {
        alias /home/mysite/static;                # 静态文件路径
    }
 
    location /media {
        alias /home/mysite/media;                 # 图片文件路径
    }
 
    location / {
        uwsgi_pass 127.0.0.1:8001;                # 本地监听端口
        include /etc/nginx/uwsgi_params;    
    }
}

Set soft links to sites-enabled

sudo ln -s /etc/nginx/sites-available/mysite.conf /etc/nginx/sites-enabled/mysite.conf

Configuring uwsgi, create ini file

[uwsgi]
chdir = /home/mysite                         # django项目路径
home = /home/mysite_env                      # 虚拟环境路径
module = mysite.wsgi:application
master = True
processes = 4                                # 进程数
harakiri = 60                                # 超时时长
max-requests = 5000                          # 最大请求数
socket = 127.0.0.1:8001                    
uid = 1000
gid = 2000
pidfile = /home/mysite_uwsgi/master.pid
daemonize = /home/mysite_uwsgi/mysite.log
vacuum = True

Start uwsgi

启动:sudo uwsgi --ini /home/mysite_uwsgi/mysite.ini
查看uwsgi进程数:ps -aux | grep uwsgi

 

Test nginx

命令:sudo nginx -t

Ok no abnormal returns, otherwise check their profiles

 

Restart nginx

sudo service nginx restart

Collect static files

Some static files are not collected, resulting in some page styles are not

Found settings.py, the top with STATIC_URL STATIC_ROOT = os.path.join (BASE_DIR, 'static_collected'), preservation

Start the virtual environment:

启动: source /home/mysite_env/bin/activate

Collect static files:

python /home/mysite/manage.py collectstatic

Upload modify folder permissions

sudo chmod -R 777 media

Other commonly used commands

正常关闭uwsgi进程:sudo uwsgi --stop /home/mysite_uwsgi/master.pid

强制关闭全部uwsgi进程:sudo ps -aux | grep uwsgi |awk '{print $2}'|xargs kill -9

重新加载uwsgi:sudo uwsgi --reload /home/mysite_uwsgi/master.pid

 

Blog website source

Personal blog site

Published 59 original articles · won praise 19 · views 10000 +

Guess you like

Origin blog.csdn.net/weixin_43507959/article/details/101593284
Recommended