阿里云centos7 下通過nginx+uwsgi+mariadb部署django應(yīng)用


一.安裝python3.6

1. 獲取

wget https://www.python.org/ftp/python/3.6.3/Python-3.6.3.tgz

tar -xzvf Python-3.6.3.tgz -C? /tmp

cd? /tmp/Python-3.6.3/

2. 把Python3.6安裝到 /usr/local 目錄

yum install gcc

yum install zlib zlib-devel -y

./configure --prefix=/usr/local

make

make altinstall

3. 更改/usr/bin/python鏈接

????ln -s /usr/local/bin/python3.6 /usr/bin/python3

二. maridb

1. 安裝

? ? sudo yum install mariadb-server

2. 啟動(dòng), 重啟

? ? sudo systemctl start mariadb

? ? sudo systemctl restart mariadb

3. 設(shè)置bind-ip

? ? vim /etc/my.cnf

? ? 在 [mysqld]:

? ? ? ? 下面加一行

? ? ? ? bind-address = 0.0.0.0

4. 設(shè)置外部ip可以訪問

? ? 先進(jìn)入mysql才能運(yùn)行下面命令:

? ? ? ? mysql 直接進(jìn)入就行

? ? GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123456' WITH GRANT OPTION;

? ? FLUSH PRIVILEGES;

5. 設(shè)置阿里云的對(duì)外端口

6. 安裝mysqlclient出問題

? ? centos 7:

? ? ? ? yum install python-devel mariadb-devel -y

? ? ubuntu:

? ? ? ? sudo apt-get install libmysqlclient-dev

? ? 然后:

? ? ? ? pip install mysqlclient

7.mariadb設(shè)置初始密碼

https://blog.csdn.net/hyq776141352/article/details/69808662

8.本地?cái)?shù)據(jù)庫(kù)同步到mariadb(navicat)

全部數(shù)據(jù):右鍵本地?cái)?shù)據(jù)庫(kù)--數(shù)據(jù)傳輸---選擇要同步的數(shù)據(jù)庫(kù)

表結(jié)構(gòu):暫無(wú)

三. 安裝nginx

https://www.digitalocean.com/community/tutorials/how-to-install-nginx-on-centos-7

四. 安裝virtualenvwrapper

yum install python-setuptools python-devel

pip install virtualenvwrapper

編輯.bashrc文件,添加下面2行

export WORKON_HOME=$HOME/.virtualenvs

source/usr/bin/virtualenvwrapper.sh

重新加載.bashrc文件

source? ~/.bashrc

新建虛擬環(huán)境

mkvirtualenv --no-site-packages -p /usr/bin/python3?mxonline

(刪除命令rmvirtualenv)

進(jìn)入虛擬環(huán)境

workon mxonline (退出命令deactive)

安裝pip包

我們可以通過 pip freeze > requirements.txt 將本地的虛擬環(huán)境安裝包相信信息導(dǎo)出來

然后將requirements.txt文件上傳到服務(wù)器之后運(yùn)行:

pip install -r requirements.txt

安裝依賴包

python manage.py runserver 0.0.0.0:8000 運(yùn)行服務(wù)器測(cè)試

五. 安裝uwsgi

pip install uwsgi(虛擬環(huán)境下)

六. 測(cè)試uwsgi

https://www.cnblogs.com/teachen/p/9118926.html

setting配置

DEBUG =True

STATIC_URL ='/static/'

STATICFILES_DIRS = (

os.path.join(BASE_DIR, "static/mxshop"),)

STATIC_ROOT = os.path.join(BASE_DIR, 'static')

進(jìn)入服務(wù)器項(xiàng)目的文件目錄

python manage.py collectstatic 部署時(shí)收集靜態(tài)資源文件

uwsgi --http :8000 --module MxShop.wsgi?--static-map=/static=static? --static-map=/media=media(在沒有nginx情況下,映射靜態(tài)資源并啟動(dòng))

uwsgi --http :8000 --file MxShop/wsgi.py? -d uwsgi.log?

七. 配置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 server

server {

# 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

}

}

八. 將該配置文件加入到nginx的啟動(dòng)配置文件中

sudo ln -s 你的目錄/Mxonline/conf/nginx/uc_nginx.conf /etc/nginx/conf.d/

更改/etc/nginx/nginx.conf 里面user為user root;防止訪問靜態(tài)資源出現(xiàn)403錯(cuò)誤

九. 拉取所有需要的static file 到同一個(gè)目錄(已做過就不用)

在django的setting文件中,添加下面一行內(nèi)容:

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

運(yùn)行命令

? ? python manage.py collectstatic

十. 運(yùn)行nginx

sudo /usr/sbin/nginx

這里需要注意 一定是直接用nginx命令啟動(dòng), 不要用systemctl啟動(dòng)nginx不然會(huì)有權(quán)限問題

十一. 通過配置文件啟動(dòng)uwsgi

新建uwsgi.ini 配置文件, 內(nèi)容如下:

? ? # mysite_uwsgi.ini file

? ? [uwsgi]

? ? # Django-related settings

? ? # the base directory (full path)

? ? chdir? ? ? ? ? = /home/bobby/Projects/MxOnline

? ? # Django's wsgi file

? ? module? ? ? ? ? = MxOnline.wsgi

? ? # the virtualenv (full path)

? ? # process-related settings

? ? # master

? ? master? ? ? ? ? = true

? ? # maximum number of worker processes

? ? processes? ? ? = 10

? ? # the socket (use the full path to be safe

? ? socket? ? ? ? ? = 127.0.0.1:8000

? ? # ... with appropriate permissions - may be needed

? ? # chmod-socket? ? = 664

? ? # clear environment on exit

? ? vacuum? ? ? ? ? = true

? ? virtualenv = /home/bobby/.virtualenvs/mxonline

? ? logto = /tmp/mylog.log

注:

? ? chdir: 表示需要操作的目錄,也就是項(xiàng)目的目錄

? ? module: wsgi文件的路徑

? ? processes: 進(jìn)程數(shù)

? ? virtualenv:虛擬環(huán)境的目錄


workon mxonline

uwsgi --ini 你的目錄/Mxonline/conf/uwsgi.ini &

訪問

http://你的ip地址/

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?注:本文來自http://projectsedu.com/

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容