閱讀128 返回首頁    go 技術社區[雲棲]


Django Nginx Gunicorn Centos7 環境部署,將項目上線

序言:相信很多人遇到了這樣的問題,將自己的django項目完成後,準備部署到Linux上,遇到了很多的麻煩,網上的教程大多數使用python2的django項目,一旦用於python3後,就會出很多的bug。本教程詳細描述python3 Django 1.11項目上線的詳細過程

創建一個虛擬環境

cd /home/
mkdir djangoproject
cd ./djangoproject/
python -m venv djangoprojectenv

其中 python -m venv xxx 是python3自帶的一種創建xxx虛擬環境的方式,這裏我已經創建了一個 djangoprojectenv 虛擬環境

進入虛擬環境

source djangoprojectenv/bin/activate

可以發現已經進入了虛擬環境,在命令行前有一個(djangoprojectenv) 標識,例如:

(djangoprojectenv) [root@iz2zeb45dolegxb5nfuexez djangoproject]# 

安裝工具
使用pip時推薦大家使用豆瓣源,安裝速度會飛快,例如:

pip install -i https://pypi.douban.com/simple django

由於我使用的是阿裏雲服務器,用pip安裝應用時,會自動加速,所以采用默認方式,大家如果覺得自己下載速度慢,可以采用豆瓣源

pip install django
yum install mysql-devel 
pip install mysqlclient
pip install gunicorn

創建django項目

django-admin.py startproject djangoproject .
ls

可以看到目錄結構

djangoproject  djangoprojectenv  manage.py

注意:創建djangoproject . 後麵的 . 一定不要搞掉了

修改項目的settings

vim djangoproject/settings.py 

修改settings.py中的數據庫參數

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'djangoproject',
        'USER': 'root',
        'PASSWORD': 'password',
        'HOST': '127.0.0.1',
        'PORT': '3306',
        'OPTIONS': {
            'init_command': "SET sql_mode='STRICT_TRANS_TABLES'"
        }
    }
}

注意修改數據庫名、賬號和密碼

在settings.py 添加靜態文件路徑參數

STATIC_ROOT = os.path.join(BASE_DIR, "static/")

在settings.py 修改ip權限,允許所有ip訪問

ALLOWED_HOSTS = ["*"]

然後保存文件,按esc :wq

在數據庫中生成數據表

./manage.py makemigrations
./manage.py migrate

創建一個超級用戶

./manage.py createsuperuser

收集靜態文件

./manage.py collectstatic

這一步很重要,否則頁麵會亂碼
開啟Django項目

./manage.py runserver 0.0.0.0:8000

你可以在你的瀏覽器訪問服務器,輸入ip和port,格式如下:

https://server_domain_or_IP:8000

還可以訪問你的數據庫

https://server_domain_or_IP:8000/admin

登入你自己設置的superuser查看數據庫

測試Gunicorn是否能啟動你的項目服務

gunicorn --bind 0.0.0.0:8000 djangoproject.wsgi:application

訪問ip地址看瀏覽器是否能正常查看內容(此時沒有退出虛擬環境)
完成測試後,按CTRL-C 停止 Gunicorn 運行

退出虛擬環境

deactivate

創建一個 Gunicorn Systemd Service 文件

vim /etc/systemd/system/gunicorn.service

修改內容如下:

[Unit]
Description=gunicorn daemon
After=network.target

[Service]
User=root
Group=nginx
WorkingDirectory=/home/djangoproject
ExecStart=/home/djangoproject/djangoprojectenv/bin/gunicorn --workers 3 --bind unix:/home/djangoproject/djangoproject.sock djangoproject.wsgi:application

[Install]
WantedBy=multi-user.target

一定要主要自己的工程路經和虛擬環境路徑
WorkingDirectory與ExecStart 修改為自己的路徑

開啟Gunicorn服務並開機自啟

systemctl start gunicorn
systemctl enable gunicorn

配置nginx代理通過Gunicorn

vim /etc/nginx/nginx.conf

添加內容如下:

server {
    listen       80;
    server_name  server_domain_or_IP;

        location = /favicon.ico { access_log off; log_not_found off; }
        location /static/ {
        root /home/djangoproject;
        }

        location / {
        proxy_set_header Host $http_host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_pass https://unix:/home/djangoproject/djangoproject.sock;
        }

    }

注意修改自己的IP地址或域名,還有文件路徑
server_domain_or_IP 代表你的IP地址或域名

修改nginx的權限

usermod -a -G root nginx
chmod 710 /home/
nginx -t

如果沒有報錯,就行下一步操作

開啟nginx服務並開機自啟

systemctl start nginx
systemctl enable nginx

現在,你可以訪問你的ip了

注意:本文我是root用戶登陸的

本文轉載我的csdn博客: https://blog.csdn.net/lanhaixuanvv/article/details/78258680

最後更新:2017-10-17 14:03:25

  上一篇:go  8Manage:從企業管理軟件中反思電子招投標的安全
  下一篇:go  如何設置有利於網站優化的uRL結構?