学了一下 python3,于是租了阿里云服务器,玩一玩.后来我才发现玩一玩装个 VirtualBox 就够了: https://ninghao.net/blog/1566 ,关键虚拟机可以任意装玩.
现在开始讲讲 centos7+nginx+python3+django 配置,我用的是 mac,新手可以先用 VirtualBox 练手,不行就删了重新生成一个虚拟机,把各种步骤都理解熟悉了,再到服务器上实战.一次性操作完难免有问题,一步一步来.
1. 租的服务器(选择 centos)的话,需要在阿里云后台控制台开放几个端口,克隆一下已开放的端口,tcp 自定义就行,mysql(3306),nginx(8000 以上都行).(都切换到 root 用户操作)
2. 安装 python3:http://www.mamicode.com/info-detail-2019356.html.
3. 安装 nginx: https://www.cnblogs.com/tangjiansheng/p/6928722.html.
4. 安装 mysql: https://www.cnblogs.com/bigbrotherer/p/7241845.html.(这一步如果暂时用不上数据库也可以不操作)
为了区分开发 python 版本环境,你可以用 pip3 装一下 virtualenv 以及 virtualenv 的管理工具 virtualenvwrapper(这步我没做,显得麻烦)
5. 确定 2,3 两步安装成功了,接下来就用 pip3 安装 django 和 uwsgi,如果 django 和 uwsgi 的命令不能用,就这样操作一下:(不熟悉 linux 命令可以先去了解一下,还有 vim 编辑器的使用)
ln -s /usr/local/python3/bin/django-admin /usr/bin/django-admin 和 ln -s /usr/local/python3/bin/uwsgi /usr/bin/uwsgi(或者 uwsgi3(区分版本))
在进行下一步之前建议你看一下这篇博客:http://blog.csdn.net/c465869935/article/details/53242126
还有官方文档:https://uwsgi-docs.readthedocs.io/en/latest/tutorials/Django_and_nginx.html(直接搜 uwsgi 官方文档(找到 Setting up Django and your web server with uWSGI and nginx)).
6. 测试 uwsgi 能否正常运行:
随便找个干净的目录下 vim test.py, 新建一个 py 文件,在里面写上:
def application(env, start_response):
start_response('200 OK', [('Content-Type','text/html')]) return "Hello World".encode()并保存.然后在当前目录下执行:uwsgi --http :8000 --wsgi-file test.py.接着在你电脑的浏览器上输入你虚拟机或者服务器的 ip:8000, 或者 ctrl+n 新建一个终端,进入你的虚拟机(或者服务器)执行:curl http://127.0.0.1:8000, 能显示 Hello World 说明 uwsgi 没问题.7. 随便找个干净的目录 (/py36_projects/) django-admin
startproject pyDemo 新建一个 django 项目
cd pyDemo/pyDemo 进入项目里面 编辑 settings.py : vim settings.py ,在里面加上:
ALLOWED_HOSTS = ['test.xq.com', 'localhost', '127.0.0.1', '虚拟机自己的 ip']#test.xq.com为你自己买的域名,
暂时可以不用,
用虚拟机自己的ip或者服务器的ip就行同时加上: STATIC_ROOT = os.path.join(BASE_DIR, 'static')
并保存 执行 cd .. 切换到 pyDemo 项目的根目录,能看到 manage.py 即可,然后执行:python3 manage.py collectstatic.
在同一目录下新建 uwsgi 的配置文件: vim uwsgi.ini , 在里面写上:
[uwsgi]
socket = 127.0.0.1:8001
chdir=/py36_projects/pyDemo
module=pyDemo.wsgi
master = true
processes=2
threads=2
max-requests=2000
chmod-socket=664
vacuum=true
daemonize = /py36_projects/pyDemo/uwsgi.log
并保存 8. 剩下的只有配置 nginx 的配置文件,vim /etc/nginx/nginx.conf, 进入 nginx.conf 配置文件看看有没有下面这句代码:
include /etc/nginx/conf.d/*.conf;(意思是导入 / etc/nginx/conf.d / 下的所有配置文件)
于是我们只要在 / etc/nginx/conf.d / 目录下:
cd /etc/nginx/conf.d
新建一个 conf 就行:vim pyDemo.conf(名字随便取),里面写上:
upstream django {
# server unix:///path/to/your/mysite/mysite.sock; # for a file socket server 127.0.0.1:8001; # for a web port socket (we'll use this first)
}
# 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 location /static { alias /py36_projects/pyDemo/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 /etc/nginx/uwsgi_params; # the uwsgi_params file you installed }
}
并保存
一些说明:
listen 8000; 阿里云服务器的话为上面提到的阿里云后台控制台添加的端口(对外的端口)
server_name localhost; localhost 可以替换你购买的域名(如前面 django 的配置文件里的 test.xq.com)
/py36_projects/pyDemo/ 是我的项目路径
uwsgi_pass django; 里面的 server 127.0.0.1:8001; 和上面的 uwsgi.ini 配置文件的 socket = 127.0.0.1:8001 的端口一致,这个端口 8000 以上把(不要和 nginx 配置的端口相同就行)
include /etc/nginx/uwsgi_params; 引入 / etc/nginx / 目录下的 uwsgi_params 文件,首先你要到该路径下看有没有这个文件,默认是有的,没有就新建一个并写上:
uwsgi_param QUERY_STRING $query_string;
uwsgi_param REQUEST_METHOD $request_method;
uwsgi_param CONTENT_TYPE $content_type;
uwsgi_param CONTENT_LENGTH $content_length;
uwsgi_param REQUEST_URI $request_uri;
uwsgi_param PATH_INFO $document_uri;
uwsgi_param DOCUMENT_ROOT $document_root;
uwsgi_param SERVER_PROTOCOL $server_protocol;
uwsgi_param REQUEST_SCHEME $scheme;
uwsgi_param HTTPS $https if_not_empty;
uwsgi_param REMOTE_ADDR $remote_addr;
uwsgi_param REMOTE_PORT $remote_port;
uwsgi_param SERVER_PORT $server_port;
uwsgi_param SERVER_NAME $server_name;
并保存
你也可以在 server 里面加上 access_log 和 error_log 配置参数,定义 nginx 访问日志和错误日志的存放路径. (暂时可不做)
9. 以上基本配置好了,验证的准备:先执行(一步一步来):
pkill -9 uwsgi
pkill -9 nginx
然后切换到项目根目录:
cd /py36_projects/pyDemo 执行:
uwsgi --ini uwsgi.ini
最后执行:
systemctl start nginx 或者 nginx -c /etc/nginx/nginx.conf(都是启动 nginx 的)
10. 开始验证:
打开浏览器:输入你虚拟机或者服务器的 ip:8000, 像我的就是:120.79.14.122:8008 或者 120.79.14.122:8008/admin,
显示的页面分别是:
这样就配置成功了,切记
http://blog.csdn.net/c465869935/article/details/53242126
还有官方文档:https://uwsgi-docs.readthedocs.io/en/latest/tutorials/Django_and_nginx.html(直接搜 uwsgi 官方文档(找到 Setting up Django and your web server with uWSGI and nginx)).
浏览这两篇文章很重要,对你的理解有帮助
来源: http://www.bubuko.com/infodetail-2468158.html