安装环境
-
Remote: CentOS 7.4 x64 (django.example.com)
-
Python: Python3.6.5
-
Apache: Apache 2.4.6
-
Mod_wsgi: 4.6.4
-
Django: Django 2.0.4
一. 系统环境配置
1.关闭 iptables 和 selinux
# su - root
# service iptables stop
# setenforce 0
# vi /etc/sysconfig/selinux
修改
SELINUX=disabled
2.添加本地 host DNS
# vi /etc/hosts
127.0.0.1 django.example.com
二. Python 配置
1.安装 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
2.编译安装 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
3.安装 virtualenv
# pip3.6 install --upgrade pip
# pip3.6 install virtualenv
三. Django 环境配置
1. 配置 Django virtualenv
# mkdir -p /var/www/html/django
# cd /var/www/html/django
# virtualenv -p /usr/local/bin/python3.6 .py3env
2. 开启 virtualenv python3 环境
# source .py3env/bin/activate
3. 在此环境安装 Django 相关模块
# pip install django pymysql
四. Apache 配置
1. 安装 apache package
# yum install httpd httpd-devel -y
2.安装 mod_wsgi for python3
Tip:这里其实是一个远古巨坑, 网上 90% 以上资料的会粗心的直接使用 yum install mod_wsgi 去安装 apache mod_wsgi 模块, 这样做其实最终 mod 模块会调用本地默认的 python2 的所有库文件, 无论你后面如何配置 django 入口文件, apache 都不会使用我们配置的 virutalenv 下隔离的 python3, 导致 apache 无法调用 python3 而报错. 这里小伙伴要注意哦.
# pip install mod_wsgi
3.导出 apache 所需的 mod_wsgi 模块
# mod_wsgi-express install-module
LoadModule wsgi_module "/usr/lib64/httpd/modules/mod_wsgi-py36.cpython-36m-x86_64-linux-gnu.so"WSGIPythonHome "/var/www/html/.py3env"
4.配置 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-pachages
WSGIScriptAlias / /var/www/html/django/myproject/wsgi.py
</VirtualHost>
5.重启 apache 并设置开机自启动
# systemctl restart httpd
# systemctl enable httpd
五. Django 项目配置
1. 保证 virtualenv python3 环境开启
# source /var/www/html/django/.py3env/bin/activate
2.创建一个 Django 项目
# cd /var/www/html/django/
# django-admin startproject myproject .
3.添加 static 目录
# vi myproject/settings.py
末行添加:
STATIC_ROOT = os.path.join(BASE_DIR, "static/")
4.创建本地 SQLlite 文件
Tip:这里使用 SQLlite 代替其他数据库作为我们项目的 DB
# ./manage.py makemigrations
# ./manage.py migrate
Operations to perform:
Apply all migrations: admin, auth, contenttypes, sessions
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 auth.0009_alter_user_last_name_max_length... OK
Applying sessions.0001_initial... OK
5.创建项目管理员账户
# ./manage.py createsuperuser
Username (leave blank to use 'root'): root
Email address: admin@admin.com
Password:
Password (again):
Superuser created successfully.
6.生成项目静态文件目录
# ./manage.py collectstatic
7.修改 wsgi 入口文件
# vi myproject/wsgi.py
import os
import sys
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myproject.settings")
sys.path.append('/var/www/html/django')
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
8.添加ALLOWED_HOSTS
# vi myproject/settings.py
Update:
ALLOWED_HOSTS = ['django.example.com']
9.修改项目属主和权限
# chmod -R 755 /var/www/html
# chown -R apache:apache /var/www/html
查看最终目录下的生成的项目文件
# ls -l
-rwxr-xr-x 1 apache apache 38912 Apr 16 15:04 db.sqlite3
-rwxr-xr-x 1 apache apache 541 Apr 16 14:50 manage.py
drwxr-xr-x 3 apache apache 4096 Apr 16 15:21 myproject
drwxr-xr-x 3 apache apache 4096 Apr 16 15:05 static
最终浏览器访问 django 项目
Tip:保证 windows 本地添加 django 服务器的 HOST 域名
django 测试页面
项目主页, 输入之前创建的管理员账号密码
项目后台
Finished...
转载|本文由(showerlee)原创
原文链接:http://www.showerlee.com/archives/2511
公告通知
自动化运维班、架构师班、区块链正在招生中
各位小伙伴们,欢迎试听和咨询:
扫码添加小助手微信,备注"公开课,来源简书",进入分享群
网友评论