前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >django开发个人简易Blog—nginx+uwsgin+django1.6+mysql 部署到CentOS6.5

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

作者头像
古时的风筝
发布2018-01-08 11:18:51
1K0
发布2018-01-08 11:18:51
举报
文章被收录于专栏:古时的风筝古时的风筝

前面说完了此项目的创建及数据模型设计的过程。如果未看过,可以到这里查看,并且项目源码已经放大到github上,可以去这里下载。

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

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

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

代码语言:javascript
复制
	服务器: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的常用命令:

代码语言:javascript
复制
	检查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的版本,所以需要升级。下面给出源码安装的方法:

代码语言:javascript
复制
#下载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 #安装

安装完成后,可以在终端窗口中输入以下命令测试是否安装成功,如果没有出现错误信息,则说明安装成功。

代码语言:javascript
复制
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文件,代码如下:

代码语言:javascript
复制
#-*- coding:utf-8 -*-

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

进入文件所在目录,执行命令:

代码语言:javascript
复制
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常用命令:

代码语言:javascript
复制
#查看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
image

7.配置uwsgi与nginx支持django:

uwsgi和nginx都可以单独工作,我们要把这两者联系起来,用来支持django项目。

首先我们打开项目所在目录,在根目录,也就是manage.py所在的目录新建一个django_uwsgi.py的文件,这个文件是要django以uwsgi的方式来运行,文件内容如下:代码中注释的那两行是manage.py运行django的方式,可以看出有什么不同。

代码语言:javascript
复制
"""
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,内容如下:

代码语言:javascript
复制
<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,内容如下:

代码语言:javascript
复制
[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配置文件,修改内容:

代码语言:javascript
复制
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中的路由配置:

代码语言:javascript
复制
( 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配置:

代码语言:javascript
复制
uwsgi -x django_socket.xml

如果系统不支持-x命令,可以运行下面的命令启动django_socket.ini配置:

代码语言:javascript
复制
uwsgi --ini django_socket.ini

启动成功后,会得到如下信息:

代码语言:javascript
复制
[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 即可查看效果

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2014-07-18 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
云数据库 MySQL
腾讯云数据库 MySQL(TencentDB for MySQL)为用户提供安全可靠,性能卓越、易于维护的企业级云数据库服务。其具备6大企业级特性,包括企业级定制内核、企业级高可用、企业级高可靠、企业级安全、企业级扩展以及企业级智能运维。通过使用腾讯云数据库 MySQL,可实现分钟级别的数据库部署、弹性扩展以及全自动化的运维管理,不仅经济实惠,而且稳定可靠,易于运维。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档