Centos 6.5部署nginx+uwsgi+django
生活随笔
收集整理的這篇文章主要介紹了
Centos 6.5部署nginx+uwsgi+django
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Centos 6.5部署nginx+uwsgi+django
一、安裝python3,系統默認是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、創建python及pip命令軟鏈接 ln -s /usr/local/python3/bin/python3 /usr/bin/python3 ln -s /usr/local/python3/bin/pip3 /usr/bin/pip3 4、測試 [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、訪問鏈接下載最新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、測試 上傳django項目 cd django_project python3 manage.py runserver 0.0.0.0:80 訪問http://127.0.0.1 可以訪問 三、安裝uwsgi web網關 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、啟動,測試 創建測試文件 # vim test.py def application(env, start_response): start_response('200 OK', [('Content-Type','text/html')]) return [b"Hello World"] 啟動 /usr/local/uwsgi/uwsgi --http :9090 --wsgi-file /root/test.py 訪問http://ip:9090 可以訪問成功 3、uwsgi和django結合 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 訪問http:ip:9090 可以訪問成功 #如果在訪問django項目時靜態文件加載失敗可以先忽略,在nginx配置中會指定靜態文件路徑,配置后可以正常訪問 ps: --chdir /home/code/Ticket_system django項目目錄-
- http : 協議類型和端口號
- processes : 開啟的進程數量
- workers : 開啟的進程數量,等同于processes(官網的說法是spawn the specified number ofworkers / processes)
- chdir : 指定運行目錄(chdir to specified directory before apps loading)
- wsgi-file : 載入wsgi-file(load .wsgi file)
- stats : 在指定的地址上,開啟狀態服務(enable the stats server on the specified?
-
- threads : 運行線程。由于GIL的存在,我覺得這個真心沒啥用。(run each worker in prethreaded
- mode with the specified number of threads)
- master : 允許主進程存在(enable master process)
- daemonize : 使進程在后臺運行,并將日志打到指定的日志文件或者udp服務器(daemonize uWSGI)。實際上最常用的,還是把運行記錄輸出到一個本地文件上。
- pidfile : 指定pid文件的位置,記錄主進程的pid號。
- vacuum : 當服務器退出的時候自動清理環境,刪除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;? ? ? ? ?#修改啟動用戶為www worker_processes 4; ? ? ? ? #啟動4個進程,根據實際需求配置 6、啟動nginx /usr/local/nginx/sbin/nginx 訪問測試http://ip 默認80端口,可以訪問五、nginx+uwsgi+django三者結合
1、django項目中/根目錄下創建uwsgi.ini # vim uwsgi.ini [uwsgi] #uwsgi啟動端口 socket = 127.0.0.1:9090? #django項目目錄 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; #指定靜態文件路徑,避免django項目中靜態文件無法加載 location /static { alias /home/code/Ticket_system/app01/static; } location / { #新增加兩個uwsgi配置 include uwsgi_params; uwsgi_pass 127.0.0.1:9090; root html; index index.html index.htm; } ....... #下邊必須操作,否則后臺管理admin的靜態資源會無法訪問 1)第一種方法:找到django-admin靜態文件的目錄 #?python3? >>>?import?django?? >>>?django.__file__?? '/usr/local/python3/lib/python3.6/site-packages/Django-2.0.4-py3.6.egg/django/__init__.py' 找到根目錄之后我需要的絕對路徑為(不同版本可能不一樣,根據自己實際情況來) /usr/local/python3/lib/python3.6/site-packages/Django-2.0.4-py3.6.egg/django/contrib/admin/static 把static 拷貝到自己定義的static目錄,一般是在項目的根目錄下,或者應用的根目錄下, 2)第二種方法,建立自己的靜態文件夾 修改settings,建立自己的文件夾 STATIC_ROOT?=?“/home/code/Ticket_system/static"?? 運行下面命令把相關文件copy到這個目錄 python?manage.py?collectstatic ? 3、啟動uwsgi uwsgi --ini /home/code/Ticket_system/uwsgi.ini ps:如果啟動時報錯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
轉載于:https://www.cnblogs.com/zhangmeixia/p/8109986.html
總結
以上是生活随笔為你收集整理的Centos 6.5部署nginx+uwsgi+django的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: javascript的性能优化
- 下一篇: Openfire on Centos7