标签:python nginx django mysql-python mysqldb
名称 | 说明 |
服务器 | CentOS6.5 |
nginx | 1.12.1 |
python | 2.7.13 |
django | 1.9 |
mysql | 5.5.32 |
uwsgi | 2.0.15 |
下面介绍一下我的部署过程,针对不同的配置可能有所不同仅供参考:
如果你的操作系统是本地的一台虚拟机,可能需要配置yum源和IP地址。关于yum源和IP地址的配置因为挺简单的,我这里就不详细的做介绍了,仅提供以下信息:
配置文件地址:/etc/yum.repos.d
配置文件内容:
[c6-media]
name=localserver
baseurl=file:///media/CentOS_6.5_Final/
gpgcheck=0
enabled=1
打开配置文件:[root@localhost]# vim/etc/sysconfig/network-scrips/ifcfg-eth0
配置文件内容EVICE=eth0
HWADDR=00:0C:29:4F:96:59
TYPE=Ethernet
UUID=05521fea-360e-47a5-b329-654087bcd9e3
ONBOOT=yes
NM_CONTROLLED=yes
BOOTPROTO=dhcp
修改完配置文件后重启服务:service networkrestart
我这里配置的是用dhcp自动获取IP,也可以配置静态IP地址。
wget:cmake
首先检查系统有没有安装mysql所需要的包
rpm –qa | grep gcc-c++ rpm –qa | grep ncurses-devel
如果系统没有安装,则安装:
yum install gcc-c++ -y yum install ncurses-devel –y
cd到已下载安装包的所在目录,执行以下命令:
tar –zxvf cmake-2.8.8.tar.gz cd cmake-2.8.5 ./bootstrap;make;make install cmake –version #查看cmake版本
wget:mysql
如果不是root账号,请切换到root账号下操作(su root)
vim /etc/profile
将光标移至到文件末尾,添加以下语句:
PATH=$PATH:/sbin export PATH=$PATH:/sbin
保存退出(wq)
source/etc/profile #该命令的作用是重新执行刚修改的初始化文件,使之立即生效。
groupadd mysql useradd –g mysql mysql –s /usr/sbin/nologin mkdir /usr/local/mysql mkdir /usr/local/mysql/data
cd到已下载安装包的所在目录,执行以下命令:
tar –zxvf mysql-5.5.32.tar.gz cd mysql-5.5.32 cmake–DCMAKE_INSTALL_PREFIX=/usr/local/mysql
注释:cmake的时候,参数可以不用那么多个,只要一个DCMAKE_INSTALL_PREFIX=/usr/local/mysql就行了,我们可以在my.cnf里面配置[mysql]中的内容
make && make install
如果需要更改配置,则
make clean rm –f CmakeCache.txt
复制配置文件
cp support –files/my-medium.cnf/usr/local/mysql/my.cnf
设置权限
chmod +x /usr/local/mysql chown –R mysql:mysql /usr/local/mysql
配置开机启动
cp/usr/local/mysql/support-files/mysql.server /etc/init.d/mysql chmod +x /etc/init.d/mysql chkconfig –list chkconfig –add mysql chkconfig mysql on chkconfig –list mysql
修改my.cnf配置
vim /usr/local/mysql/my.cnf [mysql]下添加: datadir=/usr/local/mysql/data default-storage-engine=MyISAM log-error=/usr/local/mysql/mysql_error.log pid-file=/usr/local/mysql/mysql.pid user=mysql tmpdir=/tmp
安装默认数据表
/usr/local/mysql/scripts/mysql_install_db–basedir=/usr/local/mysql –datadir=/usr/local/mysql/data –usr=mysql
启动mysql
servicemysql start
或者:
/etc/init.d/mysqlstart
设置mysql命令行路径
ln –s /usr/local/mysql/bin/mysql/usr/bin
测试mysql是否启动
1.查看是否有mysql进程
ps–ef | grep mysq
2.查看端口时候运行
netstat –tnl| grep 3306
至此mysql安装完成。
CentOS 6.5自带的python版本是2.6我们要升级到2.7的版本
python官网:https://www.python.org/downloads/
python安装包:Python-2.7.13.tgz
yum install zlib-devel yum install bzip2-devel yum install openssl-devel yum install ncurses-devel yum install sqlite-devel
cd到python安装包所在的目录
tar –zxvf Python-2.7.13.tar.gz ./configure –prefix=/usr/local make&&make altinstall
ln–s /usr/local/bin/python2.7 /usr/local/bin/python
首先到python网站下载pip程序包,解压后执行:
python setup.py install
如果没有安装setuptools系统会提示让你安装setuptools,那么在同样的网站上下载setuptools程序包,在安装setuptools之前要安装它的依赖包,为了让大家少踩点坑我把包名提供给大家(
appdirs、packaging、pyparsing、six)将这些包下载下来解压安装后,再安装setuptools,然后安装pip包,安装成功。
注意:此时你的系统里会有两个pip,一个在python2.6下一个在python2.7下,默认的pip使用的是python2.6的pip。需要将pip命令映射到python2.7下的pip:
ln –s /usr/local/bin/pip2.7/usr/bin/pip
前提:服务器需要接入外网
pip install Django==1.9
格式:django-admin.py startproject 项目名称
cd到存放Django项目的目录
django-admin.pystartproject mysite
运行此命令后会在当前目录下创建一个mysite的目录,cd到此目录下面,开启web服务
python manage.pyrunserver 127.0.0.1:8080
在浏览器地址栏输入127.0.0.1:8080就能访问到我们的站。
(有关于python django web框架这篇文章不过多说明)
至此我们的Django 就安装好了。
pip installMySQL-python
可能会报错:EnvironmentError: mysql_config not found,意思是没有找到mysql_config这个文件。
解决办法:
1.使用find命令找到mysql_config
find / -name mysql_config
通过返回信息发现在/usr/local/mysql/bin/目录下
2.将此目录加到环境变量:
vim /etc/profile
在文件的末尾加上下面这条语句:
PATH=$PATH:/usr/local/mysql/bin
保存退出并且使之立即生效:
source /etc/profile
重新安装就不会报错了,等安装成功以后,进入python shell,测试导入模块
>>>import MySQLdb
可能会报错:ImportError:libmysqlclient.so.18: cannot open shared object file: No such file or directory。根据提示,找不到一个叫libmysqlclient.so.18的文件。
解决办法:
找到这个文件,并做一个软连接
ln -s
/usr/local/mysql/lib/libmysqlclient.so.18 /usr/lib64/libmysqlclient.so.18
重新导入,应该就没有问题了
export LDFLAGS=’-Xlinker –no-as-needed’ pip install uwsgi
新建一个uwsgiTest.py文件,代码如下:
#-*- coding:utf-8 -*- def application(env, start_response): start_response(‘200 OK‘, [(‘Content-Type‘,‘text/html‘)]) return "Hello uwsgi"
进入文件所在目录,执行命令:
uwsgi --http :1992 --wsgi-file uwsgiTest.py
之后,在浏览器访问http://127.0.0.1:1992,如果出现Hello uwsgi字样,说明uwsgi安装成功。
在我的云盘上下载CentOS6.x所需要的nginx所需要的rpm文件。运行命令:
rpm –ivh nginx-release-centos-6-o.el6.ngx.noarch.rpm yum install nginx
whereis nginx #查看nginx安装位置 service nginx status #查看nginx服务状态 service nginx start/stop/restart #启动/停止/重启nginx服务
注意:启动nginx服务要具有管理员权限
启动nginx服务之后,在浏览器地址栏输入http://127.0.0.1,如果看到如下界面,说明nginx安装正确并启动成功。
uwsgi和nginx都可以单独工作,我们需要把两者联系起来,用来支持django项目。
首先我们进入django项目所在目录,也就是manage.py文件所在的目录新建一个django_uwsgi.py的文件,这个文件是要django以uwsgi的方式来运行,文件内容如下:
vim django_uwsgi.py
#__*__ decoding:utf-8 __*__ """ WSGI config for xiaolei1Blog project. It exposes the WSGI callable as a module-levelvariable named ‘application‘. For more information on this file,see https://docs.djangoproject.com/en/dev/howto/deployment/wsgi/ """ import os import sys os.environ.setdefault(‘DJANGO_SETTINGS_MODULE‘,‘xiaolei_blog.settings‘) #from django.core.wsgi import get_wsgi_application #application=get_wsgi_application() from django.core.wsgi importget_wsgi_application application=get_wsgi_application()
为了实现nginx与uwsgi的连接,两者之间将采用soket通讯方式,在与django_uwsgi.py同一目录下新建django_socket.ini文件,该文件是uwsgi的参数配置文件,文件内容如下:
vim django_django_socket.ini
[uwsgi] vhost = false socket = 127.0.0.1:8079 ;通信端口 chdir=/djproject/xiaolei_blog ;django项目所在目录 master = true enable-threads = true workers = 4 ;开启4个线程 wsgi-file = django_uwsgi.py ;指定模块,即上面创建的django_uwsgi.py
配置nginx,打开nginx配置文件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 /djproject/xiaolei_blog/log/access.log; #日志 error_log /djproject/xiaolei_blog/log/error.log; #charset koi8-r; #access_log logs/host.access.log main; location / { include uwsgi_params; uwsgi_pass 127.0.0.1:8079; #前面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 /static/ { alias /djproject/xiaolei_blog/static/; } location /kindeditor/ { alias /djproject/xiaolei_blog/static/kindeditor/;} } }
说明:server就是django站点,可以配置多个,注意静态文件路径的配置格式。在上面的设置后,可以让Nginx来处理静态文件,非静态文件请求nginx会发给socket 8079,然后让uwsgi处理。
上述配置完成后,重启nginx:service nginx restart
启动uwsgi服务
进入django_uwsgi.py所在的目录运行如下命令:
uwsgi --ini django_socket.ini
显示如下信息:
[uWSGI] getting INI configuration fromdjango_socket.ini
*** Starting uWSGI 2.0.15 (64bit) on [FriJul 14 10:09:34 2017] ***
compiled with version: 4.4.7 20120313 (RedHat 4.4.7-4) on 12 July 2017 12:01:53
os: Linux-2.6.32-431.el6.x86_64 #1 SMP FriNov 22 03:15:09 UTC 2013
nodename: localhost.localdomain
machine: x86_64
clock source: unix
detected number of CPU cores: 4
current working directory:/djproject/xiaolei_blog
detected binary path: /usr/local/bin/uwsgi
!!! no internal routing support, rebuildwith pcre support !!!
uWSGI running as root, you can use--uid/--gid/--chroot options
*** WARNING: you are running uWSGI as root!!! (use the --uid flag) ***
chdir() to /djproject/xiaolei_blog
your processes number limit is 14740
your memory page size is 4096 bytes
detected max file descriptor number: 1024
lock engine: pthread robust mutexes
thunder lock: disabled (you can enable itwith --thunder-lock)
uwsgi socket 0 bound to TCP address127.0.0.1:8079 ;通信端口 fd 3
Python version: 2.7.13 (default, Jun 6 2017, 10:50:08) [GCC 4.4.7 20120313 (Red Hat 4.4.7-4)]
Python main interpreter initialized at0xfe0380
python threads support enabled
your server socket listen backlog islimited to 100 connections
your mercy for graceful operations onworkers is 60 seconds
mapped 363840 bytes (355 KB) for 4 cores
*** Operational MODE: preforking ***
WSGI app 0 (mountpoint=‘‘) ready in 1seconds on interpreter 0xfe0380 pid: 3838 (default app)
*** uWSGI is running in multipleinterpreter mode ***
spawned uWSGI master process (pid: 3838)
spawned uWSGI worker 1 (pid: 3840, cores:1)
spawned uWSGI worker 2 (pid: 3841, cores:1)
spawned uWSGI worker 3 (pid: 3842, cores:1)
spawned uWSGI worker 4 (pid: 3843, cores:1)
查看上述信息,启动成功,并开启了4个线程。
之后访问站点:http://127.0.0.1 就可以看到你的django项目的页面。
至此所有的配置完成。
本文出自 “不倒翁先生” 博客,请务必保留此出处http://kudangren.blog.51cto.com/11300146/1952310
nginx+uwsgi+django1.9+mysql+python2.7部署到CentOS6.5
标签:python nginx django mysql-python mysqldb
原文地址:http://kudangren.blog.51cto.com/11300146/1952310