部署1:
- 安装包
sudo apt update
apt install mysql-server mysql-client
- 设置远程访问mysql
a) 查找 mysql.cnf
find / -name mysql.cnf
b)注释mysql.cnf文件的bind_address
/etc/mysql/mysql.conf.d
找到[mysqld] skip-name-resolve
# mysql -u root -p root
c)切换到mysql数据库
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '密码' WITH GRANT OPTION;
flush privileges;
- 修改django的配置文件
配置云服务器的数据库 需要对DATABASES中进行配置
a)修改settings.py文件中的DEBUG=FALSE,ALLOWED_HOST=['*']
b)修改urls.py
b1)from django.views.static import serve
url中加入以下配置
url(r'^static/(?P<path>.*)$', serve, {"document_root": settings.STATIC_ROOT}),
url(r'^media/(?P<path>.*)$', serve, {"document_root": settings.MEDIA_ROOT}),
b2)setting中
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
b3)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
- 启动项目
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 uwsgi
- nginx的配置文件中加载自定义的nginx的配置文件
vim /etc/nginx/nginx.conf
在server中加入以下配置:
include /home/app/conf/*.conf;
- 配置自定义的nginx配置文件
server {
listen 80;
server_name 47.92.164.198 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/day11axf0/static/;
expires 30d;
}
}
- 配置uwsgi,名称为uwsgi.ini
[uwsgi]
# variables
projectname = day11axf0
newprojectname = day11axf
base = /home/app
# config
#plugins = python
master = true
#protocol = uwsgi
processes = 4
#env = DJANGO_SETTINGS_MODULE=%(projectname).settings
= %(base)/%(projectname)
module = %(newprojectname).wsgi
socket = 127.0.0.1:8890
logto = %(base)/log/uwsgi.log
启动方式: uwsgi --ini uwsgi.ini