“Django系列环境配置”的版本间的差异
小 (→配置生产环境) |
小 |
||
(未显示1个用户的1个中间版本) | |||
第133行: | 第133行: | ||
===让http|80端口自动转至 https|433端口=== | ===让http|80端口自动转至 https|433端口=== | ||
+ | 使能rewrite模块: | ||
+ | a2enmod rewrite | ||
+ | 配置80端口站点: | ||
<pre> | <pre> | ||
− | |||
− | |||
<VirtualHost *:80> | <VirtualHost *:80> | ||
ServerName yoursite.com | ServerName yoursite.com | ||
第170行: | 第171行: | ||
</VirtualHost> | </VirtualHost> | ||
</pre> | </pre> | ||
+ | 重启apache2: | ||
+ | /etc/init.d/apache2 restart | ||
+ | Done! | ||
+ | ps.如果出错,可以使用 systemctl status apache2.service查看错误信息。 |
2020年4月4日 (六) 00:09的最后版本
目录 |
安装基本运行环境
安装Django
官网:https://www.djangoproject.com
Django当前最新版本是 1.11.4,1.11系列也是 Django支持 python2.7的最后一个系列版本,安装也很简单:
进入 python的安装目录,如默认的 C:\\Python27,再进入 Scripts目录,这里应该可以找到 pip.exe,cmd运行:
C:\Python27\Scripts>pip install Django==1.11.4
确认安装是否成功:
C:\Python27\Scripts>python Python 2.7.9 (default, Dec 10 2014, 12:24:55) [MSC v.1500 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> import django >>> print django.get_version() 1.11.4
安装验证码模块captcha
$ pip install django-simple-captcha
安装apache2
$ sudo apt install apache2
安装Mysql
$ sudo apt-get install mysql-server mysql-client
设置mysql密码:https://blog.csdn.net/qq_38737992/article/details/81090373
$ sudo apt-get install libmysqlclient-dev
安装python接口
$ pip install MySQL-python
配置Django项目
下载项目代码并配置生产环境变量
1 将Djanog代码checkout至 /var/www/xxx/下
2 配置生产环境变量:xxx/settings.py
数据库初始化
登陆mysql手动创建数据库:
create database py_xxx DEFAULT CHARACTER SET utf8;
对数据库初始化:
python manage.py makemigrations python manage.py migrate
初始化语言
安装语言处理工具:
$ apt-get install gettext
编译语言:
python manage.py makemessages -l en python manage.py compilemessages
配置生产环境
这里我们直接配置 default-ssl站点,使用https安全连接。
加载ssl模块
a2enmod ssl
安装加载 wsgi模块
参考:https://modwsgi.readthedocs.io/en/develop/user-guides/quick-installation-guide.html
简单说来,就是要下载源代码:https://github.com/GrahamDumpleton/mod_wsgi/releases
wget https://github.com/GrahamDumpleton/mod_wsgi/archive/4.7.1.zip
然后编译:
./configure make make install
有可能提示出错:The 'apxs' command appears not to be installed or is not executable.
那是因为apache2-dev未安装,执行:
apt-get install apache2-dev
编译完成提示如下内容则表示安装成功
Libraries have been installed in: /usr/lib/apache2/modules
最后与apache2集成(生成load文件),并使能:
apt-get install libapache2-mod-wsgi a2enmod wsgi
启用站点
a2ensite default-ssl
修改站点配置文件 sites-enabled/default-ssl.conf
<IfModule mod_ssl.c> <VirtualHost _default_:443> ServerAdmin feedback@yoursite.com ServerName www.yoursite.com Alias /static /var/www/YourSiteDir/main/static <Directory /var/www/YourSiteDir/main/static> #Require all granted Order deny,allow Allow from all </Directory> <Directory /var/www/YourSiteDir/YourSiteDir> <Files wsgi.py> #Require all granted Order deny,allow Allow from all </Files> </Directory> WSGIScriptAlias / /var/www/YourSiteDir/YourSiteDir/wsgi.py ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined SSLEngine on SSLCertificateFile /etc/apache2/ssl/366566_public.crt SSLCertificateKeyFile /etc/apache2/ssl/366566.key SSLCertificateChainFile /etc/apache2/ssl/366566_chain.crt <FilesMatch "\.(cgi|shtml|phtml|php)$"> SSLOptions +StdEnvVars </FilesMatch> <Directory /usr/lib/cgi-bin> SSLOptions +StdEnvVars </Directory> </VirtualHost> </IfModule>
到现在,使用 https://www.yoursite.com应该可以访问了
最后我们让 http://www.yoursite.com 默认重定向至 https
让http|80端口自动转至 https|433端口
使能rewrite模块:
a2enmod rewrite
配置80端口站点:
<VirtualHost *:80> ServerName yoursite.com ServerAdmin feedback@yoursite.com # 静态文件存放的路径 Alias /static /var/www/YourSiteDir/main/static <Directory /var/www/YourSiteDir/main/static> #Require all granted Order deny,allow Allow from all </Directory> # 项目中wsgi.py的路径 <Directory /var/www/YourSiteDir/YourSiteDir> <Files wsgi.py> #Require all granted Order deny,allow Allow from all </Files> </Directory> # 所使用的python的路径,我是用的virtualenv WSGIDaemonProcess app python-path=/var/www/YourSiteDir/ WSGIProcessGroup app WSGIScriptAlias / /var/www/YourSiteDir/YourSiteDir/wsgi.py ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined RewriteEngine on RewriteCond %{SERVER_PORT} !^443$ RewriteRule ^(.*)$ https://%{SERVER_NAME}$1 [L,R] </VirtualHost>
重启apache2:
/etc/init.d/apache2 restart
Done!
ps.如果出错,可以使用 systemctl status apache2.service查看错误信息。