python项目发布前本地准备
修改setting.py
DEBUG = False
导出项目需要的依赖包
pip3 freeze > requirements.txt
创建 uwsgi9009.ini
[uwsgi]
#http = :8080
socket = :9009
#项目路径
chdir= 服务器放置的项目路径
# uwsgi的文件
wsgi-file= 项目名称/wsgi.py
# 虚拟环境
# virtualenv = /root/env
# 进程个数
processes = 2
# 线程个数
threads=2
# 后台启动,指定日志的输出
# daemonize=/data/crm/django.log
# 清除临时文件
vacuum = true
# python文件发生改变自动重启
py-autoreload=1
讲项目传送到服务器
scp 本地目录 name@ip:服务器目录
服务端环境准备
查看python3版本
python3 --version
查看pip3版本
pip3 --version
查看uwsgi版本
uwsgi --version
查看nginx版本
nginx --version
安装PyMySQL
pip3 install PyMySQL
安装MySQL
wget -i -c http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm
yum -y install mysql57-community-release-el7-10.noarch.rpm
yum -y install mysql-community-server
以上如果木有请自行安装
安装项目需要的依赖
安装以来
pip3 install -r requirements.txt
测试python是否正常运行(请务必开通相应端口)
python3 manage.py runserver 0.0.0.0:9009
服务器项目准备
sqlite准备
python3 manage.py makemigrations
python3 manage.py migrate
python3 manage.py createsuperuser
MySQL准备
systemctl start mysqld.service #启动MySQL
systemctl enable mysqld.service #配置开机自启
systemctl status mysqld.service #查看运行状态
grep "password" /var/log/mysqld.log #日志种找出密码
mysql -uroot -p密码 #根据找到的密码进入数据库
ALTER USER 'root'@'localhost' IDENTIFIED BY 'new password'; #修改密码
systemctl restart mysqld #重启服务
exit #退出mysql
create database webDB default character set utf8 collate utf8_general_ci; #创建库utf8
drop database webDB #删除库
########备用命令 ###########
####开启mysql的远程访问
#允许所有ip访问,以root用户形式,**如果所有用户可以将root改为:***
grant all privileges on *.* to 'root'@'192.168.0.1' identified by 'password' with grant option;
flush privileges;
####如果要修改为root这样的弱密码,需要进行以下配置
查看密码策略:show variables like '%password%';
修改密码策略:vi /etc/my.cnf
#添加validate_password_policy配置
validate_password_policy=0
#关闭密码策略
validate_password = off
######未找到匹配的参数: mysql-community-server
yum module disable mysql
资源准备:
python3 manage.py collectstatic
配置 nginx(请务必开通80端口)
server {
listen 80 default_server;
listen [::]:80 default_server;
#需要修改
server_name 服务器地址;
root /usr/share/nginx/html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
#需要添加
include uwsgi_params;
#需要添加
uwsgi_pass 0.0.0.0:9009;
}
#需要添加
location /static{
alias /home/rong/src/weBank/static/;
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
启动服务
启动nginx
nginx
启动项目
nohup uwsgi --ini /etc/uwsgi.ini #后面要改成你自己的uwsgi路径
重启nginx
nginx -s reload
遇到的问题
- 端口没有开通导致访问不到
- nginx资源设置错误导致访问不到资源(not /static/ is /static)
- 项目权限问题导致访问资源403(不要在root和子账户之前切换使用~)