我們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可以正常訪問網站了。