django開發個人簡易Blog—nginx+uwsgin+django1.6+mysql 部署到CentOS6.5


前面說完了此項目的創建及數據模型設計的過程。如果未看過,可以到這里查看,並且項目源碼已經放大到github上,可以去這里下載。

代碼也已經部署到sina sea上,地址為http://fengzheng.sinaapp.com/

先跳過視圖展示及表單處理的部分,先介紹一下如何部署。

標題中已經把部署環境介紹的很清楚了:

	服務器:CentOS6.5  其實就是我的開發機
	mysql:Server version: 5.1.73 Source distribution
	nginx版本: nginx/1.6.0
	python版本:2.7.3
	django版本:(1, 6, 5, 'final', 0)
	uwsgi

下面介紹一下我的部署過程,僅僅是我的部署過程,針對不同的配置可能會有所不同,僅供參考。

有些軟件需要在線安裝,而linux的默認源是國外的,下載速度特別慢,可以先設置一個國內源,我這里設置的是163源,下載速度還是很快的.

1、進入存放源配置的文件夾

cd /etc/yum.repos.d

2、備份默認源

mv ./CentOS-Base.repo ./CentOS-Base.repo.backup

3、使用wget下載163的源

wget http://mirrors.163.com/.help/CentOS-Base-163.repo

wget http://mirrors.163.com/.help/CentOS6-Base-163.repo

4、把下載下來的文件CentOS-Base-163.repo設置為默認源

mv CentOS-Base-163.repo CentOS-Base.repo

mv CentOS6-Base-163.repo CentOS-Base.repo

1.安裝mysql:

CentOS6.5默認的mysql版本就是5.1.73,所以如果不是有特殊要求的話,可以不進行更改。如果有要求的話,可以卸載自帶的mysql,重新安裝需要的版本。

這里有一篇介紹用yum命令安裝mysql的文章,可以參考安裝。當然,還可以下載源碼,解壓縮,編譯,安裝。過程就不做過多介紹了。

mysql的常用命令:

	檢查mysql服務狀態
	# service mysqld status

	啟動mysql服務,要啟動mysql必須有權限 一般之前會用su命令,輸入管理員密碼
	# service mysqld start
	
	停止mysql服務
    # service mysqld stop

	重啟
	# service mysqld restart
    
	登錄 用root身份
	# mysql -u root –p

	顯示所有數據庫
	# show databases;

	使用myblog數據庫
	# use myblog;

	顯示所有表
	# show tables;

2.升級python到2.7.3:

由於CentOS6.5默認的python版本是2.6的版本,所以需要升級。下面給出源碼安裝的方法:

#下載python2.7.3源碼壓縮包
wget http://www.python.org/ftp/python/2.7.3/Python-2.7.3.tar.bz2

#解壓縮
tar xf Python-2.7.3.tar.bz2

#進入解壓縮后的目錄
cd Python-2.7.3

#配置及環境檢查
./configure

#安裝
make install

安裝之后,在終端窗口中輸入python,可以查看python版本是否已經是2.7.3的版本。

注:這樣升級之后可能會導致yum命令失效,

因為yum依賴於ContOS系統默認的python版本,而升級python之后,yum腳本中的python版本被修改為最新版本,此時需要改回為原來的python版本,ContOS6.5默認的python版本為python2.6.6,解決方法如下:

進入yum所在目錄

cd /usr/bin

su

vim yum

將第一行
#!/usr/bin/python2.7
改為:
#!/usr/bin/python2.6

輸入:wq! 強制保存

3.安裝MySQLdb模塊:

需要到這里下載源碼壓縮包,目前最新版本是1.2.3。安裝過程:

cd /home/fengzheng/Soft/  #進入壓縮包所在目錄

tar -zxf MySQL-python-1.2.3.tar.gz  #解壓

cd MySQL-python-1.2.3  #進入解壓后的目錄

python setup.py build #編譯

python setup.py install #安裝

安裝完成后,可以在終端窗口中輸入以下命令測試是否安裝成功,如果沒有出現錯誤信息,則說明安裝成功。

python
import MySQLdb  

4.安裝django:

這個不多說,可以到django官網下載源碼,然后用命令進行源碼安裝:

cd /home/fengzheng/Soft/

tar -zxf Django-1.6.5.tar.gz

cd Django-1.6.5/

python setup.py install

 

也可以用官網上提供的在線安裝方法,需要pip的支持:pip install Django==1.6.5

5.安裝uwsgi:

export LDFLAGS="-Xlinker --no-as-needed"
$ pip install uwsgi

測試uwsgi是否安裝成功:

新建一個uwsgiTest.py文件,代碼如下:

#-*- coding:utf-8 -*-

