Django+mod_wsgi + Apache环境是和比较小型的网站,也是Django的推荐环境
*启动MySQL
service [mariadb|mysqld] [start/restart]
*安装apache
!!!需要修改python到默认版本 2.7 参见http://blog.****.net/valexchao/article/details/51223120
yum install httpd httpd-devel -y
添加到自动启动
chkconfig httpd on
手动启动服务
service httpd start
浏览器输入localhost 可见apache页面
*重新编译python3
定位到python3解压后的文件夹中
make clean
重新编译 (因为缺少安装wsgi需要的共享库)
./configure --enable-shared
make
make install
添加共享库的位置到设置
vi /etc/ld.so.conf
再打开的文件增加 /usr/local/lib
:wq保存
执行下面的命令使之生效
/sbin/ldconfig -v
*下载mod_wsgi到downloads文件夹
wget https://pypi.python.org/packages/88/bd/3fa573dad32892f4fc24749bf008518439e9d7d23f539d75122d2f8d1479/mod_wsgi-4.5.2.tar.gz
tar zxvf mod_wsgi-4.5.2.tar.gz
cd mod_wsgi-4.5.2/
./configure
make
make install
*修改一个文件
vi /etc/selinux/config
修改
SELINUX=enforcing
为
SELINUX=disabled
:wq保存
使之立刻生效
setenforce=0
*在项目的路径下增加一个配置文件
apache_django_wsgi.conf
内容如下
<del><del><IfModule mod_wgsi.c>
WSGISocketPrefix /var/run/wsgi
Alias /uploads/ /root/django-projects/prj1/uploads/
Alias /static/ /root/django-projects/prj1/static/
<Directory /root/django-projects/prj1/static>
Require all granted
</Directory>
<Directory /root/django-projects/prj1/uploads>
Require all granted
</Directory>
<Directory /root/django-projects/prj1/prj1>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
WSGIDaemonProcess blogprj1
python-path=/root/django-projects/prj1:/usr/local/lib/python3.5/site-packages user=apache
group=apache
WSGIProcessGroup blogprj1
WSGIScriptAlias / /root/django-projects/prj1/prj1/wsgi.py
AddType text/html .py
</IFModule></del></del>
*让apache读取上面的配置
vi /etc/httpd/conf/httpd.conf
在文件的最后添加下面两行
LoadModule wsgi_module modules/mod_wsgi.so
Include /root/django-projects/prj1/apache_django_wsgi.conf
在项目所在的文件下
chown -R apache:apache prj1
chmod -R 755 prj1
cd ..
chmod 755 django-projects
cd ..
chmod 755 root
重启
搞了好久搞不定
决定重新安装系统
centos6.5
django1.8.3
python2.7.11
安装依赖包 必须先安装
yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel python-devel mysql-devel gcc make
下载python 2.7
wget https://www.python.org/ftp/python/2.7.11/Python-2.7.11.tgz
解压
tar zxvf Python-2.7.11.tgz
编译
./configure --enable-shared
make
make install
添加共享库的位置到设置
vi /etc/ld.so.conf
再打开的文件增加 /usr/local/lib
:wq保存
执行下面的命令使之生效
/sbin/ldconfig -v
安装mysql
yum install mysql-server
启动mysql
service mysqld start
登陆
mysql -uroot
创建一个数据库
CREATE DATABASE blog_db CHARSET=utf8
创建一个用户
GRANT ALL ON *.* TO valex@localhost IDENTIFIED BY "kylie";
修改root密码
SET PASSWORD FOR root@localhost=password('kylie');
FLUSH PRIVILEGES;
安装pip
先安装setuptools
wget https://pypi.python.org/packages/2.7/s/setuptools/setuptools-0.6c11-py2.7.egg --no-check-certificate
chmod +x setuptools-0.6c11-py2.7.egg
sh setuptools-0.6c11-py2.7.egg
再安装pip
wget https://pypi.python.org/packages/source/p/pip/pip-1.3.1.tar.gz --no-check-certificate
tar zxvf pip-1.3.1.tar.gz
cd pip-1.3.1
python setup.py install
安装mysqlclient 连接mysql和django
pip install mysqlclient
安装Django 1.8.3
pip install django==1.8.3
<chmod -R 777 /home/mypackage
那么mypackage 文件夹和它下面的所有子文件夹的属性都变成了777.>
<安装vmware tools
下载VMwareTools-9.2.0-799703.tar.gz
进入解压后的文件夹 执行
./vmware-install.pl
>
< 添加到启动项 ?
chkconfig mysqld on>
配置mysql到django
创建一个django project 叫mysite
修改settings.py中数据库设置
DATABASES = {
'default':{
'ENGINE':'django.db.backends.mysql',
'NAME':'blog_db',
'USER':'valex',
'PASSWORD':'kylie',
}
}
确保mysql服务已启动(需要管理员权限 su)
service mysqld start
定位到manage.py所在目录
$python manage.py migrate
$python manage.py runserver 0.0.0.0:8080
浏览器打开 localhost:8080
显示 It worked! 配置成功
-------------------------------------
安装apache
yum install httpd httpd-devel -y
添加到自动启动
chkconfig httpd on
手动启动服务
service httpd start
浏览器输入localhost 可见apache页面
安装mod_wsgi <一定要这么安装 不能yum 或 pip>
下载mod_wsgi到downloads文件夹
wget https://pypi.python.org/packages/88/bd/3fa573dad32892f4fc24749bf008518439e9d7d23f539d75122d2f8d1479/mod_wsgi-4.5.2.tar.gz
tar zxvf mod_wsgi-4.5.2.tar.gz
cd mod_wsgi-4.5.2/
./configure
make
make install
*修改一个文件
vi /etc/selinux/config
修改
SELINUX=enforcing
为
SELINUX=disabled
:wq保存
使之立刻生效
setenforce=0
配置apache和django 通过 wsgi
项目主目录下
新建文件apache_django_wsgi.conf
WSGIScriptAlias / /home/valex/mysite/mysite/wsgi.py
WSGIPythonPath /home/valex/mysite:/usr/local/lib/python2.7/site-packages
<Directory /home/valex/mysite/mysite>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
非windows下建议使用daemon mode
WSGISocketPrefix /var/run/wsgi
WSGIScriptAlias / /home/valex/mysite/mysite/wsgi.py
#WSGIPythonPath /home/valex/mysite:/usr/local/lib/python2.7/site-packages
<Directory /home/valex/mysite/mysite>
<Files wsgi.py>
Order deny,allow
Allow from all
#Require all granted
</Files>
</Directory>
WSGIDaemonProcess example.com python-path=/home/valex/mysite:/usr/local/lib/python2.7/site-packages
WSGIProcessGroup example.com
WSGIScriptAlias / /home/valex/mysite/mysite/wsgi.py process-group=example.com
让apache读取上面的配置
vi /etc/httpd/conf/httpd.conf
在文件的最后添加下面两行
LoadModule wsgi_module modules/mod_wsgi.so
Include /home/valex/mysite/apache_django_wsgi.conf
在项目所在的文件下
chown -R apache:apache mysite
chmod -R 755 mysite
cd ..
chmod 755 mysite
...
cd ..
chmod 755 root
reboot
重启
打开浏览器 如果显示 Internal Server Error
查看apache错误日志
cat /var/log/httpd/error_log
基本上已经ok了
下一步是配置静态文件
修改apache_django_wsgi.conf
增加静态文件部分
WSGISocketPrefix /var/run/wsgi
WSGIScriptAlias / /home/valex/mysite/mysite/wsgi.py
#WSGIPythonPath /home/valex/mysite:/usr/local/lib/python2.7/site-packages
Alias /static/ /home/valex/mysite/static/
<Directory /home/valex/mysite/static>
Order deny,allow
Allow from all
</Directory>
<Directory /home/valex/mysite/mysite>
<Files wsgi.py>
Order deny,allow
Allow from all
#Require all granted
</Files>
</Directory>
WSGIDaemonProcess example.com python-path=/home/valex/mysite:/usr/local/lib/python2.7/site-packages
WSGIProcessGroup example.com
WSGIScriptAlias / /home/valex/mysite/mysite/wsgi.py process-group=example.com
settings.py中增加
STATIC_ROOT = os.path.join(BASE_DIR, "static")
python manage.py collectstatic
之后
localhost localhost/admin的静态文件就都显示了
--end--