nginx+uwsgi+django環境部署部署

 

 

首科常昊技術部

 

Django框架部署文檔:

 

硬件環境:阿里雲ecs服務器8核心、16GB10Mbps

環境結構:nginx+uwsgi+django

所需軟件:python-3.6.1pip3.6mysql-5.1.73 nginx-1.6.3

 

 

 

 

 

 

[root@shouke src]# yum install zlib-develbzip2-devel pcre-devel openssl-devel ncurses-devel sqlite-devel readline-develtk-devel
[root@shouke ~]# yum -y install opensslopenssl-devel

 

 編譯安裝python3.6.1
[root@shouke leslie]# tar zxvf Python-3.6.1.tgz -C /usr/src/
[root@shouke leslie]# cd /usr/src/Python-3.6.1/
[root@shouke Python-3.6.1]# ./configure--prefix=/usr/local/python --enable-shared CFLAGS=-fPIC
[root@shouke Python-3.6.1]#make&&make install

 

 

##安裝完python3.6過程中自動安裝了pip工具

'''

#Collecting setuptools

#Collecting pip

#Installing collected packages: setuptools,pip

#Successfully installed pip-9.0.1setuptools-28.8.0

'''

 

 

現在輸入python還是python2.6

[root@shouke ~]# python
Python 2.6.6 (r266:84292, Jul 23 2015,15:22:56)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-11)] onlinux2
Type "help","copyright", "credits" or "license" for moreinformation.
>>>

2.6python的程序備份

[root@shouke ~]# which python
/usr/bin/python
[root@shouke ~]# cd /usr/bin/
[root@shouke bin]# mv python python.bak

 

3.6python軟鏈接到/usr/bin/python

[root@shouke bin]# ln -s/usr/local/python/bin/python3 /usr/bin/python
[root@shouke bin]# python
python: error while loading sharedlibraries: libpython3.6m.so.1.0: cannot open shared object file: No such fileor directory

 

缺少libpython3.6m.so.1.0 python安裝目錄下的libpython3.6m.so.1.0鏈接到/usr/lib64庫下

[

root@shouke bin]# ln -s/usr/local/python/lib/libpython3.6m.so.1.0 /usr/lib64/

 

可以使用了

[root@shouke bin]# python
Python 3.6.1 (default, Oct 26 2017,11:49:35)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-17)] onlinux
Type "help","copyright", "credits" or "license" for moreinformation.
>>> print("hello world")
hello world
>>>

 

現在yum無法使用了,因爲yum是使用python解釋器運行的,現在找不到原來版本的python了,所以無法運行了,

[root@shouke bin]# yum
 File "/usr/bin/yum", line 30
   except KeyboardInterrupt, e:
                            ^
SyntaxError: invalid syntax
 
 
[root@shouke bin]# which yum
/usr/bin/yum

 

[root@shouke bin]# cat /usr/bin/yum

#!/usr/bin/python2.6   ##修改第一行爲這樣
import sys
try:
   import yum
