阿里云部署Django详细过程
一.Centos7下部署django项目
1.关闭 iptables 和 selinux(此步骤是为了不影响项目的上传,iptables 与selinux是美国安全局研发的一种安全机制,会阻止文件的上传)
# su - root
# service iptables stop
# setenforce 0
# vi /etc/sysconfig/selinux修改SELINUX=disable
2.添加本地 host DNS
# vi /etc/hosts
127.0.0.1 django.example.com
3.安装 python3.6.5 源及依赖包
# yum install epel-release -y
# yum groupinstall "Development tools" -y
# yum install zlib-devel bzip2-devel openssl-devel ncurses-devel zx-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel -y
4.编译安装 python3.6.5 以及 pip package manager
# wget https://www.python.org/ftp/python/3.6.5/Python-3.6.5.tar.xz --no-check-certificate
# tar xf Python-3.6.5.tar.xz
# cd Python-3.6.5
# ./configure --prefix=/usr/local --with-ensurepip=install --enable-shared LDFLAGS="-Wl,-rpath /usr/local/lib"
# make && make altinstall
5.安装 virtualenv
# pip3.6 install virtualenv
6.配置 Django virtualenv
# mkdir -p /var/www/html/django
# cd /var/www/html/django
# virtualenv -p /usr/local/bin/python3.6 .py3env(.py3env是django运行的虚拟环境,此文件夹可以自己命名)
7.开启 virtualenv python3 环境
# source .py3env/bin/activate
8.在此环境安装 Django 相关模块
# pip install django pymysql
9.安装 apache package(这里应该注意,安装apache服务器应该在虚拟机上安装,不应该在virtualenv python3 环境,否则django项目只能在此环境下运行)
#deactivate
# yum install httpd httpd-devel -y
10.安装 mod_wsgi for python3(这里应该在virtualenv python3 环境下安装)
#source .py3env/bin/activate
# pip install mod_wsgi
11.配置 apache 配置文件
# vi /etc/httpd/conf/httpd.conf
末行添加:
LoadModule wsgi_module "/usr/lib64/httpd/modules/mod_wsgi-py36.cpython-36m-x86_64-linux-gnu.so"
# vi /etc/httpd/conf.d/django.conf(在此步骤,需要更改一些文件)
Alias /static /var/www/html/django/static<Directory /var/www/html/django/static>Require all granted</Directory>
<Directory /var/www/html/django/myproject>
<Files wsgi.py>
Require all granted
</Files></Directory>
WSGIPythonHome "/var/www/html/django/.py3env"
Listen 8080<VirtualHost *:8080>
ServerName django.example.com
WSGIDaemonProcess myproject python-path=/var/www/html/django/.py3env/lib/python3.6/site-pachagesWSGIScriptAlias / /var/www/html/django/myproject/wsgi.py
</VirtualHost>
12.重启 apache 并设置开机自启动
# systemctl restart httpd
# systemctl enable httpd
13.保证 virtualenv python3 环境开启,确保项目在此环境下运行
# source /var/www/html/django/.py3env/bin/activate
14.将项目中的静态文件收集
#python manage.py collectstatic
15。修改wsgi入口文件
# vi myproject/wsgi.py(此处,应把myproject改为自己项目,)
16.数据库表生成
# python manage.py makemigrations
# python manage.py migrate
17.添加ALLOWED_HOSTS
# vi myproject/settings.py
ALLOWED_HOSTS = ['django.example.com'](django.example.com是一个域名,可以改为自己的IP,也可以改为*,即所有网址都可以访问)
18.修改项目属主和权限
# chmod -R 755 /var/www/html
# chown -R apache:apache /var/www/html
二.Centos7安装MySQL8.0
https://blog.csdn.net/su_xingyu/article/details/80287740
三.Apache部署django项目,连接MySQL数据库设置
centos中项目连接mysql数据库进行迁移时,提示需要安装mysqlclient,
如果直接pip install mysqlclient时会报错,此时需要先安装yum install mysql-devel,
再pip install mysqlclient,另外,在你的项目中的__init__.py文件中需要添加
import pymysql
pymysql.install_as_MySQLdb()