我们LINUX的版本为Ubuntu 14.04,由于Ubuntu 14.04预装python 2.7,因此我们仅安装apache2 和 django等相关软件。
1. 安装apache2
apt-get install apache2
2. 安装 sql-server
sudo apt-get install mysql-server mysql-client
并根据提示信息输入root密码,密码设置为echoproject
3. 安装mod_wsgi
sudo apt-get install libapache2-mod-wsgi
4. 安装pip工具
sudo apt-get install python-pip
5. 安装Django ,开发版本是1.9.2,但是我们使用最新的1.10,因为1.10的release notes表明,现有的代码不需要进行更改。
pip install Django
6. 安装mysqldb-python
第一条命令一定需要,需要安装mysql开发版本才能安装mysql-python
apt-get install python-devlibmysqlclient-dev
pip install MySQL-python
7. 安装crispy-froms
pip install django-crispy-forms
8. 建立django项目与app
Cd /var/www/html
django-admin startproject echo_site
cd echo_site/
django-admin startapp echo
9. 将原有文件上传至相应目录,并设置mysql的密码,注意原有settings.py中的secret_key一定要保留,不要使用原来开发文件中的key。
10. 登录mysql建立一个名为echosite的数据库
mysql -uroot –pechoproject.
CREATE DATABASE `echosite` DEFAULT CHARACTER SET utf8COLLATE utf8_general_ci;
11. 建立数据库
Cd /var/www/html/echo_site
python manage.py makemigrations
python manage.py migrate
12. 建立新的静态内容文件夹
我们将所有css等静态文件通过collectstatics放入同一个文件中。
如果我们将新的static文件夹建立在html目录下,在settings.py中增加相关配置。这样将所有项目的CSS文件都归并到了这个文件夹下,调用便会去该文件夹下查找。如果开启了admin,这一步是很必要的,不然部署到生产环境的时候会找不到样式文件 Settings.py:
STATIC_ROOT = '/var/www/html/static/'
python manage.py collectstatic
根据配置文件会询问你,是否将所有的静态文件COPY到该目录下。
13.复制Apache2虚拟配置文件配置
cd /etc/apache2/
其中apache2.conf是主要配置文件,如果服务器只包含一个网站,那么直接在这个文件中配置即可。但如果要设置虚拟主机,那么需要在sites-available进行相关设置。
进入该文件夹发现有个000-default.conf 文件,将其复制一份
cp 000-default.conf echosite.conf
14.修改echosite.conf
我们对echosite.conf进行修改
<VirtualHost*:80>
# The ServerName directive sets therequest scheme, hostname and port that
# the server uses to identify itself.This is used when creating
#redirection URLs. In the context of virtual hosts, the ServerName
# specifies what hostname must appearin the request's Host: header to
# match this virtual host. For thedefault virtual host (this file) this
# value is not decisive as it is usedas a last resort host regardless.
# However, you must set it for anyfurther virtual host explicitly.
ServerName echosite.echoproject.cn
ServerAdmin webmaster@localhost
DocumentRoot /var/www/html
#Django settings
#设置相关的媒体文件位置
Alias /media//var/www/html/echo_site/media/
#设置静态文件的位置
Alias /static/ /var/www/html/static/
#设置静态文件的权限
<Directory /var/www/html/static>
Require all granted
</Directory>
#设置媒体文件的权限
<Directory/var/www/html/echo_site/media>
Require all granted
</Directory>
#告知wsgi.py的位置
WSGIScriptAlias //var/www/html/echo_site/echo_site/wsgi.py
#如果是在apache2.conf中配置写以下这条
#WSGIPythonPath/var/www/html/echo_site
#如果是配置虚拟主机,写以下两条
WSGIDaemonProcess echosite.echoproject.cnpython-path=/var/www/html/echo_site:/var/www/html/.virtualenvs/echo_site/lib/python2.7/site-packages
WSGIProcessGroup echoproject.cn
#设置wsgi的权限
<Directory /var/www/html/echo_site/echo_site>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
# Available loglevels: trace8, ...,trace1, debug, info, notice, warn,
# error, crit, alert, emerg.
# It is also possible to configure theloglevel for particular
# modules, e.g.
#LogLevel info ssl:warn
#设置日志文件
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.logcombined
# For most configuration files fromconf-available/, which are
# enabled or disabled at a globallevel, it is possible to
# include a line for only oneparticular virtual host. For example the
# following line enables the CGIconfiguration for this host only
# after it has been globally disabledwith "a2disconf".
#Includeconf-available/serve-cgi-bin.conf
</VirtualHost>
15.禁用默认的apache中默认的配置000-default.conf,将我们的配置开启。
sudo a2dissite 000-default.conf
sudo a2ensite echosite.conf
16.通过域名http://echosite.echoproject.cn可以正常访问网站了。