阿里云上部署Django項(xiàng)目
1霹肝、首先阿里云上安裝Centos7系統(tǒng)
此處省略安裝流程
安裝好以后設(shè)置阿里云上面實(shí)例的端口叹话,開(kāi)放80和8080端口
2彬犯、安裝Apache服務(wù)器
yum install httpd httpd-devel -y
開(kāi)啟服務(wù)器:
systemctl? start? httpd.service
設(shè)置apache服務(wù)器隨centos開(kāi)機(jī)啟動(dòng)
systemctl enable? httpd.service
開(kāi)啟防火墻的80端口和8080端口
firewall-cmd --zone=public --add-port=80/tcp --permanent
firewall-cmd --zone=public --add-port=8080/tcp --permanent
firewall-cmd --reload
此步操作看需求脓魏,如果阿里云服務(wù)器沒(méi)有開(kāi)啟防火墻颤殴,此步操作可以跳過(guò)
重啟系統(tǒng)
reboot
然后可以再瀏覽器地址欄輸入對(duì)應(yīng)公網(wǎng)ip地址溶耘,如果出現(xiàn)apache測(cè)試頁(yè)面二拐,如下說(shuō)明apache安裝成功
2 、安裝python3以及創(chuàng)建虛擬環(huán)境
首先安裝python3的依賴(lài)包
yum install epel-release -y
yum groupinstall "Development tools" -y
yum install zlib-devel bzip2-devel openssl-devel ncurses-devel zx-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel -y
下載并安裝python3?
wget https://www.python.org/ftp/python/3.6.5/Python-3.6.5.tar.xz --no-check-certificate
tar xf Python-3.6.5.tar.xz``
cd Python-3.6.5
./configure --prefix=/usr/local --with-ensurepip=install --enable-shared LDFLAGS="-Wl,-rpath /usr/local/lib"
make && make altinstall
創(chuàng)建虛擬環(huán)境
先安裝 virtualenv模塊
pip3.6 install virtualenv
然后創(chuàng)建虛擬環(huán)境? venv汰具,此處django為django項(xiàng)目所處的位置
mkdir -p /var/www/html/django
cd /var/www/html/django
virtualenv -p /usr/local/bin/python3.6? venv
啟動(dòng)虛擬環(huán)境
source? /var/www/html/django/venv/bin/activate
然后在虛擬環(huán)境中安裝Django工程所需要的模塊
pip install django
pip install pymysql
pip install? mod_wsgi
導(dǎo)出 mod_wsgi模塊的位置:
LoadModule wsgi_module "/usr/lib64/httpd/modules/mod_wsgi-py36.cpython-36m-x86_64-linux-gnu.so"
將上段代碼添加到? /etc/httpd/conf下面的httpd.conf文件的最后一行
3卓鹿、創(chuàng)建配置文件
創(chuàng)建django.conf配置文件
vi /etc/httpd/conf.d/django.conf
將下面的代碼copy到該文件中
Alias /static /var/www/html/django/django2/static
<Directory /var/www/html/django/django2/static>
Require all granted
</Directory>
<Directory /var/www/html/django//django2/django2>
<Files wsgi.py>
? Require all granted
</Files>
</Directory>
WSGIPythonHome "/var/www/html/django/venv"
Listen 8080
<VirtualHost *:8080>
ServerName django.example.com
WSGIDaemonProcess /django2? python-path=/var/www/html/django/venv/lib/python3.6/site-pachages
WSGIScriptAlias / /var/www/html/django/django2/django2/wsgi.py
</VirtualHost>
此處django2為django工程的根目錄
4、上傳Django項(xiàng)目
先手修改django項(xiàng)目中的wsgipy文件
加入下列兩行代碼:
import sys
sys.path.append('/var/www/html/django/django2')
其次修改?settings.py文件里的ALLOWED_HOSTS :
ip為阿里云的公網(wǎng)ip
ALLOWED_HOSTS = ['ip','0.0.0.0']
修改項(xiàng)目屬主和權(quán)限
chmod -R 755 /var/www/html
chown -R apache:apache /var/www/html
重啟系統(tǒng):
reboot
此時(shí)就應(yīng)該可以通過(guò)公網(wǎng)ip加8080端口訪問(wèn)django項(xiàng)目了
---------------------
作者:廉少
來(lái)源:簡(jiǎn)書(shū)
原文:http://www.reibang.com/p/7c1a4344a217
版權(quán)聲明:本文為博主原創(chuàng)文章留荔,轉(zhuǎn)載請(qǐng)附上博文鏈接吟孙!