1.首先安裝pythonhtml
主要介紹linux環境下安裝python
cd /usr/local/src //進入安裝目錄mysql
wget https://www.python.org/ftp/python/3.6.2/Python-3.6.2.tar.xzlinux
xz -d Python-3.6.2.tar.xz && tar xvf Python-3.6.2.tar //解壓縮nginx
cd Python-3.6.2 //進入解壓目錄sql
./configure --prefix=/usr/local/pythondjango
make && make installvim
/usr/local/python/bin/python3.6 --version //查看當前python版本併發
ln -s /usr/local/python/bin/python3.6 /usr/bin/python3.6 //作軟連接app
ln -s /usr/local/python/bin/pip3 /usr/bin/pip3
2.安裝Django
pip3 install Django
cd /usr/local/python/bin //進入文件夾
ln -s /usr/local/python/bin/django-admin /usr/bin/django-admin //建立django-admin 工程的軟連接
cd /home //進入任意一個目錄
django-admin startproject mysite //建立工程
進入mysite工程下同名目錄mysqite 修改settings 若是用的是mysql pip3 install pymysql
須要:
yum -y install mysql-devel
pip3 install mysqlclient
3.啓動Django
在settings中找到ALLOWED_HOSTS=[],加入主機名或者域名
python3.6 manager.py runserver ip:port
安裝uwsgi
pip3 install uwsgi
基本測試:
vim /home/test.py
# test.py def application(env, start_response): start_response('200 OK', [('Content-Type','text/html')]) return [b"Hello World"] # python3 #return ["Hello World"] # python2
運行:
/usr/local/python/bin/uwsgi --http :8000 --wsgi-file /home/test.py
cd mysite #進入django項目主目錄下 /usr/local/python/bin/uwsgi --http :8900 --module mysite.wsgi
也能夠把參數寫到配置文件裏
vim /home/EasyBlog.ini
[uwsgi] http = :9900 #the local unix socket file than commnuincate to Nginx socket = 127.0.0.1:9901 #socket nginx監聽端口 # the base directory (full path) chdir = /home/EasyBlog# Django's wsgi file wsgi-file = EasyBlog/wsgi.py # maximum number of worker processes processes = 4 #thread numbers startched in each worker process threads = 2 #monitor uwsgi status stats = 127.0.0.1:9191 # clear environment on exit vacuum = true
啓動
/usr/local/python/bin/uwsgi /home/EasyBlog.ini
安裝nginx配合nginx使用
vim /data/nginx/conf/uwsgi.conf
upstream django { server 127.0.0.1:9901; #端口爲監聽uwsgi的socket端口 } server { listen 80; server_name blog.fuzegame.com; charset utf-8; client_max_body_size 75M; # adjust to taste location /media { alias /path/to/your/mysite/media; # your Django project's media files - amend as required } location /static { alias /home/EasyBlog/static; # your Django project's static files - amend as required 配置靜態文件路徑 } location / { uwsgi_pass django; include /home/EasyBlog/uwsgi_params; # the uwsgi_params file you installed } }
重要:把 nginx conf配置文件夾下的 uwsgi_params 拷貝到 django 目錄下
cp /usr/local/nginx/conf/uwsgi_params /home/EasyBlog
在生產環境中,應該把django settings中的Debug= False
這樣配置完成後 訪問admin頁面時看不到靜態文件,能夠這樣操做
在 django settings 添加一行
STATIC_ROOT = 'all_static_files'
而後執行:
python3 manage.py collectstatic
執行完成後會在django工程目錄下 自動建立一個 all_static_files 目錄
最後還須要在nginx 中的django配置文件中 修改
location /static { alias /data/mysite/all_static_files; # 修改爲all_static_files }
重啓nginx
此時啓動Nginx 和Uwsgi,你的django項目就能夠實現高併發啦!