安装
1.Nginx的安装
brew nginx
安装过程中,有两句提示需要注意:
The default port has been set in /usr/local/etc/nginx/nginx.conf to 8080 so that
nginx can run without sudo. # 说明了nginx.conf的位置及默认为8080端口。
nginx will load all files in /usr/local/etc/nginx/servers/. # 说明了nginx加载文件的位置。
2.uWSGI安装
pip uwsgi
文件配置
1.将/usr/local/etc/nginx/
文件夹下面的uwsgi_params
文件复制至Django项目文件夹下面(与manage.py
处于同一文件级)
2.在项目文件夹下新建mysite_uwsgi.ini
文件(vi mystic_uwsgi.ini
),写入如下代码:
[uwsgi]
socket = 127.0.0.1:3031 # uwsgi运行的网络接口,与nginx.conf中的配置必须一致。
chdir = /path/to/my/Django/project/ # Django项目文件夹路径
wsgi-file = project/wsgi.py
processes = 4
threads = 2
stats = 127.0.0.1:9191
3.在项目文件夹下新建mysite_nginx.conf
文件,写入如下代码:
# mysite_nginx.conf
# the upstream component nginx needs to connect to
upstream django {
# server unix:///path/to/your/mysite/mysite.sock; # for a file socket
server 127.0.0.1:3031; # 与mysite_uwsgi.ini文件中的socket配置一致。
}
# configuration of the server
server {
# the port your site will be served on
listen 8000;
# the domain name it will serve for
server_name localhost; # substitute your machine's IP address or FQDN 真实场景此处填写服务器地址
charset utf-8;
# max upload size
client_max_body_size 75M; # adjust to taste
# Django media
location /media {
alias /path/to/my/Django/project/media; # your Django project's media files - amend as required
}
location /static {
alias /path/to/my/Django/project/static; # your Django project's static files - amend as required
}
# Finally, send all non-media requests to the Django server.
location / {
uwsgi_pass django;
include /path/to/my/Django/project/uwsgi_params; # the uwsgi_params file you installed
}
}
接着,将mysite_nginx.conf
文件软连接至/usr/local/etc/nginx/servers/
文件夹,在终端输入如下指令:
ln -s /path/to/my/Django/project/mysite_nginx.conf
/usr/local/etc/nginx/servers/
以上命令可能需要加入sudo
服务器启动
1.终端输入nginx
即可启动Nginx服务器。退出输入nginx -s quit
。
2.cd
进入/path/to/my/Django/project
文件夹,输入uwsgi mysite_uwsgi.ini
启动uWSGI服务器。退出应同时按键ctrl c
。
注意事项
我遇见了一个大坑的事,我之前上一个远程主机用的是阿里云ECS,选的是经典网络。后来专有网络的ECS有大优惠,就转到专有网络了。现在我在专有网络上建一个网站,域名也添加解析了,nginx也安装了,但网站就是访问不了,不能出现nginx的欢迎页面,困扰了我很久。最终,让我发现专有网络比以前的经典网络多了一个叫安全组的东西,默认的安全组策略是限制http80端口访问的,需要自己开通。真是大坑啊!