Centos 6.5部署nginx+uwsgi+django
生活随笔
收集整理的這篇文章主要介紹了
Centos 6.5部署nginx+uwsgi+django
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
Centos 6.5部署nginx+uwsgi+django
一、安裝python3,系統(tǒng)默認(rèn)是python2.6
1、安裝依賴軟件 yum -y install sqlite-devel yum -y install make zlib zlib-devel gcc-c++ libtool openssl openssl-devel 2、下載安裝python3.6 wget https://www.python.org/ftp/python/3.6.1/Python-3.6.1.tgz tar zxvf Python-3.6.1.tgz cd Python-3.6.1 ./configure --prefix=/usr/local/python3 make && make install 3、創(chuàng)建python及pip命令軟鏈接 ln -s /usr/local/python3/bin/python3 /usr/bin/python3 ln -s /usr/local/python3/bin/pip3 /usr/bin/pip3 4、測(cè)試 [root@localhost ~]# python3 Python 3.6.1 (default, Dec 21 2017, 16:14:49) [GCC 4.4.7 20120313 (Red Hat 4.4.7-18)] on linux Type "help", "copyright", "credits" or "license" for more information. >>> print('test_haha'); test_haha二、安裝django
1、訪問(wèn)鏈接下載最新django版本,https://www.djangoproject.com/download/ 2、安裝django依賴組件pytz pip3 install pytz-2017.3-py2.py3-none-any.w 3、安裝django tar zxvf Django-2.0.tar.gz cd Django-2.0 python3 setup.py install 4、測(cè)試 上傳django項(xiàng)目 cd django_project python3 manage.py runserver 0.0.0.0:80 訪問(wèn)http://127.0.0.1 可以訪問(wèn) 三、安裝uwsgi web網(wǎng)關(guān) 1、下載安裝uwsgi wget http://projects.unbit.it/downloads/uwsgi-2.0.1.tar.gz tar zxvf uwsgi-2.0.1.tar.gz cd uwsgi-2.0.1 python3 uwsgiconfig.py --build python3 uwsgiconfig.py --clean cp -R /root/uwsgi-2.0.1 /usr/local/uwsgi ln -s /usr/local/uwsgi/uwsgi /usr/bin/uwsgi 2、啟動(dòng),測(cè)試 創(chuàng)建測(cè)試文件 # vim test.py def application(env, start_response): start_response('200 OK', [('Content-Type','text/html')]) return [b"Hello World"] 啟動(dòng) /usr/local/uwsgi/uwsgi --http :9090 --wsgi-file /root/test.py 訪問(wèn)http://ip:9090 可以訪問(wèn)成功 3、uwsgi和django結(jié)合 uwsgi --http :9090 --chdir /home/code/Ticket_system --wsgi-file Ticket_system/wsgi.py --master --processes 4 --threads 2 --stats 127.0.0.1:9192 訪問(wèn)http:ip:9090 可以訪問(wèn)成功 #如果在訪問(wèn)django項(xiàng)目時(shí)靜態(tài)文件加載失敗可以先忽略,在nginx配置中會(huì)指定靜態(tài)文件路徑,配置后可以正常訪問(wèn) ps: --chdir /home/code/Ticket_system django項(xiàng)目目錄-
- http : 協(xié)議類型和端口號(hào)
- processes : 開(kāi)啟的進(jìn)程數(shù)量
- workers : 開(kāi)啟的進(jìn)程數(shù)量,等同于processes(官網(wǎng)的說(shuō)法是spawn the specified number ofworkers / processes)
- chdir : 指定運(yùn)行目錄(chdir to specified directory before apps loading)
- wsgi-file : 載入wsgi-file(load .wsgi file)
- stats : 在指定的地址上,開(kāi)啟狀態(tài)服務(wù)(enable the stats server on the specified?
-
- threads : 運(yùn)行線程。由于GIL的存在,我覺(jué)得這個(gè)真心沒(méi)啥用。(run each worker in prethreaded
- mode with the specified number of threads)
- master : 允許主進(jìn)程存在(enable master process)
- daemonize : 使進(jìn)程在后臺(tái)運(yùn)行,并將日志打到指定的日志文件或者udp服務(wù)器(daemonize uWSGI)。實(shí)際上最常用的,還是把運(yùn)行記錄輸出到一個(gè)本地文件上。
- pidfile : 指定pid文件的位置,記錄主進(jìn)程的pid號(hào)。
- vacuum : 當(dāng)服務(wù)器退出的時(shí)候自動(dòng)清理環(huán)境,刪除unix socket文件和pid文件(try to remove all of the generated file/sockets)
四、安裝nginx
1、下載安裝依賴第三方軟件pcre wget http://jaist.dl.sourceforge.net/project/pcre/pcre/8.34/pcre-8.34.tar.bz2 tar jxvf pcre-8.34.tar.bz2 cd pcre-8.34 ./configure --enable-utf8 make && make install 2、下載安裝依賴第三方軟件openssl 下載openssl-1.0.2h.tar.gz tar zxvf openssl-1.0.2h.tar.gz 3、下載安裝nginx nginx-1.9.9.tar.gz tar zxvf nginx-1.9.9.tar.gz cd nginx-1.9.9 ./configure --prefix=/usr/local/nginx --with-pcre --with-http_stub_status_module --with-http_ssl_module --with-openssl=/root/openssl-1.0.2h --with-http_gzip_static_module --with-http_sub_module --with-cc=/usr/bin/gcc make && makeinstall 4、新建www用戶 useradd -s /sbin/nologin -M www 5、修改配置文件 備份nginx配置文件 cd /usr/local/nginx/conf cp -f nginx.conf nginx.conf_bak vim nginx.conf user www;? ? ? ? ?#修改啟動(dòng)用戶為www worker_processes 4; ? ? ? ? #啟動(dòng)4個(gè)進(jìn)程,根據(jù)實(shí)際需求配置 6、啟動(dòng)nginx /usr/local/nginx/sbin/nginx 訪問(wèn)測(cè)試http://ip 默認(rèn)80端口,可以訪問(wèn)五、nginx+uwsgi+django三者結(jié)合
1、django項(xiàng)目中/根目錄下創(chuàng)建uwsgi.ini # vim uwsgi.ini [uwsgi] #uwsgi啟動(dòng)端口 socket = 127.0.0.1:9090? #django項(xiàng)目目錄 chdir=/home/code/Ticket_system? module=Ticket_system.wsgi master = true processes=2 threads=2 max-requests=2000 chmod-socket=664 vacuum=true # 日志路徑 daemonize = /home/code/Ticket_system/logs/uwsgi.log? 2、配置nginx # vim /usr/local/nginx/conf/nginx.conf server { listen 80; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; #指定靜態(tài)文件路徑,避免django項(xiàng)目中靜態(tài)文件無(wú)法加載 location /static { alias /home/code/Ticket_system/app01/static; } location / { #新增加兩個(gè)uwsgi配置 include uwsgi_params; uwsgi_pass 127.0.0.1:9090; root html; index index.html index.htm; } ....... #下邊必須操作,否則后臺(tái)管理admin的靜態(tài)資源會(huì)無(wú)法訪問(wèn) 1)第一種方法:找到django-admin靜態(tài)文件的目錄 #?python3? >>>?import?django?? >>>?django.__file__?? '/usr/local/python3/lib/python3.6/site-packages/Django-2.0.4-py3.6.egg/django/__init__.py' 找到根目錄之后我需要的絕對(duì)路徑為(不同版本可能不一樣,根據(jù)自己實(shí)際情況來(lái)) /usr/local/python3/lib/python3.6/site-packages/Django-2.0.4-py3.6.egg/django/contrib/admin/static 把static 拷貝到自己定義的static目錄,一般是在項(xiàng)目的根目錄下,或者應(yīng)用的根目錄下, 2)第二種方法,建立自己的靜態(tài)文件夾 修改settings,建立自己的文件夾 STATIC_ROOT?=?“/home/code/Ticket_system/static"?? 運(yùn)行下面命令把相關(guān)文件copy到這個(gè)目錄 python?manage.py?collectstatic ? 3、啟動(dòng)uwsgi uwsgi --ini /home/code/Ticket_system/uwsgi.ini ps:如果啟動(dòng)時(shí)報(bào)錯(cuò)uwsgi: error while loading shared libraries: libpcre.so.1: cannot open shared object file: No such file or directory 解決: cd /lib64
ln -s libpcre.so.0.0.1 libpcre.so.1
轉(zhuǎn)載于:https://www.cnblogs.com/zhangmeixia/p/8109986.html
總結(jié)
以上是生活随笔為你收集整理的Centos 6.5部署nginx+uwsgi+django的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: javascript的性能优化
- 下一篇: 集合(2)