Django Nginx+uwsgi安装与配置(CENTOS环境)
使用python manage.py runserver运行服务器。这仅适合在测试环境中使用。
对于正式发布的服务,我们需要稳定且可持续的服务器,如apache、Nginx、lighttpd等。本文将以 Nginx 为例。
安装基础开发包
Centos下的安装步骤如下:
yum groupinstall "Development tools" yum install zlib-devel bzip2-devel pcre-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel
CentOS自带Python 2.4.3,但我们可以安装Python2.7.5:
cd ~ wget http://python.org/ftp/python/2.7.5/Python-2.7.5.tar.bz2 tar xvf Python-2.7.5.tar.bz2 cd Python-2.7.5 ./configure --prefix=/usr/local make && make altinstall
安装Python包管理
easy_install包 https://pypi.python.org/pypi/distribute
安装步骤:
cd ~ wget https://pypi.python.org/packages/source/d/distribute/distribute-0.6.49.tar.gz tar xf distribute-0.6.49.tar.gz cd distribute-0.6.49 python2.7 setup.py install easy_install --version
pip包:https://pypi.python.org/pypi/pip
安装pip的好处是可以pip list和pip uninstall进行Python包管理,easy_install没有这个功能,只能卸载
安装uwsgi
uwsgi:https://pypi.python.org/pypi/uWSGI
uwsgi参数详细:http://uwsgi-docs.readthedocs.org/en/latest/Options.html
pip install uwsgi uwsgi --version #查看 uwsgi 版本
测试uwsgi是否正常:
创建一个新文件test.py,内容如下:
def application(env, start_response): start_response('200 OK', [('Content-Type','text/html')]) return "Hello World"
然后运行它在终端中:
uwsgi --http :8001 --wsgi-file test.py
在浏览器中输入:http://127.0.0.1:8001,检查是否有“Hello World”输出。如果没有结果,请检查安装过程。
安装Django
pip install django
测试django是否正常。运行:
django-admin.py startproject demosite cd demosite python2.7 manage.py runserver 0.0.0.0:8002
在浏览器中输入:http://127.0.0.1:8002,检查django是否正常工作。
安装Nginx
安装命令如下:
cd ~ wget http://nginx.org/download/nginx-1.5.6.tar.gz tar xf nginx-1.5.6.tar.gz cd nginx-1.5.6 ./configure --prefix=/usr/local/nginx-1.5.6 \ --with-http_stub_status_module \ --with-http_gzip_static_module make && make install
了解更多信息,请阅读Nginx安装配置。?安装目录(如:/usr/local/nginx/),打开conf/nginx.conf文件,更改服务器配置:
server { listen 80; server_name localhost; location / { include uwsgi_params; uwsgi_pass 127.0.0.1:9090; //必须和uwsgi中的设置一致 uwsgi_param UWSGI_SCRIPT demosite.wsgi; //入口文件,即wsgi.py相对于项目根目录的位置,“.”相当于一层目录 uwsgi_param UWSGI_CHDIR /demosite; //项目根目录 index index.html index.htm; client_max_body_size 35m; } }
更多信息,您可以阅读Nginx安装配置。
设置完成后,在终端中运行:
uwsgi --ini /etc/uwsgi9090.ini & /usr/local/nginx/sbin/nginx
在浏览器中输入:http://127.0.0.1,可以看到django的“Working”。
版权声明
本文仅代表作者观点,不代表Code前端网立场。
本文系作者Code前端网发表,如需转载,请注明页面地址。
发表评论:
◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。