except ImportError:
   print >> sys.stderr, """\
There was a problem importing one of thePython mod
。。。


 

yum可以使用了:

[root@shouke bin]# yum makecache
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 *base: mirrors.aliyuncs.com
 *epel: mirrors.aliyuncs.com
 *extras: mirrors.aliyuncs.com

 

 

 

安裝 uwsgi

 

[root@shouke bin]#/usr/local/python/bin/pip3 install uwsgi
Collecting uwsgi
 Downloadinghttp://mirrors.aliyun.com/pypi/packages/bb/0a/45e5aa80dc135889594bb371c082d20fb7ee7303b174874c996888cc8511/uwsgi-2.0.15.tar.gz(795kB)
   100% |████████████████████████████████| 798kB 58.5MB/s
Installing collected packages: uwsgi
 Running setup.py install for uwsgi ... done
Successfully installed uwsgi-2.0.15

 

 

驗證安裝:

[root@shouke bin]#/usr/local/python/bin/uwsgi --version
2.0.15

 

安裝django

[root@shouke ~]# /usr/local/python/bin/pip3install django
Collecting django
 Downloadinghttp://mirrors.aliyun.com/pypi/packages/82/33/f9d2871f3aed5062661711bf91b3ebb03daa52cc0e1c37925f3e0c4508c5/Django-1.11.6-py2.py3-none-any.whl(6.9MB)
   100% |████████████████████████████████| 7.0MB 68.8MB/s
Collecting pytz (from django)
 Downloadinghttp://mirrors.aliyun.com/pypi/packages/55/62/e7cd0e15b76062d298413f14bb4ec3cd8568a22d274427f9c3c7286969f4/pytz-2017.2-py2.py3-none-any.whl(484kB)
   100% |████████████████████████████████| 491kB 67.8MB/s
Installing collected packages: pytz, django
Successfully installed django-1.11.6pytz-2017.2

 

 

驗證安裝:

[

root@shouke ~]# python
Python 3.6.1 (default, Oct 26 2017,11:49:35)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-17)] on linux
Type "help","copyright", "credits" or "license" for moreinformation.
>>> import django

 

 

 

>>> django.VERSION
(1, 11, 6, 'final', 0)

 

輸入url:8002驗證。(提示服務器ip地址加入到允許訪問的列表內)

spacer.gif

 

 

 
[root@shouke demosite]# vim demosite/settings.py   #修改此文件


spacer.gif

[root@shouke demosite]# python manage.pyrunserver 0.0.0.0:8002

 

再次運行,訪問成功:

spacer.gif

 

 

部署項目:

 

將項目上傳到服務器:

spacer.gif

移動到項目/opt下面,並在/opt下面新建文件夾script

[root@shouke opt]# ls
gitlab online_edit_script  script

 

script目錄裏新建uwsgi.ini配置文件

 

[root@shouke opt]# cd script/
[root@shouke script]# ls
uwsgi.ini
[root@shouke script]# vim uwsgi.ini

 

#uwsig使用配置文件啓動

[uwsgi]

# 項目目錄

chdir=/opt/online_edit_script/

# 指定項目的application

module=online_edit_script.wsgi:application

# 指定sock的文件路徑      

socket=/opt/script/uwsgi.sock

# 進程個數      

workers=5

pidfile=/opt//script/uwsgi.pid

# 指定IP端口      

http=127.0.0.1:9001

# 指定靜態文件

static-map=/static=/opt/online_edit_script/static

# 啓動uwsgi的用戶名和用戶組

uid=root

gid=root

# 啓用主進程

master=true

# 自動移除unix Socket和pid文件當服務停止的時候

vacuum=true

# 序列化接受的內容,如果可能的話

thunder-lock=true

# 啓用線程

enable-threads=true

# 設置自中斷時間

harakiri=30

# 設置緩衝

post-buffering=4096

# 設置日誌目錄

daemonize=/opt/script/uwsgi.log

 

 

 

 

配置nginx:

服務器上nginx已經部署了,直接修改配置文件

   

 server {
       server_name www.leslie.com;
        location /{
           include uwsgi_params;        ##加載uwsgi支持模塊
           uwsgi_connect_timeout 30;    ##超時時間
            uwsgi_pass unix:/opt /script/uwsgi.sock;    #轉到uwsgi這個進程
 
        }
        location/static/ {         ##靜態文件nginx自己處理
         alias/opt/online_edit_script/static/;
        indexindex.html index.htm;
         }
}


 

檢測語法:

[root@shouke script]# /application/nginx/sbin/nginx -t
nginx: the configuration file/application/nginx-1.6.3//conf/nginx.conf syntax is ok
nginx: configuration file/application/nginx-1.6.3//conf/nginx.conf test is successful


 

 

安裝mysql:

yum -y install mysql mysql-devel mysql-server mysql-client


 

pip3 install mysqlclient


 

修改配置文件將域名加入到允許訪問的列表裏面。

[root@shouke ~]# vim /opt/online_edit_script/online_edit_script/settings.py


spacer.gif

 

 

初始化uwsgi

 

Uwsgi –ini /opt/script/uwsgi.ini


 

會生成一個進程監聽本地的9001端口

tcp        0      0 127.0.0.1:9001              0.0.0.0:*                   LISTEN      19214/uwsgi


 

重啓nginx:

kill all nginx
/usr/local/nginx/sbin/nginx


 

訪問測試:

spacer.gif

OK!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章