记录下来是因为每次部署都会让我头秃一段时间。之前一直是用apache+uwsgi,配置apache让我痛不欲生。docker似乎可以快速部署,但我一直没整明白。后来通过mezzanine发现有一个自动部署的方案叫fabric,mezzanine就是用它一键nginx+gunicorn部署。
我打算先手工部署一遍,再研究怎么写fabfile文件。过程基本上是参照油管上的这个教程https://www.youtube.com/watch?v=YnrgBeIRtvo&t=311s
安装 venv
sudo apt install -y python3-venv
建立项目根文件夹
mkdir django-test && cd django-test
建立虚拟环境
python3 -m venv test_env
source test_env/bin/activate
安装gunicorn django
pip install gunicorn
pip install django
新建django项目
django-admin startproject myproject
修改settings
vim myproject/myproject/settings.py
#settings.py
ALLOWED_HOSTS = ['your_ipaddress']
配置gunicorn
mkdir conf
vim conf/gunicorn_conf.py
#gunicorn_conf.py
command = '/home/root/django-test/test_env/bin/gunicorn'
pythonpath ='/home/root/django-test/myproject'
bind = '0.0.0.0:8000'
workers = 2
运行gunicorn
gunicorn -c conf/gunicorn_conf.py myproject.wsgi &
这时访问 http://youripaddress:8000 ,可以看到django欢迎页面
启动nginx
sudo service nginx start
修改STATIC URL
mkdir /home/root/django-test/static
vim myproject/myproject/settings.py
STATIC_URL='/home/root/django-test/static'
配置nginx
vim /etc/nginx/sites-available/myproject
server{
listen 80;
server_name 161.117.49.142;
location /static/{
root /home/root/django-test/static/;
}
location / {
proxy_pass http://161.117.49.142:8000;
}
}
链接
cd /etc/nginx/sites-enabled
sudo ln -s /etc/nginx/sites-available/myproject
ls -l
这时访问 http://youripaddress 就能通过80端口访问了
接下来是添加域名