Mac下Django+gunicorn+Nginx服务器部署

来源:互联网 发布:网络诈骗在哪里报警 编辑:程序博客网 时间:2024/04/29 18:39

最近在学习python服务器开发,需要将写好的程序部署到服务器上,查找了一番,最终决定用Nginx服务器,但是配置的时候遇到了一些问题,折腾了一天,最终配置成功,现将配置方法纪录下来,我用的是MAC电脑,步骤如下:

一、安装brew:
启动命令行,运行如下代码:

uby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" 

不出意外,brew会安装成功,接下来安装nginx。

二、安装nginx:
在命令行依次执行下列代码,安装nginx:

brew search nginxbrew install nginx

执行上述代码后,系统会自动安装nginx,当系统提示:

Summary��  /usr/local/Cellar/nginx/1.4.2: 7 files, 920K, built in 2.1 minutes

说明已经安装完毕,可以在命令行输入nginx -v来查看nginx版本,如果版本显示正常,说明已经安装成功。

安装成功后,输入nginx命令启动nginx服务器,在浏览器输入localhost:8080可以看到如下界面:

Welcome to nginx!

If you see this page, the nginx web server is successfully installed and working. Further configuration is required.

For online documentation and support please refer to nginx.org.
Commercial support is available at nginx.com.

Thank you for using nginx.

nginx安装在/usr/local/Cellar/nginx 中,配置文件在/usr/local/etc/nginx中。

常用命令:

nginx -s reload配置文件后重启

nginx -t 查看配置文件是否正确

nginx -V 查看版本,以及配置文件地址

nginx -v 查看版本

nginx -c filename 指定配置文件

nginx -h 帮助

nginx -s [reload\reopen\stop\quit]

关闭

  查询nginx主进程号

  ps -ef | grep nginx

  从容停止 kill -QUIT 主进程号

  快速停止 kill -TERM 主进程号

  强制停止 kill -9 nginx

  若nginx.conf配置了pid文件路径,如果没有,则在logs目录下

  kill -信号类型 ‘/usr/local/nginx/logs/nginx.pid’

homebrew 常用的指令:

brew search mysql : 搜索具体的程序包

brew install mysql : 安装具体的程序包

brew info mysql : 查看具体程序的信息

brew uninstall mysql : 卸载具体的应用(这里只是用mysql 作个例子

三、安装gunicorn

在命令行输入

pip install gunicorn

安装gunicorn

四、配置nginx

在第二步说明的/usr/local/etc/nginx文件夹中找到nginx.conf文件,修改其中的内容:

server {        listen       8000;        server_name  localhost;        #charset koi8-r;        #access_log  logs/host.access.log  main;        location / {                proxy_pass http://127.0.0.1:8000;                proxy_set_header Host $host;                proxy_set_header X-Real-IP $remote_addr;                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;        }    location /static/ {                root /Users/tianweitao/PycharmProjects/weihu; #Django项目所在目录        }

五、配置Django

在你的Django应用的setting.py文件的INSTALLED_APPS中添加gunicorn应用如下所示:

INSTALLED_APPS = (    'django.contrib.admin',    'django.contrib.auth',    'django.contrib.contenttypes',    'django.contrib.sessions',    'django.contrib.messages',    'django.contrib.staticfiles',    'httpservers',    'gunicorn')

将上述步骤都配置完之后,运行Django应用,就可以通过nginx服务器访问了。

主要参考:

http://blog.csdn.net/cssmhyl/article/details/9717189
http://www.cnblogs.com/flysun/p/4017668.html
http://www.cnblogs.com/jianxie/p/3990377.html

0 0