ICode9

精准搜索请尝试: 精确搜索
首页 > 系统相关> 文章详细

CentOS7+python3+django+nginx+uwsgi部署

2019-08-14 15:04:51  阅读:82  来源: 互联网

标签:bin local django CentOS7 nginx yum usr soft python3



现在想要在一台centos7上部署django来学习django框架,发现django的资料,很多都是python3的,而且python2也逐步向python3过渡中,在搜索了很多资料之后,总算成功的搭建出了环境,记录一下,方便以后搜索查阅。

 

大环境是新安装的CentOS 7 1810 mini版

搭建主要就是以下几个步骤

1、编译安装python3

2、安装django和uwsgi

3、安装nginx

4、调整细节

 

初始预安装环境

在部署之前,我们先要预安装一些软件,建议一开始先把yum的源替换成国内源,这样下载的会比较快一点

yum -y install vim rsync lrzsz openssl-devel bzip2-devel expat-devel gdbm-devel readline-devel sqlite-devel gcc-c++ libxml* mlocate dos2unix

updatedb

 

编译安装python3

由于网络的原因,下载python3的安装包可能会比较慢,大家可以用迅雷等软件下载好,再上传回服务器,这样效率会高一点

wget https://www.python.org/ftp/python/3.6.3/Python-3.6.3.tgz
tar -zxvf Python-3.6.3.tgz
cd Python-3.6.3
./configure --prefix=/usr/local/python3
make -j2
make install -j2


ln -s /usr/local/python3/bin/python3.6 /usr/bin/python3
ln -s /usr/local/python3/bin/pip3 /usr/bin/pip3

安装django和uwsgi

pip3 install django uwsgi
ln -s /usr/local/python3/bin/uwsgi /usr/bin/uwsgi3

安装nginx

wget http://nginx.org/download/nginx-1.13.7.tar.gz
tar -zxvf nginx-1.13.7.tar.gz
cd nginx-1.13.7
./configure
make
make install

调整细节

创建django-admin软链

ln -s /usr/local/python3/bin/django-admin /usr/local/bin

 

安装升级sqlite3

wget https://www.sqlite.org/2019/sqlite-autoconf-3270200.tar.gz
tar -zxvf sqlite-autoconf-3270200.tar.gz
cd sqlite-autoconf-3270200
./configure --prefix=/usr/local
make && make install

mv /usr/bin/sqlite3  /usr/bin/sqlite3_old
ln -s /usr/local/bin/sqlite3   /usr/bin/sqlite3
echo "export LD_LIBRARY_PATH=\"/usr/local/lib\"" >> ~/.bashrc
source ~/.bashrc

 

至此,整个部署就完成了,整个过程中,就是那几个wget的安装包下载的很慢,其他的整体上没什么问题

 

对于全新的初始环境,我还提供了一键式的shell脚本,直接部署,为避免wget下载慢的问题,大家可以先下载安装包,然后在/root目录下,创建soft,把安装包都丢进soft里面,sh执行脚本即可

#!/bin/bash

echo -e "\n开始配置环境\n"
echo -e "开始替换yum国内源\n"
if [[ $(rpm -qa | grep wget) ]]; then
    echo -e "wget已安装,进行下一步\n"
else
    yum -y install wget
fi

if [ ! -d "/root/soft" ]; then
    echo -e "soft目录不存在,创建目录/root/soft \n"
    mkdir /root/soft
else
    echo -e "\n/root/soft目录存在,执行下一步\n"
fi

# 比较yum源是否有替换过
yum1=$(md5sum /etc/yum.repos.d/CentOS-Base.repo | awk '{print $1}')
if [[ ! -f /root/soft/Centos-7.repo ]]; then
    wget -P /root/soft http://mirrors.aliyun.com/repo/Centos-7.repo
fi
echo -e "yum国内源已下载,匹配检查中......\n"
yum2=$(md5sum /root/soft/Centos-7.repo | awk '{print $1}')
if [[ $yum1==$yum2 ]]; then
    echo -e "主机yum源已是国内源,无需替换\n"
else
    echo -e "正在替换yum国内源\n"
    cp /root/soft/Centos-7.repo /etc/yum.repos.d/
    mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.bak
    mv /etc/yum.repos.d/Centos-7.repo /etc/yum.repos.d/CentOS-Base.repo
    yum clean all
    yum makecache
    yum -y update
    echo -e "\nyum国内源替换完成\n"
fi

echo -e "开始部署django\n"
# 安装环境
yum -y install vim rsync lrzsz openssl-devel bzip2-devel expat-devel gdbm-devel readline-devel sqlite-devel gcc-c++ libxml* mlocate dos2unix

updatedb

# 安装python3
if [[ ! -f /root/soft/Python-3.6.3.tgz ]]; then
    wget -P /root/soft https://www.python.org/ftp/python/3.6.3/Python-3.6.3.tgz
fi
cd /root/soft
tar -zxvf Python-3.6.3.tgz
cd Python-3.6.3
./configure --prefix=/usr/local/python3
make -j2
make install -j2

# 创建软链
ln -s /usr/local/python3/bin/python3.6 /usr/bin/python3
ln -s /usr/local/python3/bin/pip3 /usr/bin/pip3

# 替换pip的国内源
cd ~
mkdir .pip
cd .pip
cat>pip.conf<<EOF
[global]
index-url = https://pypi.tuna.tsinghua.edu.cn/simple
EOF

cd ~

# 安装django和uwsgi
pip3 install django uwsgi

# 创建软链
ln -s /usr/local/python3/bin/uwsgi /usr/bin/uwsgi3

# 安装nginx
cd ~/soft
if [[ ! -f /root/soft/nginx-1.13.7.tar.gz ]]; then
    wget http://nginx.org/download/nginx-1.13.7.tar.gz
fi

tar -zxvf nginx-1.13.7.tar.gz
cd nginx-1.13.7
./configure
make
make install

# 创建django-admin软链
ln -s /usr/local/python3/bin/django-admin /usr/local/bin

# 安装升级sqlite3
cd ~/soft
if [[ ! -f /root/soft/sqlite-autoconf-3270200.tar.gz ]]; then
    wget https://www.sqlite.org/2019/sqlite-autoconf-3270200.tar.gz
fi

tar -zxvf sqlite-autoconf-3270200.tar.gz
cd sqlite-autoconf-3270200
./configure --prefix=/usr/local
make && make install

mv /usr/bin/sqlite3  /usr/bin/sqlite3_old
ln -s /usr/local/bin/sqlite3   /usr/bin/sqlite3
echo "export LD_LIBRARY_PATH=\"/usr/local/lib\"" >> ~/.bashrc
source ~/.bashrc

echo "部署完成"

 



标签:bin,local,django,CentOS7,nginx,yum,usr,soft,python3

专注分享技术,共同学习,共同进步。侵权联系[admin#icode9.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有