针对系统中自带的Python2.7版本
- 1.安装python-devel
yum install python-devel
- 2.安装uwsgi
pip install uwsgi
- 3.测试uwsgi是否能正常工作
# 1.新建一个index.py
[root@iZss911l4lr8aaZ ~]# touch index.py
[root@iZss911l4lr8aaZ ~]# vi index.py
# index.py
def application(env, start_response):
start_response('200 OK', [('Content-Type','text/html')])
return "Hello World"
# 2.
uwsgi --http :8000 --wsgi-file index.py
浏览器访问8000端口看是否有hello world输出
注意:确保8000端口能被外网访问
错误: 不能访问
解决: 在阿里云的安全组中添加8000端口的访问权限
- 4.测试Django能否正常工作
# 1
[root@iZss911l4lr8aaZ /]# pip install django
[root@iZss911l4lr8aaZ var]# mkdir /var/www
[root@iZss911l4lr8aaZ /]# cd /var/www
$ django-admin startproject mysite
$ cd mysite
$ python manage.py runserver 0.0.0.0:8000
错误 :Error: That port is already in use.
解决:
[root@iZss911l4lr8aaZ mysite]# netstat -apn|grep 8000
tcp 0 0 0.0.0.0:8000 0.0.0.0:* LISTEN 10127/uwsgi
[root@iZss911l4lr8aaZ mysite]# kill -9 10127
# 2
浏览器访问8000端口看是否有hello world输出
错误:
解决:
[root@iZss911l4lr8aaZ mysite]# vim mysite/settings.py
添加:
ALLOWED_HOSTS = [u'112.74.207.218']
在django的setting文件中,添加下面一行内容:
STATIC_ROOT = os.path.join(BASE_DIR, "static/")
然后运行:
python manage.py collectstatic
- 5.测试uwsgi是否能和django集成
uwsgi --http :8000 --chdir=/var/www/mysite --module mysite.wsgi
错误:bind(): Address already in use [core/socket.c line 769]
解决:
[root@iZss911l4lr8aaZ mysite]# netstat -apn|grep 8000
tcp 0 0 0.0.0.0:8000 0.0.0.0:* LISTEN 10194/python
[root@iZss911l4lr8aaZ mysite]# kill -9 10194
- 6.配置Nginx,使Nginx能为Django提供服务
# 1
yum install nginx
# 2
[root@iZss911l4lr8aaZ mysite]# touch /etc/nginx/conf.d/mysite_nginx.conf
[root@iZss911l4lr8aaZ mysite]# cp /etc/nginx/uwsgi_params /var/www/mysite/uwsgi_params
[root@iZss911l4lr8aaZ mysite]# vim /etc/nginx/conf.d/mysite_nginx.conf
upstream django {
server 127.0.0.1:8000;
}
server {
listen 80;
server_name 112.74.207.218;
charset utf-8;
client_max_body_size 75M;
location /media {
alias /var/www/mysite/media;
}
location /static {
alias /var/www/mysite/static;
}
location / {
uwsgi_pass django;
include /var/www/mysite/uwsgi_params;
}
}
# 3
检查配置文件是否出错
nginx -t
# 4
启动nginx服务
CentOS 7启动服务的命令是
systemctl restart nginx.service
错误:502 错误
解决:执行下面一个命令,启动uwsgi。
uwsgi --socket :8000 --chdir=/var/www/mysite --module mysite.wsgi
阿里云的防火墙网址
参考