nginx+uwsgi+django1.9+mysql+python2.7部署到CentOS6.5
一、相關版本介紹
名稱 | 說明 |
服務器 | 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地址的配置因為挺簡單的,我這裏就不詳細的做介紹了,僅提供以下信息:
2.1配置yum源倉庫:
配置文件地址:/etc/yum.repos.d
配置文件內容:
[c6-media]
name=localserver
baseurl=file:///media/CentOS_6.5_Final/
gpgcheck=0
enabled=1
2.2配置IP地址:
打開配置文件:[[email protected]]# 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地址。
三、編譯安裝 mysql
3.1安裝cmake
3.1.1下載安裝包
wget:cmake
3.1.2 編譯安裝
首先檢查系統有沒有安裝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版本
3.2安裝配置mysql
3.2.1下載安裝包
wget:mysql
3.2.2設置linux自動匹配環境變量
如果不是root賬號,請切換到root賬號下操作(su root)
vim /etc/profile
將光標移至到文件末尾,添加以下語句:
PATH=$PATH:/sbin export PATH=$PATH:/sbin
保存退出(wq)
source/etc/profile #該命令的作用是重新執行剛修改的初始化文件,使之立即生效。
3.2.3創建用戶組
groupadd mysql useradd –g mysql mysql –s /usr/sbin/nologin mkdir /usr/local/mysql mkdir /usr/local/mysql/data
3.2.4安裝mysql
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安裝完成。
四、安裝python
CentOS 6.5自帶的python版本是2.6我們要升級到2.7的版本
python官網:https://www.python.org/downloads/
python安裝包:Python-2.7.13.tgz
4.1安裝devtoolset
yum install zlib-devel yum install bzip2-devel yum install openssl-devel yum install ncurses-devel yum install sqlite-devel
4.3編譯安裝python2.7.13
cd到python安裝包所在的目錄
tar –zxvf Python-2.7.13.tar.gz ./configure –prefix=/usr/local make&&make altinstall
4.4將python命令指向python 2.7.13
ln–s /usr/local/bin/python2.7 /usr/local/bin/python
4.5安裝pip包
首先到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
五、安裝django1.9
前提:服務器需要接入外網
pip install Django==1.9
5.1新建一個項目
格式: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 就安裝好了。
5.2安裝MySQLdb模塊
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
重新導入,應該就沒有問題了
六、安裝uwsgi
export LDFLAGS=’-Xlinker –no-as-needed’ pip install uwsgi
6.1測試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安裝成功。
七、安裝nginx
在我的雲盤上下載CentOS6.x所需要的nginx所需要的rpm文件。運行命令:
rpm –ivh nginx-release-centos-6-o.el6.ngx.noarch.rpm yum install nginx
7.1 nginx常用命令
whereis nginx #查看nginx安裝位置 service nginx status #查看nginx服務狀態 service nginx start/stop/restart #啟動/停止/重啟nginx服務
註意:啟動nginx服務要具有管理員權限
7.2測試nginx
啟動nginx服務之後,在瀏覽器地址欄輸入http://127.0.0.1,如果看到如下界面,說明nginx安裝正確並啟動成功。
八、配置uwsgi與nginx支持django
uwsgi和nginx都可以單獨工作,我們需要把兩者聯系起來,用來支持django項目。
8.1配置uwsgi與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()
8.2配置uwsgi與nginx通訊
為了實現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處理。
8.3啟動網站
上述配置完成後,重啟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