1. 程式人生 > >Centos7部署Django+Uwsgi+Nginx環境

Centos7部署Django+Uwsgi+Nginx環境

關閉防火牆

systemctl stop firewalld
systemctl disable firewalld

 安裝wget工具

yum install -y wget

 安裝nginx

連結:https://www.cnblogs.com/hulue/p/9171995.html

安裝完成後確定可以訪問到nginx頁面

然後安裝python3及django+uwsgi

#安裝的python3.6
wget https://www.python.org/ftp/python/3.6.6/Python-3.6.6.tgz
tar xf Python-3.6.6.tgz 
cd Python-3.6.6
./configure --prefix=/usr/local/python3
make && make install

#建python3軟連結,python2和python3共存的
ln -s /usr/local/python3/bin/python3.6 /usr/bin/python3

#看能否執行python3
python3
    Python 3.6.6 (default, Oct 18 2018, 15:49:46) 
    [GCC 4.8.5 20150623 (Red Hat 4.8.5-28)] on linux
    Type "help", "copyright", "credits" or "license" for more information.


#建pip3軟連結
ln -s /usr/local/python3/bin/pip3 /usr/bin/pip3

#通過pip3安裝django和uwsgi
pip3 install django    #最新版2.1.2
pip3 install uwsgi

#建uwsgi軟連結
ln -s /usr/local/python3/bin/uwsgi /usr/bin/uwsgi

 配置django專案

#安裝django專案需要的包
pip3 install psutil
pip3 install include

mkdir /mnt/www/
cd /mnt/www/

#我通過xshell本地上傳專案,專案名:cmdb1,app名:cmdb1
yum install -y lrzsz
rz
unzip cmdb1
#將django專案的表提交到資料庫
cd cmdb1
python3 manage.py makemigrations
python3 manage.py migrate

#直接用django執行專案
python3 manage.py runserver “主機IP”:9999

#然後使用瀏覽器訪問 “主機IP”:9999,看是否能訪問的

 編輯uwsgi啟動django專案檔案

vim cmdb1.xml

   <uwsgi>
   <socket>127.0.0.1:9999</socket><!-- 內部埠,自定義 -->
   <chdir>/mnt/www/cmdb1/</chdir><!-- 專案路徑 -->
   <module>cmdb1.wsgi</module>
   <processes>2</processes> <!-- 程序數 -->
   <daemonize>uwsgi.log</daemonize><!-- 日誌檔案 -->
   </uwsgi>

 編輯nginx配置檔案

 

vim /application/nginx/conf/nginx.conf

    location / {
       include uwsgi_params;
       uwsgi_pass 127.0.0.1:9999;
       uwsgi_param UWSGI_SCRIPT cmdb1.wsgi;
       uwsgi_param UWSGI_CHDIR /mnt/www/cmdb1;
           
    }
    location /static/ {
    alias /mnt/www/cmdb1/static/; 
    }

 執行服務

#啟動uwsgi + django ,一起啟動的,不用單獨啟動了
uswgi -x cmdb1.xml
#啟動nginx
#先檢查配置檔案沒有問題再啟動
/application/nginx/sbin/nginx -t
/application/nginx/sbin/nginx -s reload  #重新啟動


#然後訪問ngxin的80埠就可以訪問到了