def application(env, start_response):
    start_response('200 OK', [('Content-Type','text/html')])
    return "Hello uwsgi"

進入文件所在目錄,執行命令:

uwsgi --http :1989 --wsgi-file uwsgiTest.py

之后,在瀏覽器訪問http://127.0.0.1:1989 ,如果出現Hello uwsgi字樣,說明uwsgi安裝成功。

6.安裝nginx:

這里下載CentOS6.x所需的nginx所需的rpm文件。運行命令:

su

rpm –ivh nginx-release-centos-6-0.el6.ngx.noarch.rpm

yum install nginx

nginx常用命令:

#查看nginx安裝位置:
whereis nginx

#查看ngin狀態
service nginx status

啟動Nginx:
/usr/sbin/nginx 
或者直接輸入 nginx 
或者 service nginx start 

#停止nginx
service nginx stop

#重啟
service nginx restart
或者
nginx -s  reload

注:啟動服務必須具有管理員權限 即之前要有su命令


有時候會出現異常:nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)

出現此問題是因為80端口被Nginx自己占用,解決方法為:

fuser -k 80/tcp

然后再啟動Nginx

最后訪問http://127.0.0.1 ,看到如下界面,說明nginx安裝正確並成功啟動:

image

7.配置uwsgi與nginx支持django:

uwsgi和nginx都可以單獨工作,我們要把這兩者聯系起來,用來支持django項目。

首先我們打開項目所在目錄,在根目錄,也就是manage.py所在的目錄新建一個django_uwsgi.py的文件,這個文件是要django以uwsgi的方式來運行,文件內容如下:代碼中注釋的那兩行是manage.py運行django的方式,可以看出有什么不同。

"""
WSGI config for fengzhengBlog project.

It exposes the WSGI callable as a module-level variable named ``application``.

For more information on this file, see
https://docs.djangoproject.com/en/dev/howto/deployment/wsgi/
"""

import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "fengzhengBlog.settings")

#from django.core.wsgi import get_wsgi_application
#application = get_wsgi_application()
from django.core.handlers.wsgi import WSGIHandler
application = WSGIHandler()

為了實現Nginx與uWSGI的連接,兩者之間將采用soket來通訊方式,還需要在項目根目錄,即和上面的django_uwsgi.py同一目錄新建一個文件來實現,文件格式可以是xml,命名為django_socket.xml,內容如下:

<uwsgi>
    <socket>:8077</socket>
        <chdir></chdir>
        <module>django_uwsgi</module><!-- 指定模塊 即上面創建的django_uwsgi.py的名稱 -->
        <processes>4</processes> <!-- 進程數 --> 
    <daemonize>uwsgi.log</daemonize>
</uwsgi>

或者是ini格式,命名為django_socket.ini,內容如下:

[uwsgi]
vhost = false
socket = 127.0.0.1:8077      ;通信端口
master = true
enable-threads = true
workers = 4
wsgi-file = django_uwsgi.py   ;指定模塊 即上面創建的django_uwsgi.py

配置nginx,用weheris nginx命令查看nginx的安裝目錄在/etc/nginx,進入此目錄,用vim打開nginx.conf配置文件,修改內容:

user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


