环境:

CentOS7、python-3.5.3、Nignx 1.10.3 、Django 1.10.6、uWSGI 2.0.14

django项目目录:/var/webRoot/p1

项目结构:

基础包安装

yum install -y bind-utils traceroute wget man sudo ntp ntpdate screen patch make gcc gcc-c++ flex bison zip unzip ftp net-tools --skip-broken

关联动态库

# vi /etc/ld.so.conf                         添加如下内容

include /etc/ld.so.conf.d/*.conf

/usr/local/lib

/usr/local/lib64

/lib

/lib64

/usr/lib

/usr/lib64

编辑完ld.so.conf,执行

# ldconfig

使动态库生效

安装pcre

下载最新的pcre包

http://pcre.org/

#cd /usr/local/src

tar -zxvf pcre-8.40.tar.gz

cd pcre-8.40

./configure --enable-jit; make; make install

ldconfig

安装openssl

下载最新的openssl包

https://www.openssl.org/

cd /usr/local/src

tar -zxvf openssl-1.0.2k.tar.gz

cd openssl-1.0.2k

./config; make; make install

ldconfig

安装python

yum -y install sqlite-devel

下载XZ compressed source tarball    Python-3.5.3.tar.xz 到 /usr/local/src

cd /usr/local/src; tar -Jxvf Python-3.5.3.tar.xz; cd Python-3.5.3;

./configure --prefix=/usr/local/python_3.5.3; make; make install

python -c "import ssl; print(ssl.OPENSSL_VERSION)"    #打印python中ssl模块版本

添加环境变量

在 /etc/profile添加下面这行

export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/usr/local/python_3.5.3/bin

重载环境变量  . /etc/profile

若想让 python3 成为默认的python

可以先查询 原来的python路径,然后把原来的路径做软链接指向新的python3路径即可,可直接向/bin做python3版本的链接 ln -s /usr/local/python_3.5.3/bin/python3.5

which python

修复pip3 list格式告警问题

mkdir ~/.pip

echo -en "[list]\nformat=columns" > ~/.pip/pip.conf

安装django

pip3 install django

安装uwsgi

pip3 install uwsgi

uwsgi --version  # 可查看到uwsgi版本号

创建运行uwsgi的用户

useradd uwsgi -M -s /sbin/nologin

id  uwsgi

uid=(uwsgi) gid=(uwsgi) groups=(uwsgi)

测试

uwsgi --http :8000 --chdir /var/webRoot/project/ --wsgi-file project/wsgi.py

curl http://127.0.0.1:8000 可以测试浏览页面

uwsgi 配置

mkdir /etc/uwsgi

vi  /etc/uwsgi/uwsgi9090.ini

[uwsgi]
socket = 127.0.0.1:9090
chdir = /var/webRoot/p1/
wsgi-file = p1/wsgi.py
;virtualenv = /opt/pyevn27/ ;虚拟环境的路径
uid = 1005 ;指定运行的用户,指定运行的用户与组,root用户不用指定uid与gid
gid = 1005
master=True
vacuum=True
processes=5
max-requests=10000
pidfile = /var/run/uwsgi9090.pid
daemonize = /var/log/uwsgi/uwsgi9090.log

注意:

/var/webRoot/p1/目录及 /var/webRoot/p1/db.sqlite3  所属者与所属组应与上面的 用户与组对应

可参考https://docs.djangoproject.com/en/1.10/howto/deployment/wsgi/uwsgi/

uwsgi启动脚本:

Emperor模式
uWSGI的Epreror模式可以用来管理机器上部署的uwsgi服务,在这种模式下,会有一个特殊的进程对其它部署的服务进行监视。我们将所有配置文件(ini或xml文件)统一放到一个文件夹(如:/etc/uwsgi)中,然后启动Emperor模式:

uwsgi --emperor /etc/uwsgi

用systemd管理uwsgi服务

新建  /etc/systemd/system/uwsgi.service 文件,内容如下

[Unit]
Description=uWSGI Emperor
After=syslog.target [Service]
ExecStart=/usr/local/python_3.5.3/bin/uwsgi --emperor /etc/uwsgi
Restart=always
KillSignal=SIGQUIT
Type=notify
StandardError=syslog
NotifyAccess=all [Install]
WantedBy=multi-user.target

systemctl enable uwsgi

systemctl start uwsgi

安装nginx

yum -y install zlib zlib-devel gd gd-devel --skip-broken

添加一个不能登录且没有主目录的用户Nginx

#useradd nginx -M -s /sbin/nologin

下载nginx 最新稳定版

cd /usr/local/src

tar -zxvf nginx-1.10.3.tar.gz; cd nginx-1.10.3

./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module --with-http_ssl_module --with-pcre=/usr/local/src/pcre-8.40 --with-http_realip_module --with-http_image_filter_module --with-http_gzip_static_module --with-openssl=/usr/local/src/openssl-1.0.2k --with-openssl-opt="enable-tlsext" --with-stream --with-stream_ssl_module --with-http_v2_module

make; make install

nginx错误编译处理:

make[1]: *** [/usr/local/src/openssl-1.1.0e/.openssl/include/openssl/ssl.h] Error 255

原因: /usr/local/src/openssl-1.1.0e/.openssl路径不存在。

解决方法:打开 nginx解压源文件的下的 auto/lib/openssl/conf

CORE_INCS="$CORE_INCS $OPENSSL/.openssl/include"
CORE_DEPS="$CORE_DEPS $OPENSSL/.openssl/include/openssl/ssl.h"
CORE_LIBS="$CORE_LIBS $OPENSSL/.openssl/lib/libssl.a"
CORE_LIBS="$CORE_LIBS $OPENSSL/.openssl/lib/libcrypto.a"
CORE_LIBS="$CORE_LIBS $NGX_LIBDL"

修改成以下代码:

CORE_INCS="$CORE_INCS $OPENSSL/include"
CORE_DEPS="$CORE_DEPS $OPENSSL/include/openssl/ssl.h"
CORE_LIBS="$CORE_LIBS $OPENSSL/libssl.a"
CORE_LIBS="$CORE_LIBS $OPENSSL/libcrypto.a"
CORE_LIBS="$CORE_LIBS $NGX_LIBDL"

重新编译即可

添加ngxin到系统服务,设置nginx开启启动

vi /etc/rc.d/init.d/nginx                    #编辑启动文件添加下面内容

=======================================================

#!/bin/bash

# nginx Startup script for the Nginx HTTP Server

# it is v.0.0.2 version.

# chkconfig: - 85 15

# description: Nginx is a high-performance web and proxy server.

# It has a lot of features, but it's not for everyone.

# processname: nginx

# pidfile: /var/run/nginx.pid

# config: /usr/local/nginx/conf/nginx.conf

nginxd=/usr/local/nginx/sbin/nginx

nginx_config=/usr/local/nginx/conf/nginx.conf

nginx_pid=/usr/local/nginx/logs/nginx.pid

RETVAL=0

prog="nginx"

# Source function library.

. /etc/rc.d/init.d/functions

# Source networking configuration.

. /etc/sysconfig/network

# Check that networking is up.

[ "${NETWORKING}" = "no" ] && exit 0

[ -x $nginxd ] || exit 0

# Start nginx daemons functions.

start() {

if [ -e $nginx_pid ];then

echo "nginx already running...."

exit 1

fi

echo -n $"Starting $prog: "

daemon $nginxd -c ${nginx_config}

RETVAL=$?

echo

[ $RETVAL = 0 ] && touch /var/lock/subsys/nginx

return $RETVAL

}

# Stop nginx daemons functions.

stop() {

echo -n $"Stopping $prog: "

killproc $nginxd

RETVAL=$?

echo

[ $RETVAL = 0 ] && rm -f /var/lock/subsys/nginx /usr/local/nginx/logs/nginx.pid

}

reload() {

echo -n $"Reloading $prog: "

#kill -HUP `cat ${nginx_pid}`

killproc $nginxd -HUP

RETVAL=$?

echo

}

# See how we were called.

case "$1" in

start)

start

;;

stop)

stop

;;

reload)

reload

;;

restart)

stop

start

;;

status)

status $prog

RETVAL=$?

;;

*)

echo $"Usage: $prog {start|stop|restart|reload|status|help}"

exit 1

esac

exit $RETVAL

=======================================================

:wq!              #保存退出

chmod 755 /etc/rc.d/init.d/nginx              #赋予文件执行权限

chkconfig nginx on                   #设置开机启动

/etc/rc.d/init.d/nginx restart                   #重启

service nginx restart

CentOS 7 nginx启动脚本

vi /usr/lib/systemd/system/nginx.service  #编辑启动文件添加下面内容

[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network.target remote-fs.target nss-lookup.target [Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /etc/nginx/nginx.conf
ExecStart=/usr/local/nginx/sbin/nginx -c /etc/nginx/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true [Install]
WantedBy=multi-user.target

systemctl enable nginx #设置开机启动

systemctl start nginx.service #启动

nginx配置 

/usr/local/nginx/conf/conf.d/p1.conf

upstream control_cc_com {
server 127.0.0.1: weight= max_fails=;
} server {
listen ;
server_name localhost;
charset utf-;
access_log logs/p1.log main; location /static/system/ {
alias /var/webRoot/p1/upload/system/;
} location /static/user_image/ {
alias /var/webRoot/p1/upload/user_image/;
} location /static {
alias /var/webRoot/p1/static;
} location / {
include uwsgi_params;
uwsgi_pass control_cc_com;
}
}

nginx.conf

user nginx nginx;
worker_processes auto;
worker_cpu_affinity auto; error_log logs/error.log notice;
pid logs/nginx.pid;
worker_rlimit_nofile ; events {
use epoll;
worker_connections ;
} http {
include mime.types;
default_type application/octet-stream;
log_format main '$http_x_forwarded_for`$remote_addr`$proxy_add_x_forwarded_for`[$time_local]`"$request"`'
'$status`$body_bytes_sent`"$http_referer"`'
'"$http_user_agent"`"$request_time"`'
'$http_x_request_id`$upstream_response_time`$upstream_addr`$upstream_connect_time`$upstream_status'; log_format access '$remote_addr`[$time_local]`"$request"`'
'$status`$body_bytes_sent`"$http_referer"`'
'"$http_user_agent"`"$http_x_forwarded_for"`'
'$http_x_request_id`$upstream_response_time`$upstream_addr`$upstream_connect_time`$upstream_status'; # proxy_ignore_client_abort on;
proxy_headers_hash_max_size ;
proxy_headers_hash_bucket_size ;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout ;
server_tokens off; gzip on;
gzip_min_length 1k;
gzip_buffers 16k;
gzip_http_version 1.1;
gzip_comp_level ;
gzip_types text/plain application/x-javascript text/css application/xml;
gzip_vary on; client_max_body_size 100m;
client_body_buffer_size 128k;
client_body_temp_path /dev/shm/client_body_temp;
proxy_connect_timeout ;
proxy_read_timeout ;
proxy_send_timeout ;
proxy_buffer_size 16k;
proxy_buffers 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
proxy_temp_path /dev/shm/proxy_temp; include /etc/nginx/conf.d/*.conf; }

django admin静态文件加载不了问题

在settings.py 添加下面设置

先把setttings中的 STATICFILES_DIRS 注释

STATIC_ROOT = os.path.join(BASE_DIR, "static")
STATICFILES_FINDERS = (
"django.contrib.staticfiles.finders.FileSystemFinder",
"django.contrib.staticfiles.finders.AppDirectoriesFinder"
)

cd /var/webRoot/p1

python3 manage.py collectstatic    #复制 admin静态文件拷到当前目录 static目录

会生成 ./static/admin  目录,并把上面的配置删除或注释掉

时区设置,请注意大小写,到Linux生产系统时遇到这个问题。

TIME_ZONE = 'Asia/Shanghai'

python Django Nginx+ uWSGI 安装配置的更多相关文章

  1. 吴裕雄--天生自然Django框架开发笔记:Django Nginx+uwsgi 安装配置

    Django Nginx+uwsgi 安装配置 使用 python manage.py runserver 来运行服务器.这只适用测试环境中使用. 正式发布的服务,需要一个可以稳定而持续的服务器,比如 ...

  2. Django Nginx+uwsgi 安装配置

    使用 python manage.py runserver 来运行服务器.这只适用测试环境中使用. 正式发布的服务,我们需要一个可以稳定而持续的服务器,比如apache, Nginx, lighttp ...

  3. linux下Django Nginx+uwsgi 安装配置

    原文链接 在前面的章节中我们使用 python manage.py runserver 来运行服务器.这只适用测试环境中使用. 正式发布的服务,我们需要一个可以稳定而持续的服务器,比如apache, ...

  4. Install Python+Django+Nginx+UWSGI

    一.软件环境: CentOS6.6_64bit 需要用到的软件: [root@django tools]# ll 总用量 33336 -rw-r--r-- 1 root root 7497785 3月 ...

  5. CentOS 6.5下安装Python+Django+Nginx+uWSGI

    1.安装Python31.1先安装zlib库及其他三方库安装uWSGI时需要使用zlib,否则执行python uwsgiconfig.py --build时会报ImportError,就是因为在安装 ...

  6. Nginx+uwsgi安装配置

    一.安装基础开发包 yum groupinstall "Development tools" yum install zlib-devel bzip2-devel pcre-dev ...

  7. ubuntu配置Python-Django Nginx+uwsgi 安装配置

    安装Nginx sudo apt-get install nginx ubantu安装完Nginx后,文件结构大致为: 所有的配置文件都在 /etc/nginx下: 启动程序文件在 /usr/sbin ...

  8. django+nginx+uwsgi 部署配置

    django官方文档在这 https://docs.djangoproject.com/en/1.9/howto/deployment/wsgi/uwsgi/ 第一步:先收集静态文件 之前要先设置 S ...

  9. webpy+nginx+uwsgi安装配置

    转:(1)安装Nginx1.1 下载nginx-1.0.5.tar.gz并解压1.2 ./configure (也可以增加--prefix= path指定安装路径)此时有可能会提示缺少pcre支持,如 ...

随机推荐

  1. dp练习(4)——过河卒

    1010 过河卒 2002年NOIP全国联赛普及组  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold 题解       题目描述 Description 如图,A ...

  2. ES profile 性能优化用——返回各个shard的耗时

    Profile API 都说要致富先修路,要调优当然需要先监控啦,elasticsearch在很多层面都提供了stats方便你来监控调优,但是还不够,其实很多情况下查询速度慢很大一部分原因是糟糕的查询 ...

  3. 微信公众号平台开发(二)信息的分类.md

    在上一篇博客中,我们只是简单地与微信服务器建立了连接,接下来就是从微信服务器中接收信息了.在SecurityController中,我定义了两个方法(get和post).Get方法是我们用来与微信服务 ...

  4. ArrayList Vector LinkedList(一)

    ArrayList Vector LinkedList 区别与用法 ArrayList 和Vector是采用数组方式存储数据,此数组元素数大于实际存储的数据以便增加和插入元素,都允许直接序号索引元素, ...

  5. laravel中通过查询构造器,实现数据的curd

    //查询构造器: public function query1(){ //利用查询构造器,插入数据: /*$num=DB::table('student')->insert( ['name'=& ...

  6. jsp jsp九个内置对象

       每个JSP页面在第一次被访问的时候,WEB容器都会把请求交给JSP引擎(即一个JAVA)程序.JSP引擎先将JSP翻译成一个_jspServlet实质也是一次servlet,然后按照 Servl ...

  7. poj3481

    题解: splay操作 读入速度太慢,导致超时... 用字符串gets操作 代码: #pragma GCC optimize(2) #include<cstdio> #include< ...

  8. UAC 注册表 WIN64 OS 运行时主题

    首先EXE程序是32位,DelphiIDE对Project默认是启用主题的,默认情况在WIN64 OS下运行时,无管理员权限. WIN64 OS,默认情况下UAC是启用的. 上述默认情况下,EXE 是 ...

  9. 线程局部存储TLS

    1 .使用线程局部存储的理由 当我们希望这个进程的全局变量变为线程私有时,而不是所有线程共享的,也就是每个线程拥有一份副本时,这时候就可以用到线程局部存储(TLS,Thread Local Stora ...

  10. 【DevExpress v17.2新功能预告】DevExpress ASP.NET Scheduler新的自适应功能

    自适应Web设计可以帮助您解决各种尺寸的屏幕问题,网站的自适应网页设计可帮助您解决用户使用不同大小屏幕显示数据的问题. 在v17.2中,我们最大化了ASP.NET Scheduler的视图和可视化元素 ...