django开发我的简易Blog—nginx+uwsgin+django1.6+mysql 部署到CentOS6.5

前面说完了此项目的建立及数据模型设计的过程。若是未看过,能够到这里查看,而且项目源码已经放大到github上,能够去这里下载。css

代码也已经部署到sina sea上,地址为http://fengzheng.sinaapp.com/html

先跳过视图展现及表单处理的部分,先介绍一下如何部署。node

标题中已经把部署环境介绍的很清楚了:python

	服务器: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

下面介绍一下个人部署过程,仅仅是个人部署过程,针对不一样的配置可能会有所不一样,仅供参考。mysql

有些软件须要在线安装,而linux的默认源是国外的,下载速度特别慢,能够先设置一个国内源,我这里设置的是163源,下载速度仍是很快的.linux

一、进入存放源配置的文件夹nginx

cd /etc/yum.repos.dgit

二、备份默认源github

mv ./CentOS-Base.repo ./CentOS-Base.repo.backupsql

三、使用wget下载163的源

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

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

四、把下载下来的文件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 便可查看效果

相关文章
相关标签/搜索