部署方式:Nginx代理+uwsgi应用服务
操作系统:CentOS7
Django项目:https://github.com/zelinhehe/mysite.git为例:
1. 安装 Python3.6
获取安装包
wget https://www.python.org/ftp/python/3.6.2/Python-3.6.2.tgz
tar -xzvf Python-3.6.2.tgz -C /tmp
cd /tmp/Python-3.6.2/
安装到 /usr/local 目录
./configure --prefix=/usr/local
make
make altinstall
更改/usr/bin/python链接
ln -s /usr/local/bin/python3.6 /usr/bin/python3
2. MariaDB
安装 sudo yum install mariadb-server
启动 sudo systemctl start mariadb
设置bind-ip
vim /etc/my.cnf 在 [mysqld]下添加
bind-address = 0.0.0.0
设置访问权限
mysql命令直接进入mysql交互环境,运行下面命令:
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123456' WITH GRANT OPTION;
GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' IDENTIFIED BY '123456' WITH GRANT OPTION;
FLUSH PRIVILEGES;
4. 安装virtualenvwrapper
yum install python-setuptools python-devel
pip install virtualenvwrapper
在.bashrc文件中,添加
export WORKON_HOME=$HOME/.virtualenvs
source /usr/local/bin/virtualenvwrapper.sh(此路径可通过 which virtualenvwrapper.sh查看)
重新加载.bashrc文件
source ~/.bashrc
新建虚拟环境 mkvirtualenv mysite-env
进入虚拟环境 workon mysite-env
安装Django项目依赖
在开发环境通过 pip freeze > requirements.txt 将mysite的虚拟环境安装包导出来
在部署服务器的虚拟环境中运行:pip install -r requirements.txt
5. uwsgi
安装
pip install uwsgi
测试uwsgi
uwsgi --http :8000 --module mysite.wsgi
6. Nginx
安装
https://www.digitalocean.com/community/tutorials/how-to-install-nginx-on-centos-7
配置
新建mysite_nginx.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_name 你的ip地址 ; # substitute your machine's IP address or FQDN
charset utf-8;
# max upload size
client_max_body_size 75M; # adjust to taste
# Django media
location /media {
alias /root/mysite/media; # 指向django的media目录
}
location /static {
alias /root/mysite/static; # 指向django的static目录
}
# Finally, send all non-media requests to the Django server.
location / {
uwsgi_pass django;
include uwsgi_params; # the uwsgi_params file you installed
}
}
将该配置文件加入到nginx的启动配置文件中
sudo ln -s /root/mysite/mysite_nginx.conf /etc/nginx/conf.d/
7. 拉取所有需要的static file 到同一个目录
在django的setting文件中,添加下面一行内容:
STATIC_ROOT = os.path.join(BASE_DIR, "static/")
运行命令 python manage.py collectstatic
8. 重启Nginx
如果systemctl restart nginx 有权限问题,则直接用nginx命令启动 sudo /usr/sbin/nginx
9. 配置并启动uwsgi
新建uwsgi.ini 配置文件, 内容如下:
# mysite_uwsgi.ini file
[uwsgi]
# Django-related settings
# the base directory (full path)
chdir = /root/mysite
# Django's wsgi file
module = mysite.wsgi
# the virtualenv (full path)
virtualenv = /root/.virtualenvs/mysite-env
# process-related settings
# master
master = true
# maximum number of worker processes
processes = 10
# the socket (use the full path to be safe
socket = 127.0.0.1:8000
# ... with appropriate permissions - may be needed
# chmod-socket = 664
# clear environment on exit
vacuum = true
logto = /tmp/mylog.log
注:
chdir: 表示需要操作的目录,也就是项目的目录
module: wsgi文件的路径
processes: 进程数
virtualenv:虚拟环境的目录
运行uwsgi uwsgi -i /root/mysite/uwsgi.ini &
10.访问
http://{IP}/polls/
Q:
安装mysqlclient出问题
centos 7: yum install python-devel mariadb-devel -y
ubuntu: sudo apt-get install libmysqlclient-dev
然后: pip install mysqlclient