centos下配置django、uwsgi和nginx(亲测成功)
2017-10-20 17:22
681 查看
服务器版本为centos7.4,默认看本博客的人会基本的django开发,即知道如何使用pip安装django及venv虚拟环境并新建项目以及使用django自带的wsgi.py启动项目。
本教程使用的是root用户,并不推荐。
1、更新系统:
yum update
yum upgrade
2、安装nginx
如果能成功通过yum install nginx安装最好(也最简单),但是由于公司限制,我使用的是nginx源码进行安装,如果你是通过yum安装,直接跳到2.6。
2.1、安装make和g++
yum -y install gcc automake autoconflibtool make
yum install gcc gcc-c++
2.2、安装PCRF库,自行百度下载tar.gz包,放在自己的路径下
tar -zxvf pcre-8.41.tar.gz
cd pcre-8.41
./configure
make
make install
2.3、安装zlib库,自行百度下载tar.gz包,放在自己的路径下
tar -zxvf zlib-1.2.11.tar.gz
cd zlib-1.2.11
./configure
make
make install
2.4、安装ssl,自行百度下载tar.gz包,放在自己的路径下
tar –zxvf openssl-1.1.0f.tar.gz
2.5、安装nginx,自行百度下载tar.gz包,放在自己的路径下
此处我们把nginx安装在/usr/local/nginx 目录下
tar -zxvf nginx-1.12.2.tar.gz
cd nginx-1.12.2
./configure --sbin-path=/usr/local/nginx/nginx\
--conf-path=/usr/local/nginx/nginx.conf\
--pid-path=/usr/local/nginx/nginx.pid\
--with-http_ssl_module \
--with-pcre=/xxxxx/pcre-8.41 \
--with-zlib=/xxxxx/zlib-1.2.11 \
--with-openssl=/xxxxx/openssl-1.1.0f
make
make install
注意,此处的/xxxx/即为上面安装各库时选择的路径。
安装成功后/usr/local/nginx 目录下如下
2.6、查看80端口有没有被占用:
netstat -ano|grep 80
2.7、启动(如果是通过yum命令装的,默认应该是/usr/sbin/nginx)
/usr/local/nginx/nginx
在浏览器中访问ip,出现下图即为成功。
3、安装uwsgi
进入虚拟目录
cd /home/xxxx/venv
开启虚拟环境
source bin/activate
安装uwsgi
yum install python-devel(跳过这步直接安装uwsgi报错,从stackoverflow上找到的解决方案,如果还报错看看是不是没有c编译环境,安装就好了)
pip install uwsgi
4、django和uwsgi结合
进虚拟环境,自己新建一个project hello,用python manage.py runserver 0.0.0.0:8000测试是否能正常访问。
然后关闭服务器,此处假设目录为/root/django/env/hello,运行以下命令:
常用选项如下所示:
http : 协议类型和端口号
processes : 开启的进程数量
workers : 开启的进程数量,等同于processes(官网的说法是spawn the specified number ofworkers / processes)
chdir : 指定运行目录(chdir to specified directory before apps loading)
wsgi-file : 载入wsgi-file(load .wsgi file)
stats : 在指定的地址上,开启状态服务(enable the stats server on the specified
address)
threads : 运行线程。由于GIL的存在,我觉得这个真心没啥用。(run each worker in prethreaded
mode with the specified number of threads)
master : 允许主进程存在(enable master process)
daemonize : 使进程在后台运行,并将日志打到指定的日志文件或者udp服务器(daemonize uWSGI)。实际上最常用的,还是把运行记录输出到一个本地文件上。
pidfile : 指定pid文件的位置,记录主进程的pid号。
vacuum : 当服务器退出的时候自动清理环境,删除unix socket文件和pid文件(try to remove all of the generated file/sockets)
注意:–wsgi-file后面跟的是相对目录
此时在浏览器中输入:http://ip:8008/,出现如下界面,表示两者结合成功 :
由于参数很多,我们需要把它们写入到一个配置文件中,在项目的app目录下新建 hello_uwsgi.ini,输入:
保存后,运行(这步请将上面红色字体的socket改为http,socket是配置与nginx通信的(tcp协议),等下一步跟
nginx结合的时候再改回为socket,这里我们浏览器测试要改为http协议):
uwsgi --inihello_uwsgi.ini
在浏览器同样输入http://ip:8008/,看到it
works的页面,说明配置成功。
5、django与uwsgi和nginx结合
进入虚拟环境,在项目根目录(此处是/root/xxxx/venv/project)新建static文件夹,运行
python manage.py collectstatic命令将所有静态文件收集到static文件夹内。
修改/usr/local/nginx/nginx.conf(yum安装应该是/etc/nginx/nginx.conf)(以后静态文件就由nginx来处理,
走8092端口,动态资源走8008端口,去服务器请求)
# add 这里如果是多台主机,ip都要写上,并且这几台主机的nginx配置必须是一样的
upstream pacap {
#sticky;
server 1.1.1.1:8008 max_fails=2 fail_timeout=5s;
server 1.1.1.1:8008 max_fails=2 fail_timeout=5s;
}
# modify
server {
listen 8092;
server_name localhost;
root /usr/share/nginx/html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
#charset koi8-r;
#access_log logs/host.access.log main;
location /showdb {
charset utf-8;
include uwsgi_params;
uwsgi_pass pacap;
uwsgi_read_timeout 1800;
}
location /static/ {
root /home/xxxx/venv/mysite/;
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
运行命令(先确认uwsgi和nginx的进程已经全部kill掉)
在浏览器访问http://ip:8092,看到it works页面即成功。
本教程使用的是root用户,并不推荐。
1、更新系统:
yum update
yum upgrade
2、安装nginx
如果能成功通过yum install nginx安装最好(也最简单),但是由于公司限制,我使用的是nginx源码进行安装,如果你是通过yum安装,直接跳到2.6。
2.1、安装make和g++
yum -y install gcc automake autoconflibtool make
yum install gcc gcc-c++
2.2、安装PCRF库,自行百度下载tar.gz包,放在自己的路径下
tar -zxvf pcre-8.41.tar.gz
cd pcre-8.41
./configure
make
make install
2.3、安装zlib库,自行百度下载tar.gz包,放在自己的路径下
tar -zxvf zlib-1.2.11.tar.gz
cd zlib-1.2.11
./configure
make
make install
2.4、安装ssl,自行百度下载tar.gz包,放在自己的路径下
tar –zxvf openssl-1.1.0f.tar.gz
2.5、安装nginx,自行百度下载tar.gz包,放在自己的路径下
此处我们把nginx安装在/usr/local/nginx 目录下
tar -zxvf nginx-1.12.2.tar.gz
cd nginx-1.12.2
./configure --sbin-path=/usr/local/nginx/nginx\
--conf-path=/usr/local/nginx/nginx.conf\
--pid-path=/usr/local/nginx/nginx.pid\
--with-http_ssl_module \
--with-pcre=/xxxxx/pcre-8.41 \
--with-zlib=/xxxxx/zlib-1.2.11 \
--with-openssl=/xxxxx/openssl-1.1.0f
make
make install
注意,此处的/xxxx/即为上面安装各库时选择的路径。
安装成功后/usr/local/nginx 目录下如下
2.6、查看80端口有没有被占用:
netstat -ano|grep 80
2.7、启动(如果是通过yum命令装的,默认应该是/usr/sbin/nginx)
/usr/local/nginx/nginx
在浏览器中访问ip,出现下图即为成功。
3、安装uwsgi
进入虚拟目录
cd /home/xxxx/venv
开启虚拟环境
source bin/activate
安装uwsgi
yum install python-devel(跳过这步直接安装uwsgi报错,从stackoverflow上找到的解决方案,如果还报错看看是不是没有c编译环境,安装就好了)
pip install uwsgi
4、django和uwsgi结合
进虚拟环境,自己新建一个project hello,用python manage.py runserver 0.0.0.0:8000测试是否能正常访问。
然后关闭服务器,此处假设目录为/root/django/env/hello,运行以下命令:
uwsgi --http :8008 --chdir /root/django/env/hello --wsgi-file hello/wsgi.py --master --processes 4 --threads 2 --stats 127.0.0.1:8000
常用选项如下所示:
http : 协议类型和端口号
processes : 开启的进程数量
workers : 开启的进程数量,等同于processes(官网的说法是spawn the specified number ofworkers / processes)
chdir : 指定运行目录(chdir to specified directory before apps loading)
wsgi-file : 载入wsgi-file(load .wsgi file)
stats : 在指定的地址上,开启状态服务(enable the stats server on the specified
address)
threads : 运行线程。由于GIL的存在,我觉得这个真心没啥用。(run each worker in prethreaded
mode with the specified number of threads)
master : 允许主进程存在(enable master process)
daemonize : 使进程在后台运行,并将日志打到指定的日志文件或者udp服务器(daemonize uWSGI)。实际上最常用的,还是把运行记录输出到一个本地文件上。
pidfile : 指定pid文件的位置,记录主进程的pid号。
vacuum : 当服务器退出的时候自动清理环境,删除unix socket文件和pid文件(try to remove all of the generated file/sockets)
注意:–wsgi-file后面跟的是相对目录
此时在浏览器中输入:http://ip:8008/,出现如下界面,表示两者结合成功 :
由于参数很多,我们需要把它们写入到一个配置文件中,在项目的app目录下新建 hello_uwsgi.ini,输入:
# hello_uwsgi.ini file [uwsgi] # Django-related settings socket= :8008 # the base directory (full path) chdir = /root/django/env/hello # Django s wsgi file module = hello.wsgi # process-related settings # master master = true # maximum number of worker processes processes = 4 # ... with appropriate permissions - may be needed # Django s wsgi file module = hello.wsgi # process-related settings # master master = true # maximum number of worker processes processes = 4 # ... with appropriate permissions - may be needed # clear environment on exit vacuum = true # chmod-socket = 664 # add uwsgi log daemonize = /xxxxx/uwsgi.log
保存后,运行(这步请将上面红色字体的socket改为http,socket是配置与nginx通信的(tcp协议),等下一步跟
nginx结合的时候再改回为socket,这里我们浏览器测试要改为http协议):
uwsgi --inihello_uwsgi.ini
在浏览器同样输入http://ip:8008/,看到it
works的页面,说明配置成功。
5、django与uwsgi和nginx结合
进入虚拟环境,在项目根目录(此处是/root/xxxx/venv/project)新建static文件夹,运行
python manage.py collectstatic命令将所有静态文件收集到static文件夹内。
修改/usr/local/nginx/nginx.conf(yum安装应该是/etc/nginx/nginx.conf)(以后静态文件就由nginx来处理,
走8092端口,动态资源走8008端口,去服务器请求)
# add 这里如果是多台主机,ip都要写上,并且这几台主机的nginx配置必须是一样的
upstream pacap {
#sticky;
server 1.1.1.1:8008 max_fails=2 fail_timeout=5s;
server 1.1.1.1:8008 max_fails=2 fail_timeout=5s;
}
# modify
server {
listen 8092;
server_name localhost;
root /usr/share/nginx/html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
#charset koi8-r;
#access_log logs/host.access.log main;
location /showdb {
charset utf-8;
include uwsgi_params;
uwsgi_pass pacap;
uwsgi_read_timeout 1800;
}
location /static/ {
root /home/xxxx/venv/mysite/;
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
运行命令(先确认uwsgi和nginx的进程已经全部kill掉)
uwsgi --ini /root/django/env/hello/hello_uwsgi.ini & /usr/local/nginx/nginx
在浏览器访问http://ip:8092,看到it works页面即成功。
相关文章推荐
- centos7 nginx配置httpsCenos(6.6/7.1)下从源码安装Python+Django+uwsgi+nginx环境部署(二)
- centos6.5下配置django+uwsgi+nginx
- centos6.6+django+nginx+uwsgi环境配置
- centos7.4下配置django+uwsgi+nginx
- Mac os X 配置 django + uWSGI + nginx 简单构建过程分享
- nginx+django+uwsgi部署配置
- Debian环境 Python + Django + Nginx + uWSGI + MySQL 配置备忘
- CentOS7.2 nginx和uwsgi部署django项目步骤:
- Nginx+uwsgi+django+RabbitMQ+celery安装配置
- centos 部署 Nginx + uwsgi + django +mysql 多站点
- Ubuntu12.04 + Nginx + uWSGI + Django1.4配置
- uWSGI+Nginx+Django安装和配置
- nginx + uwsgi + django 配置经历
- Ubuntu Server 14.04.2 LTS 配置 Nginx + uwsgi + Django
- Django + nginx + uwsgi配置和环境搭建(ubuntu)
- 腾讯云Ubuntu 16.04 配置Django+Uwsgi+Nginx
- nginx uwsgi django supervisor 服务器配置
- django开发个人简易Blog—nginx+uwsgin+django1.6+mysql 部署到CentOS6.5
- uWSGI + Nginx配置Django项目(一)
- Django+uwsgi+nginx环境配置方法