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 virtualenv
Django 环境配置
1.配置 Django virtualenv
mkdir -p /var/www/html/django
cd /var/www/html/django
virtualenv -p /usr/local/bin/python3.6 venv
2.开启 virtualenv python3 环境
source venv/bin/activate
3.在此环境安装 Django 相关模块
pip install django pymysql
Apache 配置
1.安装 apache package
yum install httpd httpd-devel -y
2.安装 mod_wsgi for python3(在虚拟环境venv下安装)
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
Require all granted
Require all granted
WSGIPythonHome "/var/www/html/django/venv"
Listen 8080
ServerName django.example.com
WSGIDaemonProcess myproject python-path=/var/www/html/django/venv/lib/python3.6/site-pachages
WSGIScriptAlias / /var/www/html/django/myproject/wsgi.py
)
5.重启 apache 并设置开机自启动
systemctl restart httpd
systemctl enable httpd
lz
(传入自己的项目)
Django 项目配置
1.保证 virtualenv python3 环境开启
source /var/www/html/django/venv/bin/activate
(想要在网页中显示对应的ip页面,需要关闭防火墙:
启动: systemctl start firewalld
关闭: systemctl stop firewalld
查看状态: systemctl status firewalld
开机禁用 : systemctl disable firewalld
开机启用 : systemctl enable firewalld
)
2.删除遗留文件:
rm -rf app1/migrations/0001_initial.py
rm -rf app2/migrations/0001_initial.py
rm -rf db.sqlite3
- 修改 wsgi 入口文件
vi django2/wsgi.py
修改结果如下:
import os
import sys
from django.core.wsgi import get_wsgi_application
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'django2.settings')
sys.path.append('/var/www/html/django/django2')
application = get_wsgi_application()
4.添加ALLOWED_HOSTS
vi django2/settings.py
(按i进入编辑模式)
将 ALLOWED_HOSTS = []
改为 ALLOWED_HOSTS = ['*']
9.修改项目属主和权限
chmod -R 755 /var/www/html
chown -R apache:apache /var/www/html
(在进行数据处理时,需要关闭selinux)
setenforce 0