阿里雲搭建django+nginx+uwsgi環境

阿里雲搭建django環境

更新源

sudo apt-get update
sudo apt-get upgrade

安裝nginx

sudo apt-get install nginx
sudo /etc/init.d/nginx start (start能夠改爲restart/stop)
#或是sudo service nginx start
而後瀏覽器輸入服務器IP,觀察是否有welcom to nginx!

安裝py3和virtualenv

sudo apt-get install git python3 python3-pip
sudo pip3 install virtualenv

修改python版本:

法一:(不建議)
$ gedit ~/.bashrc       #gedit .bash_aliases
在頂部加入一行alias python=python3
$ source ~/.bashrc      #或是source ~/.bash_aliases o
$ python --version
就會發現是Python 3.5.2啦

/*or
Open your .bashrc file nano ~/.bashrc. Type alias python=python3 on to a new line at the top of the file then save the file with ctrl+o and close the file with ctrl+x. Then, back at your command line type source ~/.bashrc. Now your alias should be permanent.
*/
Ubuntu16.04切換python3和python2
▲.切換Python3爲默認版本:(建議)
sudo update-alternatives --install /usr/bin/python python /usr/bin/python2 100
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3 150

切換Python2爲默認版本:html

sudo update-alternatives --config python

安裝虛擬環境

cd /var/www
sudo virtualenv env35

下載和配置網站

sudo git clone https://github.com/KyrieWang233/homework_submission-master.git
source env35/bin/activate
cd home...
sudo pip3 install -r requirements.txt
sudo python manage.py collectstatic
sudo python manage.py migrate
python manage.py createsuperuser
sudo vim settings.py將其中的ALLOWED_HOST=[*]改成本身的IP

而後輸入 python manage.py runserver 0.0.0.0:8000

安裝、測試uwsgi

sudo pip3 install uwsgi
uwsgi --http :8000 --module homework_submission.wsgi

編輯/var/www下的uwsgi.ini

[uwsgi]
chdir=/var/www/homework_submission-master
module=homework_submission.wsgi
home=/var/www/env35
master=True
processes=10
socket= :8001
chmod socket=666
vacuum=True
max-requests=5000

編輯/var/www下的nginx.conf

upstream django{
server 127.0.0.1:8001;
}
server {
listen 80;
server_name sast.nymrli.top;
charset utf-8;
client_max_body_size 75M;
location /static{
alias /var/www/homework_submission-master/static;  
}
location /{
uwsgi_pass django;
include /var/www/uwsgi_params;
}
}

編輯/var/www下的uwsgi_params

uwsgi_param QUERY_STRING    $query_string;
uwsgi_param REQUEST_METHOD  $request_method;
uwsgi_param CONTENT_TYPE    $content_type;
uwsgi_param CONTENT_LENGTH  $content_length;     

uwsgi_param REQUEST_URI         $request_uri;     
uwsgi_param PATH_INFO       $document_uri;     
uwsgi_param DOCUMENT_ROOT       $document_root;     
uwsgi_param SERVER_PROTOCOL     $server_protocol;     
uwsgi_param REQUEST_SCHEME  $scheme;     
uwsgi_param HTTPS           $https if_not_empty;     

uwsgi_param REMOTE_ADDR         $remote_addr;     
uwsgi_param REMOTE_PORT         $remote_port;     
uwsgi_param SERVER_PORT     $server_port;   
uwsgi_param SERVER_NAME     $server_name;

軟鏈接

cd /etc/nginx
cd sites-enabled
rm default
ln -s /var/www/nginx.conf homework_submission
ls

重啓nginx

/etc/init.d/nginx restartpython

運行uwsgi

cd /var/www
uwsgi --ini uwsgi.ini

讓uwsgi自啓動

vim /etc/rc.local(注意非虛擬環境也得安裝uwsgi模塊)nginx

添加下面代碼:
/usr/local/bin/uwsgi --ini /var/www/uwsgi.ini

exit 0

uwsgi的熱啓動

在uwsgi.ini中加入git

py-autoreload=1github

重啓一下:killall -9 uwsgi/usr/local/bin/uwsgi --ini /var/www/uwsgi.inishell


總結

關於etc/ linit. ddjango

若是你使用過inux系統,那麼你必定據說過 init. d目錄,這個目錄究竟是幹嗎的呢?它歸根結底只作了一件事情,但這件事情非同小可,是爲整個系統作的,所以它很是重要。init.d目錄包含許多系統各類服務的啓動和中止腳本ubuntu

關於 /etc/rc.localvim

rc.local也是我常常使用的一個腳本,該腳本是在系統初始化級別腳本運行以後再執行的,所以能夠安', '地在裏面添加你想在系統啓動以後執行的腳本.瀏覽器

總結

Linux是靈活的,正由於它的靈活性,咱們老是能夠找到許多不一樣的辦法來解決同一個問題,服務的例子就是一個很好的佐證,有了 /etc/init.d目錄下的腳本,再加上 /etc/rc. local這個利器,你能夠放心的確保你的服務能夠完美的啓動和運行

相關文章
相關標籤/搜索