腾讯云-初始化轻量服务器python web环境

需求内容:

初始话环境用于运行python Django框架网站,数据库为mysql(docker),反向代理Nginx,版本控制git

系统选择

centos7.6

删除旧版python

默认自带python3.6,安装最新python3.9.7版本
先不安装python,等更新好sqlite3版本之后再安装,这样可以省去很多麻烦。
默认的sqlite3版本较低,不支持django2.0+

  • 删除命令:
    rpm -qa | grep python3 | xargs rpm -e --allmatches --nodeps
    
    whereis python3.6
    
    python3: /usr/lib/python3.6
    
    find / -name python3
    
  • 查看当前系统sqlite3版本(python3、python2均可):
    [root@localhost test_project]# python3
    >>>import sqlite3
    >>>sqlite3.sqlite_version
    '3.7.15'
    >>>
    
    如果是3.7.x则无法在django中运行

安装sqlite3、nginx、python3.9.7、docker

  • 创建文件夹存放安装包
    > pwd
    /root/
    > mkdir download
    > cd download
    > pwd
    /root/download/
    
  • 提前准备安装包,稍后使用bash脚本自动执行
    sqlite:
    	wget https://www.sqlite.org/2021/sqlite-autoconf-3350100.tar.gz
    python3.9.7:
    	wget https://www.python.org/ftp/python/3.9.7/Python-3.9.7.tar.xz
    nginx:
    	wget https://nginx.org/download/nginx-1.16.1.tar.gz
    git:
    	wget https://mirrors.edge.kernel.org/pub/software/scm/git/git-2.33.0.tar.gz --no-check-certificate
    
  • vi init_app.sh。复制好之后删除注释内容,否则可能导致无法正常执行
    #!/bin/bash
    local_dir=$(pwd)   #此处所在位置/root/download
    #升级sqlite
    cd ${local_dir}
    tar -zxvf sqlite-autoconf-*
    yum groupinstall -y "Development Tools"
    yum install -y tcl
    cd ${local_dir}
    cd sqlite-autoconf-*
    ./configure
    make
    cp -v .libs/libsqlite3.so.0.8.6 /usr/local/lib64/
    echo "/usr/local/lib64" > /etc/ld.so.conf.d/sqlite-x68_64.conf
    ldconfig
    
    #安装python 3.9.7
    yum -y install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel libffi-devel mysql-devel python3-devel
    mkdir /usr/local/python3
    cd ${local_dir}
    tar -xvJf Python-*
    cd Python-*
    ./configure --prefix=/usr/local/python3
    make && make install
    ln -s /usr/local/python3/bin/python3 /usr/bin/python3
    ln -s /usr/local/python3/bin/pip3 /usr/bin/pip3
    pip3 install --upgrade pip
    pip3 install virtualenv
    echo PATH=$PATH:/usr/local/python3/bin >> /etc/profile  #>>追加写入 >覆盖写入
    source /etc/profile
    python3 -m pip install --upgrade setuptools
    
    # 步骤1: 创建 ".pip" 文件夹
    mkdir -p ~/.pip
    # 步骤2: 创建并写入内容到 ".pip/pip.ini" 文件
    echo "[global]" > ~/.pip/pip.ini
    echo "index-url=https://pypi.mirrors.ustc.edu.cn/simple/" >> ~/.pip/pip.ini
    
    #安装nginx
    yum install -y gcc-c++ pcre pcre-devel zlib zlib-devel
    cd ${local_dir}
    tar -zxvf nginx-*
    cd nginx-*
    ./configure
    make && make install
    ln -s /usr/local/nginx/sbin/nginx /usr/bin/
    
    #安装docker
    yum install -y yum-utils
    yum-config-manager \
    --add-repo \
    https://download.docker.com/linux/centos/docker-ce.repo
    yum install -y docker-ce docker-ce-cli containerd.io
    systemctl start docker
    
    
    #安装git
    yum install -y gcc gcc-c++ zlib-devel perl-ExtUtils-MakeMaker package curl-devel libcurl-devel
    tar -zxvf git-*
    cd git-*
    ./configure --prefix=/usr/local/git
    make
    make install
    echo "export PATH=$PATH:/usr/local/git/bin">>/etc/bashrc
    source /etc/bashrc
    

初始化mysql就手动操作吧

  • 运行docker mysql
    docker pull mysql
    docker run --name mysql -p 8806:3306 -e MYSQL_ROOT_PASSWORD=xxxx  -e MYSQL_USER=xxxxx -e MYSQL_PASSWORD=xxx -d mysql:latest
    
  • 修改镜像源
    vi /etc/docker/daemon.json
    {"registry-mirrors": ["https://<id>.mirror.aliyuncs.com" ]}
    
    # 重启docker
    systemctl daemon-reload
    systemctl restart docker
    
    
  • 创建数据库,并更新user用户权限,否则该用户无法登录(navicat);需要修改认证方式使web访问(django。mysql8.0的认证方式与django不一致)
    docker ps   #找到运行的docker id
    docker exec -it 15e617373e87 /bin/bash
    mysql -u root -p
    > create database <database name>;
    > GRANT ALL ON <database name>.* TO '<username>'@'%';
    > alter user 'your user name'@'%' identified with mysql_native_password by 'your password';
    > flush privileges;
    > exit;
    

新服务器初始化后不建议的设置

git config --global credential.helper store

posted @ 2021-09-03 10:57  lisicn  阅读(230)  评论(0编辑  收藏  举报