centos7 下通过nginx+uwsgi部署django应用笔记

1.安装python3.6

1.准备工作
  yum update
  yum install gcc
2.获取
wget https://www.python.org/ftp/python/3.6.5/Python-3.6.5.tgz
tar -xzvf Python-3.6.5.tgz -C  /tmp
cd  /tmp/Python-3.6.5/
3.安装
把Python3.6安装到 /usr/local 目录
./configure --prefix=/usr/local
make
make install
4.更改/usr/bin/python链接
ln -s /usr/local/bin/python3.6 /usr/bin/python3(不要替换centos7自带的python,会出现一些问题)

2.安装mariadb

1.安装
sudo yum install mariadb-server
2.启动, 重启,设置自启动,
sudo systemctl start mariadb
sudo systemctl restart mariadb
sudo systemctl enable mariadb

3.设置bind-ip
vim /etc/my.cnf
在 [mysqld]:
下面加一行
bind-address = 0.0.0.0
4.设置密码:
mysqladmin -u root password '123456'

5.设置外部ip可以访问
先进入mysql才能运行下面命令:
    mysql 直接进入就行
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123456' WITH GRANT OPTION;
FLUSH PRIVILEGES;
6.创建数据库,恢复本地数据库备份到服务器
create database mxshop charset utf8;
将本地备份的数据库文件恢复到mariadb
use mxshop;
source mxshop.sql;

3.安装依赖环境

1.安装redis
yum install epel-release
yum install redis
systemctl start redis
systemctl enable redis
ps -ef | grep redis
2.安装虚拟环境和django应用依赖库
yum install python-pip
pip install virtualenv,virtualenvwrapper

编辑.bashrc文件
export WORKON_HOME=$HOME/.virtualenvs
source /usr/local/bin/virtualenvwrapper.sh

source ~/.bashrc

创建虚拟环境并激活: mkvirtualenv venv --python=/usr/bin/python3

mysqlclient安装前需要 yum install python-devel mariadb-devel -y
安装依赖包:pip install -r requirements.txt
python manage.py runserver 这一步没问题再继续

4.安装nginx:

1.安装nginx和开放http服务与端口
https://www.digitalocean.com/community/tutorials/how-to-install-nginx-on-centos-7

yum install nginx
systemctl start nginx
systemctl enable nginx

sudo firewall-cmd --permanet --zone=public --add-service=http
sudo firewall-cmd --permanet --zone=public --add-service=https
sudo firewall-cmd --reload
在浏览器访问设备IP或绑定的域名:出现nginx欢迎页面表示成功

2.创建conf/nginx/mxshop.conf
https://uwsgi-docs.readthedocs.io/en/latest/tutorials/Django_and_nginx.html

项目目录(~/projects/mxshop)创建conf/nginx/mxshop.conf,内容如下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# mxshop.conf

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

# configuration of the server
server {
# the port your site will be served on
listen 80;
# the domain name it will serve for
server example.com; # 设备IP地址或者绑定的域名
charset utf-8;

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

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

location /static {
alias /path/to/your/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 /path/to/your/mysite/uwsgi_params; # the uwsgi_params file you installed
}
}
3. 将该配置文件加入到nginx的启动配置文件中 创建软连接,一定要用绝对路径: sudo ln -s ~/projects/mxshop/conf/nginx/mxshop.conf /etc/nginx/conf.d/ 4.收集静态文件 STATIC_ROOT = os.path.join(BASE_DIR, "static/") python manage.py collectstatic 5.检查nginx语法错误并重启 /etc/nginx/nginx.conf 修改nginx用户为当前登录用户 sudo /usr/sbin/nginx -t systemctl restart nginx

6.虚拟环境安装uwsgi

pip install uwsgi

https://uwsgi-docs.readthedocs.io/en/latest/tutorials/Django_and_nginx.html

1.测试uwsgi
创建hello.py 内容为以下内容
def hello_world(env, start_response):
    start_response('200 OK', [('Content-Type', 'text/plain')])
    return ['Hello, World!\r\n']
uwsgi --http:8000 --wsgi-file hello.py
访问域名:8000端口,返回helloworld
测试django项目
uwsgi --http:8000 --module MxShop.wsgi

重启nginx确定没问题后:
uwsgi --socket 127.0.0.1:800 --module MxShop.wsgi --chmod-socket
访问域名:8000端口,成功再进行下一步
2.uwsgi.ini方式启动
项目目录(~/projects/mxshop)创建conf/uwsgi/mxshop_uwsgi.ini
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
官方文档
# mysite_uwsgi.ini file
[uwsgi]

# Django-related settings
# the base directory (full path)
chdir = /path/to/your/project
# Django's wsgi file
module = project.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 = /path/to/your/project/mysite.sock
# ... with appropriate permissions - may be needed
# chmod-socket = 664
# clear environment on exit
vacuum = true
运行命令: uwsgi --ini ~/projects/mxshop/conf/uwsgi/mxshop_uwsgi.ini &
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
我目前采用的ini文件
# mysite_uwsgi.ini file
[uwsgi]

# Django-related settings
# the base directory (full path)
chdir = /path/to/your/project
# Django's wsgi file
module = project.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 = /path/to/your/project/mysite.sock
socket = 设备IP
# ... with appropriate permissions - may be needed
# chmod-socket = 664
# clear environment on exit
vacuum = true
logto = /tmp/mylog.log

到此为止人工部署完成

可以安装supervisord设置nginx和uwsgi为守护进程,进程挂掉会自动重启
坚持原创技术分享,您的支持将鼓励我继续创作!