centos7 下通过nginx+uwsgi部署django应用
生活随笔
收集整理的這篇文章主要介紹了
centos7 下通过nginx+uwsgi部署django应用
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1. 安裝python3.6
1. 獲取wget https://www.python.org/ftp/python/3.6.2/Python-3.6.2.tgz tar -xzvf Python-3.6.2.tgz -C /tmp cd /tmp/Python-3.6.2/2. 把Python3.6安裝到 /usr/local 目錄./configure --prefix=/usr/local make make altinstall3. 更改/usr/bin/python鏈接ln -s /usr/local/bin/python3.6 /usr/bin/python32. maridb
1. 安裝sudo yum install mariadb-server 2. 啟動, 重啟sudo systemctl start mariadbsudo systemctl restart mariadb3. 設置bind-ipvim /etc/my.cnf在 [mysqld]:下面加一行bind-address = 0.0.0.04. 設置外部ip可以訪問先進入mysql才能運行下面命令:mysql 直接進入就行GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123456' WITH GRANT OPTION;FLUSH PRIVILEGES5. 設置阿里云的對外端口視頻中有講解這部分6. 安裝mysqlclient出問題centos 7:yum install python-devel mariadb-devel -yubuntu:sudo apt-get install libmysqlclient-dev然后:pip install mysqlclient3. 安裝nginx
https://www.digitalocean.com/community/tutorials/how-to-install-nginx-on-centos-74. 安裝virtualenvwrapper
yum install python-setuptools python-devel pip install virtualenvwrapper 編輯.bashrc文件 export WORKON_HOME=$HOME/.virtualenvs source /usr/local/bin/virtualenvwrapper.sh 重新加載.bashrc文件 source ~/.bashrc新建虛擬環境 mkvirtualenv mxonline進入虛擬環境 workon mxonline安裝pip包 我們可以通過 pip freeze > requirements.txt 將本地的虛擬環境安裝包相信信息導出來然后將requirements.txt文件上傳到服務器之后運行:pip install -r requirements.txt 安裝依賴包5. 安裝uwsgi
pip install uwsgi6. 測試uwsgi
uwsgi --http :8000 --module MxOnline.wsgi7. 配置nginx
新建uc_nginx.conf# the upstream component nginx needs to connect to upstream django { # server unix:///path/to/your/mysite/mysite.sock; # for a file socket server 127.0.0.1:8000; # for a web port socket (we'll use this first) } # configuration of the serverserver { # the port your site will be served on listen 80; # the domain name it will serve for server_name 你的ip地址 ; # substitute your machine's IP address or FQDN charset utf-8;# max upload size client_max_body_size 75M; # adjust to taste# Django media location /media {alias 你的目錄/Mxonline/media; # 指向django的media目錄 }location /static {alias 你的目錄/Mxonline/static; # 指向django的static目錄 }# Finally, send all non-media requests to the Django server. location / {uwsgi_pass django;include uwsgi_params; # the uwsgi_params file you installed } }8. 將該配置文件加入到nginx的啟動配置文件中
sudo ln -s 你的目錄/Mxonline/conf/nginx/uc_nginx.conf /etc/nginx/conf.d/8. 拉取所有需要的static file 到同一個目錄
在django的setting文件中,添加下面一行內容:STATIC_ROOT = os.path.join(BASE_DIR, "static/") 運行命令python manage.py collectstatic9. 運行nginx
sudo /usr/sbin/nginx這里需要注意 一定是直接用nginx命令啟動, 不要用systemctl啟動nginx不然會有權限問題
10. 通過配置文件啟動uwsgi
新建uwsgi.ini 配置文件, 內容如下:# mysite_uwsgi.ini file[uwsgi]# Django-related settings# the base directory (full path)chdir = /home/bobby/Projects/MxOnline# Django's wsgi filemodule = MxOnline.wsgi# the virtualenv (full path)# process-related settings# mastermaster = true# maximum number of worker processesprocesses = 10# the socket (use the full path to be safesocket = 127.0.0.1:8000# ... with appropriate permissions - may be needed# chmod-socket = 664# clear environment on exitvacuum = truevirtualenv = /home/bobby/.virtualenvs/mxonlinelogto = /tmp/mylog.log注:chdir: 表示需要操作的目錄,也就是項目的目錄module: wsgi文件的路徑processes: 進程數virtualenv:虛擬環境的目錄workon mxonline uwsgi -i 你的目錄/Mxonline/conf/uwsgi.ini &訪問
http://你的ip地址/學習來自:http://www.projectsedu.com/,如有侵權請聯系我,馬上刪除
轉載于:https://www.cnblogs.com/xinjie57/p/10524188.html
總結
以上是生活随笔為你收集整理的centos7 下通过nginx+uwsgi部署django应用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 拿到BAT等大厂offer以后,我发现了
- 下一篇: 用turtle库画童年的记忆哆啦A梦