nginx+uWSGI+django+virtualenv+superviso發佈web服務器

1.環境依賴

yum groupinstall "Development tools" -y
yum install zlib-devel bzip2-devel pcre-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel -y

2.安裝python3

https://www.cnblogs.com/zdqc/p/11788212.html

3.安裝virtualenv

https://www.cnblogs.com/zdqc/p/11788367.html

4.安裝django1.1

pip3 install django==1.11

5.安裝UWSGI

進入虛擬環境venv,安裝uwsgi
(venv) [root@slave 192.168.11.64 /opt]$pip3 install uwsgi
檢查uwsgi版本
(venv) [root@slave 192.168.11.64 /opt]$uwsgi --version
2.0.17.1
#檢查uwsgi python版本
uwsgi --python-versio

測試html

#啓動一個python
uwsgi --http :8000 --wsgi-file test.py

http :8000: 使用http協議,端口8000
wsgi-file test.py: 加載指定的文件,test.py

#test.py
def application(env, start_response):
    start_response('200 OK', [('Content-Type','text/html')])
    return [b"Hello World"] # python3

瀏覽器訪問ip端口號vue

熱加載python程序python

在啓動命令後面加上參數
uwsgi --http :8088 --module mysite.wsgi --py-autoreload=1 
#發佈命令
#command= /home/venv/bin/uwsgi --uwsgi 0.0.0.0:8000 --chdir /opt/mysite --home=/home/venv --module mysite.wsgi
#此時修改django代碼,uWSGI會自動加載django程序,頁面生效

運行django程序nginx

uwsgi --ini  /etc/uwsgi_nginx.ini
uwsgi支持ini、xml等多種配置方式,本文以 ini 爲例, 在/etc/目錄下新建uwsgi_nginx.ini,添加以下配置:

# mysite_uwsgi.ini file
[uwsgi]

# Django-related settings
# the base directory (full path)
chdir           = /opt/mysite
# Django's wsgi file
module          = mysite.wsgi
# the virtualenv (full path)
home            = /opt/venv
# process-related settings
# master
master          = true
# maximum number of worker processes
processes       = 1
# the socket (use the full path to be safe
socket          = 0.0.0.0:8000
# ... with appropriate permissions - may be needed
# chmod-socket    = 664
# clear environment on exit
vacuum          = true

uwsgi.ini

6.安裝nginx

echo "第一步下載" |pv -qL 25
mkdir /server/tools -p
cd /server/tools
wget http://nginx.org/download/nginx-1.12.2.tar.gz
tar xf nginx-1.12.2.tar.gz
cd nginx-1.12.2
yum install -y pcre-devel openssl-devel

echo "第二步建立虛擬用戶"|pv -qL 25
useradd -M -s /sbin/nologin www

echo "第三步編譯安裝"|pv -qL 25
./configure --prefix=/application/nginx-1.12.2 --user=www --group=www --with-http_ssl_module --with-http_stub_status_module
make && make install

echo "第四步軟鏈接" |pv -qL 25
ln -s /application/nginx-1.12.2 /application/nginx
echo "export PATH=$PATH:/application/nginx/sbin/" >>/etc/profile
sleep 3
source /etc/profile

echo "第五步啓動並檢查"|pv -qL 25
/application/nginx/sbin/nginx
sleep 3
ps -ef|grep nginx

配置文件:sql

worker_processes  1;

events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       80;
        server_name  192.168.119.12;
        location / {
        root /opt/07-luffy_project_01/dist;
        index index.html;
    #這一條參數確保vue頁面刷新時候,不會出現404頁面
      try_files $uri $uri/ /index.html;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
           root   html;
        }
    }

server {
listen       8000;
        server_name  192.168.119.12;
        location / {
        uwsgi_pass 0.0.0.0:9000;
        include /opt/nginx/conf/uwsgi_params;
        }
        location /static {
        alias /opt/static;
}
}
}

7.安裝supervisor

退出虛擬環境apache

yum install python-setuptools easy_install supervisor -y

經過命令生成supervisor的配支文件django

echo_supervisord_conf > /etc/supervisord.conf

而後再/etc/supervisord.conf末尾添加上以下代碼瀏覽器

[program:my]
#command=/opt/venv/bin/uwsgi --ini  /etc/uwsgi_nginx.ini  #這裏是結合virtualenv的命令 和supervisor的精髓!!!!
command= /home/venv/bin/uwsgi --uwsgi 0.0.0.0:8000 --chdir /opt/mysite --home=/home/venv --module mysite.wsgi
#--home指的是虛擬環境目錄  --module找到 mysite/wsgi.py

文件詳解tomcat

supervisord.conf配置文件參數解釋
[program:xx]是被管理的進程配置參數,xx是進程的名稱
[program:xx]
command=/opt/apache-tomcat-8.0.35/bin/catalina.sh run  ; 程序啓動命令
autostart=true       ; 在supervisord啓動的時候也自動啓動
startsecs=10         ; 啓動10秒後沒有異常退出,就表示進程正常啓動了,默認爲1秒
autorestart=true     ; 程序退出後自動重啓,可選值:[unexpected,true,false],默認爲unexpected,表示進程意外殺死後才重啓
startretries=3       ; 啓動失敗自動重試次數,默認是3
user=tomcat          ; 用哪一個用戶啓動進程,默認是root
priority=999         ; 進程啓動優先級,默認999,值小的優先啓動
redirect_stderr=true ; 把stderr重定向到stdout,默認false
stdout_logfile_maxbytes=20MB  ; stdout 日誌文件大小,默認50MB
stdout_logfile_backups = 20   ; stdout 日誌文件備份數,默認是10
; stdout 日誌文件,須要注意當指定目錄不存在時沒法正常啓動,因此須要手動建立目錄(supervisord 會自動建立日誌文件)
stdout_logfile=/opt/apache-tomcat-8.0.35/logs/catalina.out
stopasgroup=false     ;默認爲false,進程被殺死時,是否向這個進程組發送stop信號,包括子進程
killasgroup=false     ;默認爲false,向進程組發送kill信號,包括子進程

最後啓動supervisor,完成uWSGI啓動django,nginx反向代理app

supervisord -c /etc/supervisord.conf #啓動
supervisorsupervisorctl -c /etxc/supervisord.conf restart my  #重啓my項目
supervisorctl -c /etc/supervisord.conf [start|stop|restart] [program-name|all]

從新加載

1、添加好配置文件後

2、更新新的配置到supervisord    

supervisorctl update
3、從新啓動配置中的全部程序

supervisorctl reload
4、啓動某個進程(program_name=你配置中寫的程序名稱)

supervisorctl start program_name
5、查看正在守候的進程

supervisorctl
6、中止某一進程 (program_name=你配置中寫的程序名稱)

pervisorctl stop program_name
7、重啓某一進程 (program_name=你配置中寫的程序名稱)

supervisorctl restart program_name
8、中止所有進程

supervisorctl stop all
注意:顯示用stop中止掉的進程,用reload或者update都不會自動重啓。

8.靜態文件

settings.py

STATIC_ROOT='/opt/nginx1-12/static'
STATIC_URL = '/static/'
STATICFILES_DIRS=[
    os.path.join(BASE_DIR,"static"),
]

經過python3 manage.py collectstatic 收集全部你使用的靜態文件保存到STATIC_ROOT!

STATIC_ROOT 文件夾 是用來將全部STATICFILES_DIRS中全部文件夾中的文件,以及各app中static中的文件都複製過來
# 把這些文件放到一塊兒是爲了用nginx等部署的時候更方便
相關文章
相關標籤/搜索