1.首先安装python
Python编译安装
主要介绍linux环境下安装
cd /usr/local/src //进入安装目录
wget https://www.python.org/ftp/python/3.6.2/Python-3.6.2.tar.xz
xz -d Python-3.6.2.tar.xz && tar xvf Python-3.6.2.tar //解压缩
cd Python-3.6.2 //进入解压目录
./configure --prefix=/usr/local/python
make && make install
/usr/local/python/bin/python3.6 --version //查看当前python版本
ln -s /usr/local/python/bin/python3.6 /usr/bin/python3.6 //做软链接
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
用uwsgi 启动django
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项目就可以实现高并发啦!