1、安装python基础环境
[root@nginx ~]# yum -y install openssl-devel bzip2-devel expat-devel gdbm-devel readline-devel sqlite-devel gcc gcc-c++ openssl-devel zlib zlib-devel python3 python3-devel
2、安装Django框架、uwsgi
[root@nginx ~]# pip3 install -i https://mirrors.aliyun.com/pypi/simple/ --upgrade pip
[root@nginx ~]# pip3 install -i https://mirrors.aliyun.com/pypi/simple/ django==2.1.8
[root@nginx ~]# pip3 install -i https://mirrors.aliyun.com/pypi/simple/ uwsgi
3、下载pythonav项目包,安装项目所需要的依赖包
[root@nginx ~]# unzip pythonav.zip -d /opt/
## 需要下载的依赖在项目目录下的requirements.txt文件中记录
[root@nginx ~]# cat /opt/pythonav/requirements.txt
certifi==2018.11.29
chardet==3.0.4
Django==1.11
idna==2.8
Pillow==5.4.1
PyMySQL==0.9.3
pytz==2018.9
requests==2.21.0
urllib3==1.24.1
## 直接使用此文件下载
[root@nginx ~]# pip3 install -i https://mirrors.aliyun.com/pypi/simple/ -r /opt/pythonav/requirements.txt
4、通过uwsgi的方式运行项目
[root@nginx ~]# uwsgi --ini /opt/pythonav/pythonav_uwsgi.ini
[uWSGI] getting INI configuration from /opt/pythonav/pythonav_uwsgi.ini
[root@nginx ~]# netstat -lntp | grep 8811
tcp 0 0 127.0.0.1:8811 0.0.0.0:* LISTEN 2838/uwsgi
5、修改配置nginx
[root@nginx ~]# vim /etc/nginx/conf.d/test.conf
server {
listen 80;
server_name www.test.org;
client_max_body_size 100M;
location / {
uwsgi_pass 127.0.0.1:8811;
include uwsgi_params;
}
}
## 重新加载
[root@nginx ~]# systemctl reload nginx
6、修改配置、连接数据库
[root@nginx ~]# vim /opt/pythonav/pythonav/settings.py
# ################### MySQL ###################
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'pythonav',
'USER': 'test',
'PASSWORD': '111111',
'HOST': '192.168.200.127',
'PORT': '3306',
}
}
7、创建数据库远程用户、库
MariaDB [(none)]> grant all on *.* to test@'192.168.200.%' identified by '111111';
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> create database pythonav;
Query OK, 1 row affected (0.00 sec)
8、初始化数据库
[root@nginx pythonav]# python3 manage.py makemigrations
Migrations for 'rbac':
rbac/migrations/0001_initial.py
- Create model Menu
- Create model Permission
- Create model Role
Migrations for 'web':
web/migrations/0001_initial.py
- Create model Admin
- Create model Book
- Create model Chapter
- Create model Index
- Create model Module
- Create model Question
- Create model Recommend
- Create model Teacher
- Create model TextBook
- Create model UserAnswer
- Create model UserInfo
- Create model Video
- Add field user to useranswer
[root@nginx pythonav]# python3 manage.py migrate
System check identified some issues:
WARNINGS:
?: (mysql.W002) MySQL Strict Mode is not set for database connection 'default'
HINT: MySQL's Strict Mode fixes many data integrity problems in MySQL, such as data truncation upon insertion, by escalating warnings into errors. It is strongly recommended you activate it. See: https://docs.djangoproject.com/en/1.11/ref/databases/#mysql-sql-mode
Operations to perform:
Apply all migrations: admin, auth, contenttypes, rbac, sessions, web
Running migrations:
Applying contenttypes.0001_initial... OK
Applying auth.0001_initial... OK
Applying admin.0001_initial... OK
Applying admin.0002_logentry_remove_auto_add... OK
Applying contenttypes.0002_remove_content_type_name... OK
Applying auth.0002_alter_permission_name_max_length... OK
Applying auth.0003_alter_user_email_max_length... OK
Applying auth.0004_alter_user_username_opts... OK
Applying auth.0005_alter_user_last_login_null... OK
Applying auth.0006_require_contenttypes_0002... OK
Applying auth.0007_alter_validators_add_error_messages... OK
Applying auth.0008_alter_user_username_max_length... OK
Applying rbac.0001_initial... OK
Applying sessions.0001_initial... OK
Applying web.0001_initial... OK
9、将项目中的静态资源存储到指定位置中
[root@nginx pythonav]# python3 manage.py collectstatic
10、重修nginx配置、重新加载
[root@nginx pythonav]# vim /etc/nginx/conf.d/test.conf
server {
listen 80;
server_name www.test.org;
client_max_body_size 100M;
location / {
uwsgi_pass 127.0.0.1:8811;
include uwsgi_params;
}
location /static {
alias /opt/pythonav/static;
}
}
[root@nginx pythonav]# systemctl reload nginx.service
11、重启uwsgi
[root@nginx pythonav]# kill $(ps -ef | grep uwsgi | grep -v grep | awk '{print $2}')
[root@nginx pythonav]# uwsgi --ini /opt/pythonav/pythonav_uwsgi.ini
[uWSGI] getting INI configuration from /opt/pythonav/pythonav_uwsgi.ini
[root@nginx pythonav]# netstat -lntp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 127.0.0.1:8811 0.0.0.0:* LISTEN 3228/uwsgi
12、浏览器访问