http {
	include       /etc/nginx/mime.types;
	default_type  application/octet-stream;

	log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
	'$status $body_bytes_sent "$http_referer" '
	'"$http_user_agent" "$http_x_forwarded_for"';

	access_log  /var/log/nginx/access.log  main;

	sendfile        on;
	#tcp_nopush     on;

	keepalive_timeout  65;

	#gzip  on;

	include /etc/nginx/conf.d/*.conf;
	server {
			listen   80;  #80端口
			server_name 127.0.0.1;  #最后訪問的地址
			access_log /home/fengzheng/mypython/access.log;  #日志
			error_log /home/fengzheng/mypython/error.log;
			#charset koi8-r;

			#access_log  logs/host.access.log  main;

			location / {
				include        uwsgi_params;
				uwsgi_pass    127.0.0.1:8077;  #前面django_socket.xml或django_socket.ini文件中配置的端口
			}

			#error_page  404              /404.html;

			# redirect server error pages to the static page /50x.html
			#
			error_page   500 502 503 504  /50x.html;
		
			#以下配置的靜態文件
			location /css/ {
					alias  /home/fengzheng/blog/fengzhengBlog/fengzhengBlog/css/; }

			location /js/ {
					alias  /home/fengzheng/blog/fengzhengBlog/fengzhengBlog/js/;       
			}

			location /images/ {
					alias  /home/fengzheng/blog/fengzhengBlog/fengzhengBlog/images/;        }

			location /ueEditor/ {
					alias  /home/fengzheng/blog/fengzhengBlog/fengzhengBlog/ueEditor/;        }
	}
	#以下是另一個項目配置
	server {

			listen   81;
			server_name 127.0.0.1;
			access_log /home/fengzheng/mypython/accessue.log;
			error_log /home/fengzheng/mypython/errorue.log;
			#charset koi8-r;

			#access_log  logs/host.access.log  main;

			location / {
			include        uwsgi_params;
			uwsgi_pass    127.0.0.1:8088;
			}

			#error_page  404              /404.html;

			# redirect server error pages to the static page /50x.html
			#
			error_page   500 502 503 504  /50x.html;
			location = /50x.html {
			root   html;
			}

			location /upload/ {
			alias  /home/fengzheng/ueEditor_django/ueEditor_django/upload/; }

			location /UE/ {
			alias  /home/fengzheng/ueEditor_django/ueEditor_django/UE/;        }
			}

}

上面配置了兩個server,即可以支持兩個django站點。如果只有一個,可以將下面的server節點去掉。注意location節點的配置,如:

location /css/ {
                    alias  /home/fengzheng/blog/fengzhengBlog/fengzhengBlog/css/; }

location后面跟的是項目中的靜態文件的目錄前后都要有“/”,alias后面是靜態文件所在的目錄。對應urls.py中的路由配置:

( r'^css/(?P<path>.*)$', 'django.views.static.serve',
            { 'document_root': ROOT+'/css' }
    ),
    ( r'^js/(?P<path>.*)$', 'django.views.static.serve',
            { 'document_root': ROOT+'/js' }
    ),
    ( r'^images/(?P<path>.*)$', 'django.views.static.serve',
            { 'document_root': ROOT+'/images' }
    ),
    ( r'^ueEditor/(?P<path>.*)$', 'django.views.static.serve',
            { 'document_root': ROOT+'/ueEditor' }
    ),

在上面的設置后,可以讓Nginx來處理靜態文件 。非靜態文件請求Nginx會發給 socket 8077,然后讓uWSGI來進行處理。

8.啟動網站:

配置完成后,重啟nginx :  nginx -s reload

啟動uwsgi服務:

進入項目根目錄,即前面創建的django_uwsgi.py所在的目錄。

運行如下命令,使用django_socket.xml配置:

uwsgi -x django_socket.xml

如果系統不支持-x命令,可以運行下面的命令啟動django_socket.ini配置:

uwsgi --ini django_socket.ini

啟動成功后,會得到如下信息:

[uWSGI] getting INI configuration from django_socket.ini
*** Starting uWSGI 2.0.5.1 (64bit) on [Thu Jul 17 01:02:06 2014] ***
compiled with version: 4.4.7 20120313 (Red Hat 4.4.7-4) on 22 June 2014 20:36:27
os: Linux-2.6.32-431.el6.x86_64 #1 SMP Fri Nov 22 03:15:09 UTC 2013
nodename: localhost
machine: x86_64
clock source: unix
detected number of CPU cores: 1
current working directory: /home/fengzheng/blog/fengzhengBlog
detected binary path: /usr/local/bin/uwsgi
!!! no internal routing support, rebuild with pcre support !!!
your processes number limit is 1024
your memory page size is 4096 bytes
detected max file descriptor number: 1024
lock engine: pthread robust mutexes
thunder lock: disabled (you can enable it with --thunder-lock)
uwsgi socket 0 bound to TCP address 127.0.0.1:8077 fd 3
Python version: 2.7.3 (default, Jun 20 2014, 02:55:10)  [GCC 4.4.7 20120313 (Red Hat 4.4.7-4)]
Python main interpreter initialized at 0x17db280
python threads support enabled
your server socket listen backlog is limited to 100 connections
your mercy for graceful operations on workers is 60 seconds
mapped 363840 bytes (355 KB) for 4 cores
*** Operational MODE: preforking ***
WSGI app 0 (mountpoint='') ready in 10 seconds on interpreter 0x17db280 pid: 7871 (default app)
*** uWSGI is running in multiple interpreter mode ***
spawned uWSGI master process (pid: 7871)
spawned uWSGI worker 1 (pid: 7873, cores: 1)
spawned uWSGI worker 2 (pid: 7874, cores: 1)
spawned uWSGI worker 3 (pid: 7875, cores: 1)
spawned uWSGI worker 4 (pid: 7876, cores: 1)


查看上述信息,發現啟動成功,開啟了4個線程。

更詳細的安裝配置可查看http://django-china.cn/topic/101/#tophttp://django-china.cn/topic/124/講的很詳細。

之后訪問站點:http://127.0.0.1 即可查看效果


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM