部署
部署1: python启动,用于测试和本地开发
安装包
- sudo apt update
- apt install mysql-server mysql-client
设置远程访问mysql
(1) 查找 mysql.conf
find / -name mysql.cnf
(2)注释mysql.cof文件的bind_address
/etc/mysql/mysql.conf.d
(3)切换到mysql数据库
grant all privileges on . to ‘root’@’%’ identified by ‘root123’ with grant option; 设置用户root 和密码root123
flush privileges; 刷新数据
(4) 重启
service mysql restart/start/stop修改django的配置文件
(1) 修改settings.py文件中的DEBUG=FALSE,ALLOWED_HOST=[‘*’]
(2)修改urls.py
(a) from django.views.static import serve
url中加入以下配置
url(r’^static/(?P.*) ′,serve,“documentroot”:settings.STATICROOT),url(r′media/(?P.∗) ′ , s e r v e , “ d o c u m e n t r o o t ” : s e t t i n g s . S T A T I C R O O T ) , u r l ( r ′ m e d i a / ( ? P . ∗ ) ’, serve, {“document_root”: settings.MEDIA_ROOT}),
(b)setting中
STATIC_ROOT = os.path.join(BASE_DIR, ‘static’)
(c)url中修改首页访问的地址
url(r’^$’, views.home)修改首页的启动地址
修改工程目录中的url ,并修改url(r’^$’, views.home)安装pip3
apt install python3-pip安装必备库
pip3 install django==1.11
pip3 install pymysql
pip3 install Pillow查看进程
netstat -lntp上传项目,以及数据库
- 在home创建app文件,将项目上传。
- 并将django里的settings里的database,修改
启动项目
python3 manage.py runserver 0.0.0.0:80
部署2:
(使用nginx+uwsgi配置django项目)
安装nginx
sudo apt-get install nginx查看nginx的状态
systemctl status nginx 查看nginx的状态
systemctl start/stop/enable/disable nginx 启动/关闭/设置开机启动/禁止开机启动
service nginx status/stop/restart/start安装uwsgi
pip install uwsginginx的配置文件中加载自定义的nginx的配置文件
vim /etc/nginx/nginx.conf
在server中加入以下配置:
include /home/app/conf/*.conf;配置自定义的nginx配置文件,在自己的项目conf中,新建appnginx.conf,配置
server {
listen 80;
server_name 47.106.171.59 localhost;
access_log /home/app/log/access.log;
error_log /home/app/log/error.log;
location / {
include uwsgi_params;
uwsgi_pass 127.0.0.1:8890;
}
location /static/ {
alias /home/app/axf/static/;
expires 30d;
}
}
6.配置uwsgi,名称为uwsgi.ini,和上面的在同一文件夹下配置
[uwsgi]
# variables
projectname = axf
newprojectname = axf
base = /home/app
# config
#plugins = python
master = true
#protocol = uwsgi
processes = 4
#env = DJANGO_SETTINGS_MODULE=%(projectname).settings
pythonpath = %(base)/%(projectname)
module = %(newprojectname).wsgi
socket = 127.0.0.1:8890
logto = %(base)/log/uwsgi.log这里写代码片
7.配置6的简化版
[uwsgi]
master = true
processes = 4
pythonpath = /home/app/axf/
module = axf.wsgi
socket = 127.0.0.1:8890
logto = /home/app/log/uwsgi.log
8.启动方式: uwsgi –ini uwsgi.ini