环境:
1.centos7
2.python2
3.django1.11
4.apache2
1.安装python虚拟环境
pip install virtualenv
2.创建虚拟环境
mkdir django_project cd django_project virtualenv venv
3.在虚拟环境里安装django
source source venv/bin/activate pip install django
4.创建django项目
django-admin startproject mysite cd mysite #测试项目,测试正常后,ctrl+c结束 python manage.py runserver #django相关请查看我的相关博客,或者到官网学习
5.安装apache
yum install httpd httpd-devel -y
6.安装apache所需的mod_wsgi模块
yum -y install mod_wsgi #或者pip install mod_wsgi
7.查看apache2是否导入mod_wsgi模块
grep -r 'mod_wsgi.so' /etc/httpd/
8.如果第7步导入,跳过这一步,否则,手动导入mod_wsgi模块
#在/etc/httpd/conf末尾添加 LoadModule wsgi_module modules/mod_wsgi.so
9.新建一个apache的虚拟主机
我的项目详情:
vi /etc/httpd/conf.d/django.conf
<Directory /var/www/html/django_project/mysite/mysite> <Files wsgi.py> Require all granted </Files> </Directory> WSGIPythonHome /var/www/html/django_project/venv WSGIPythonPath /var/www/html/django_project/venv/lib/python2.7/site-packages Listen 8080 <VirtualHost *:8080> ServerName django.example.com WSGIScriptAlias / /var/www/html/django_project/mysite/mysite/wsgi.py </VirtualHost>
重启apache
systemctl restart httpd
访问站点测试
curl 127.0.0.1:8080