CentOS 7 上搭建Nginx、uWSGI、Django项目

💖 作者

AuthorE-mailBlog
冬酒暖阳mailto:1067764354@qq.com博客:www.lifepoem.cn

安装环境

  • centOS 7.8
  • Python 3.6.9
  • Django 2.2.6
  • uWSGI 2.0.18
  • Nginx 1.9.9

步骤

1. 关闭 防火墙

[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# systemctl disable firewalld   #开机禁用firewall服务

2. 关闭 SElinux

[root@localhost ~]# getenforce  #查看SELinux状态
Enforcing
[root@localhost ~]# setenforce 0      #临时关闭SElinux
[root@localhost ~]# getenforce 
Permissive

[root@localhost ~]# vim /etc/selinux/config    #永久关闭"SELINUX=enforcing" 改为 "SELINUX=disabled",保存后退出,重启才会生效

3. 安装基础环境包

yum install gcc-c++

(为centos系统增加编译功能)

yum install wget openssl-devel bzip2-devel expat-devel gdbm-devel readline-devel sqlite-devel

(安装这些模块都是为了成功编译安装python3,防止出现各种异常)

4. 编译安装 Python

# 切换到 home 目录
cd /home
# 下载 Python 源码包
wget https://www.python.org/ftp/python/3.6.9/Python-3.6.9.tgz
# 下载完成后,执行解压指令
tar -zxvf Python-3.6.3.tar.gz
cd Python-3.6.9
# 编译安装,依次执行下面的命令
# 将python3安装到/usr/local/python3/路径下
./configure --prefix=/usr/local/python3
make -j2
make install -j2
# 创建 python3 和 pip3 的软链接
ln -s /usr/local/python3/bin/python3.9 /usr/bin/python3
ln -s /usr/local/python3/bin/pip3 /usr/bin/pip3

5. 安装 django 和 uwsgi 及 及创建uwsgi的配置文件

pip3 install django
# 若 django 项目需要其他的第三方库,自行一并安装
pip3 install uwsgi
ln -s /usr/local/python3/bin/uwsgi /usr/bin/uwsgi
# (为了在任何位置使用uwsgi命令,创建uwsgi软链)
  • 将 django 项目放置在 /home/www 目录下(或任何其他目录)

    cd /home
    mkdir www
    cd www
    # 然后将django项目部署在www目录下
    
  • 测试 django 项目是否可以正常运行

    • cd 到 django项目目录中

    •   # 执行下面的命令
        python3 manage.py runserver 0.0.0.0:8001
      
    • 若能在浏览器中通过 ip地址:8001 来访问则通过测试

      注:若启动报错

      django.core.exceptions.ImproperlyConfigured: SQLite 3.8.3 or later is required (found 3.7.17).
      

      原因时系统自带 sqlite3 版本太低,升级即可。

      # 切换目录
      cd /home
      # 下载源码包
      wget https://www.sqlite.org/2019/sqlite-autoconf-3300000.tar.gz
      # 解压编译
      tar -zxvf sqlite-autoconf-3300000.tar.gz 
      cd sqlite-autoconf-3300000/
      ./configure --prefix=/usr/local
      make
      make install
      # 替换系统低版本 sqlite3
      mv /usr/bin/sqlite3  /usr/bin/sqlite3_old
      ln -s /usr/local/bin/sqlite3   /usr/bin/sqlite3
      echo "/usr/local/lib" > /etc/ld.so.conf.d/sqlite3.conf
      ldconfig
      sqlite3 -version
      # 若返回 3.30.0,则安装成功
      # 安装成功后重启 django 项目测试即可
      
  • 测试 uwsgi 是否安装成功

    • 编写一个测试文件

      •   vi test.py
        
      • 写入以下内容,保存退出

        # !/usr/bin/python3
        
        def application(env, start_response):
            start_response('200 OK', [('Content-Type', 'text/html')])
            return [b'UWSGI Test...']
        
    • 启动 uwsgi 服务测试

      •   uwsgi --http :8001 --wsgi-file test.py
        
      • 使用浏览器访问 ip地址:8001

      • 若出现一下界面则测试成功

      UWSGI 测试

  • 创建 uwsgi 配置文件

    vi uwsgi.ini
    

    填入以下内容,并根据项目,进行对应修改(一般情况下,仅需配置chdir,module)

    [uwsgi]
    
    # Django-related settings
    # 127.0.0.1 表示本机,后面的 8001 表示端口
    # 这里配置的端口要和后面Nginx配置文件中的socket端口相同。
    socket = 127.0.0.1:8001
    
    # the base directory (full path)
    # 设置django项目根目录
    chdir          = /root/Learning-Python3/cmdb 
    
    # Django's wsgi file
    # 设置django项目wsgi文件,格式为: django项目名 + ".wsgi"
    module          = cmdb.wsgi  
    
    # process-related settings
    # master
    master          = true
    
    # maximum number of worker processes
    # 设置进程数
    processes      = 4
    
    # ... with appropriate permissions - may be needed
    # chmod-socket    = 666
    # chown-socket = nginx:nginx
    # clear environment on exit
    vacuum          = true
    # enable-threads = true
    
    [uwsgi]
    socket = 127.0.0.1:8085
    chdir          = /root/Learning-Python3/cmdb 
    module          = cmdb.wsgi  
    
    # process-related settings
    # master
    master          = true
    
    # maximum number of worker processes
    # 设置进程数
    processes      = 4
    
    # ... with appropriate permissions - may be needed
    # chmod-socket    = 666
    # chown-socket = nginx:nginx
    # clear environment on exit
    vacuum          = true
    # enable-threads = true
    

    (若后续启动 uwsgi 服务时出现问题,则将中文注释全部删除)

  • 安装 nginx 并配置 nginx.conf 文件

    • 安装 nginx

      # 进入 /home 目录
      # 执行以下命令
      wget https://nginx.org/download/nginx-1.9.9.tar.gz
      # 若下载速度过慢,也可以使用华为提供的 nginx 镜像
      # wget https://mirrors.huaweicloud.com/nginx/nginx-1.9.9.tar.gz
      # 下载完成后,依次执行以下命令:
      tar -zxvf nginx-1.9.9.tar.gz
      cd nginx-1.9.9
      ./configure
      make
      make install
      
    • 配置 nginx.conf

      • nginx一般默认安装好的路径为/usr/local/nginx

        cd /usr/local/nginx/conf
        vi nginx.conf
        
      • 将 http 中 server 的内容替换为以下内容,并根据项目具体情况修改

        server {
        	listen       80;
        	server_name  www.your_domain.com your_domain.com  your_ip_addr; 
        	# server_name 中的值 修改为域名或ip地址
        	location / {
        		include uwsgi_params;
        		uwsgi_pass 127.0.0.1:8001;
        	}
        	location /static/ {
                alias /home/www/myproject/static/; #项目静态路径设置
            }
            location /media/ {
                alias /home/www/myproject/media/; #项目媒体路径设置
            }
        }
        

        修改后 wq 保存,执行以下命令,完成 nginx 配置检测

        cd /usr/local/nginx/sbin/
        ./nginx -t
        

        若提示下图内容,则配置文件通过测试

        nginx

        # 通过测试后,执行以下命令,启动 nginx
        ./nginx
        

6. 启动 uwsgi

  •   # 切回项目所在目录,执行以下命令
      uwsgi --ini uwsgi.ini
    

7. 若以上步骤都没有出错,即可打开浏览器访问django项目

附录(运维相关)

1. nginx 的启动、停止 与 重启

  • 启动
    systemctl start nginx
  • 停止
    systemctl stop nginx
  • 重启
    systemctl restart nginx

若提示

Failed to start nginx.service: Unit not found.

则需要,在/etc/init.d/目录下新建文件,文件名为nginx

vi /etc/init.d/nginx

插入以下代码片段

#!/bin/sh
# nginx - this script starts and stops the nginx daemin
#
# chkconfig:   - 85 15
 
# description:  Nginx is an HTTP(S) server, HTTP(S) reverse \
#               proxy and IMAP/POP3 proxy server
 
# processname: nginx
# config:      /usr/local/nginx/conf/nginx.conf
# pidfile:     /usr/local/nginx/logs/nginx.pid
 
# Source function library.
 
. /etc/rc.d/init.d/functions
 
# Source networking configuration.
 
. /etc/sysconfig/network
 
# Check that networking is up.
 
[ "$NETWORKING" = "no" ] && exit 0
 
nginx="/usr/local/nginx/sbin/nginx"
 
prog=$(basename $nginx)
 
NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"
 
lockfile=/var/lock/subsys/nginx
 
start() {
 
    [ -x $nginx ] || exit 5
 
    [ -f $NGINX_CONF_FILE ] || exit 6
 
    echo -n $"Starting $prog: "
 
    daemon $nginx -c $NGINX_CONF_FILE
 
    retval=$?
 
    echo
 
    [ $retval -eq 0 ] && touch $lockfile
 
    return $retval
 
}
 
 
stop() {
 
    echo -n $"Stopping $prog: "
 
    killproc $prog -QUIT
 
    retval=$?
 
    echo
 
    [ $retval -eq 0 ] && rm -f $lockfile
 
    return $retval
 
}
 
 
 
restart() {
 
    configtest || return $?
 
    stop
 
    start
 
}
 
 
reload() {
 
    configtest || return $?
 
    echo -n $"Reloading $prog: "
 
    killproc $nginx -HUP
 
    RETVAL=$?
 
    echo
 
}
 
force_reload() {
 
    restart
 
}
 
 
configtest() {
 
  $nginx -t -c $NGINX_CONF_FILE
 
}
 
 
 
rh_status() {
 
    status $prog
 
}
 
 
rh_status_q() {
 
    rh_status >/dev/null 2>&1
 
}
 
case "$1" in
 
    start)
 
        rh_status_q && exit 0
        $1
        ;;
 
    stop)
 
 
        rh_status_q || exit 0
        $1
        ;;
 
    restart|configtest)
        $1
        ;;
 
    reload)
        rh_status_q || exit 7
        $1
        ;;
 
 
    force-reload)
        force_reload
        ;;
    status)
        rh_status
        ;;
 
 
    condrestart|try-restart)
 
        rh_status_q || exit 0
            ;;
 
    *)
 
        echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
        exit 2
 
esac

保存退出并执行以下语句

cd /etc/init.d
 
chmod 755 /etc/init.d/nginx
 
chkconfig --add nginx
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值