自己写的Django项目 一直没试过部署上线 结果今天部署了一天才搞好 踩了不少坑 但是最后还是弄好了!
LNMP详细部署可以查看我另一篇博客
LNMP部署
yum install -y epel-release # 安装EPEL扩展源
yum install -y python36 python36-pip python36-devel sqlite-devel supervisor # 安装Python3.6及其工具组件
[root@maomao scripts]# python3
Python 3.6.8 (default, Nov 16 2020, 16:55:22)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-44)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> print('hello world')
hello world
pip3 install --upgrade pip # 升级pip版本
echo "alias python='/usr/bin/python3.6'" >/etc/profile.d/python.sh
# 添加Python 3.6为系统执行的默认Python
source /etc/profile # 使系统配置生效
这里需要注意的点 你项目的django版本 需要和服务器安装的django版本一样
pip3 list 可以查看版本
比如我是django2.2 因此安装django的时候这样写
pip3 install django==2.2
pip3 install uwsgi
vim test.py
#!/usr/bin/env python3
# -*- coding: UTF-8 -*-
def application(env, start_response):
start_response('200 OK', [('Content-Type','text/html')])
return [b'Hello World']
uwsgi --http :8000 --wsgi-file test.py
在浏览器中输入 网址:8000
,出现 “Hello World”表示安装成功。
[root@maomao home]# ls
studentsSystem.zip test.py
[root@maomao home]# unzip studentsSystem.zip
[root@maomao home]# cd studentsSystem
将数据库和路径都要改成当前环境的
运行所有ip访问
ALLOWED_HOSTS = ['*']
修改数据库信息
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'studentssystem',
'USER': 'root',
'PASSWORD': '123',
'HOST': '47.108.76.25',
'PORT': '3306',
'OPTIONS':{
'init_command': "SET sql_mode='STRICT_TRANS_TABLES';" # 初始化数据库的命令
}
}
}
这里需要注意阿里云的安全组 需要手动配置打开3306端口 不然无法连接数据库
如果还是连接不上可以授权
grant all on *.* to 'root'@'%' identified by '123';
grant all on *.* to 'root'@'localhost' identified by '123';
flush privileges;
pip install pymysql
创建自己项目的库
mysql> create database studentssystem;
执行sql
[root@maomao studentsSystem]# pwd
/home/studentsSystem
python3 manage.py migrate 创建数据表
python manage.py createsuperuser 创建admin用户
查看数据库里自己的项目表
mysql> select * from studentssystem;
ERROR 1146 (42S02): Table 'studentssystem.studentssystem' doesn't exist
mysql> show tables;
+----------------------------+
| Tables_in_studentssystem |
+----------------------------+
| auth_group |
| auth_group_permissions |
| auth_permission |
| auth_user |
| auth_user_groups |
| auth_user_user_permissions |
| course |
| django_admin_log |
| django_content_type |
| django_migrations |
| django_session |
| student |
| studentinformation |
+----------------------------+
13 rows in set (0.00 sec)
[uwsgi]
socket =127.0.0.1:8000
chdir = /home/studentsSystem
module = studentsSystem.wsgi
master = true
processes = 4
vacuum = true
通过 uwsgi 命令读取 uwsgi.ini 文件启动项目
uwsgi --ini uwsgi.ini
vim /usr/local/nginx/conf/nignx.conf
events {
worker_connections 4096;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
gzip on;
upstream django {
server 127.0.0.1:8000;
}
server {
listen 8080;
server_name localhost;
charset utf-8;
location / {
include /etc/nginx/uwsgi_params;
uwsgi_pass django;
}
location /static/ {
expires 30d;
autoindex on;
add_header Cache-Control private;
alias /home/studentsSystem/static/;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
检查语法
nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
STATIC_URL = '/static/'
MEDIA_ROOT = os.path.join(BASE_DIR,'static')
STATICFILES_DIRS = (
('css',os.path.join(MEDIA_ROOT,'css').replace('\\','/') ),
('js',os.path.join(MEDIA_ROOT,'js').replace('\\','/') ),
('images',os.path.join(MEDIA_ROOT,'images').replace('\\','/') ),
('upload',os.path.join(MEDIA_ROOT,'upload').replace('\\','/') ),
)
# 设置图片等静态文件的路径(电脑系统文件夹路径)
STATICFILES_DIRS = (
os.path.join(BASE_DIR, '/home/studentsSystem/static/').replace('\\', '/'),
)
在django项目setting.py最下面添加
STATIC_ROOT = '/home/studentsSystem/static/'
命令行输入
python manage.py collectstatic
自动将所有静态文件复制到nginx的索引目录
uwsgi --ini uwsgi.ini
systemctl start nginx