2022年3月25日
本文采用腾讯云linux云服务器,安装Centos7.6操作系统
相关版本:Apache2.4.53 | Python3.7.5 | sqlite3.38 | Django3.2.12 | mod_wsgi4.9
相关操作均在shell命令行下root身份进行
Django项目路径:/root/Desktop/BoLiXueSheProject
1.升级sqlite3(Django2.4不支持低版本sqlite3)
#一定要在安装python3前升级,然后再编译安装python3时制定sqlite3路径,不然python3用的还是旧版本cd #切换至home目录 wget https://www.sqlite.org/snapshot/sqlite-snapshot-202203052020.tar.gz tar zxvf sqlite-snapshot-202203052020.tar.gz cd sqlite-snapshot-202203052020 ./configure --prefix=/usr/local/sqlite3 #建议安装至/usr/local/ 此目录安装Python需用到 make && make install cp sqlite3 /usr/bin/sqlite3 sqlite3 -version #查看版本
2.安装python依赖
yum -y groupinstall "Development tools"
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
yum install libffi-devel -y
3.安装python3.x
#高版本容易与Django等不兼容,建议使用python3.7以下
#注意centos 自带Python2 环境变量名python
#安装Python3应以python3命名
#安装路径:建议usr/local/python3cd wget https://www.python.org/ftp/python/3.7.5/Python-3.7.5.tgz tar -zxvf Python-3.7.5.tgz cd /root/Python-3.7.5/
#安装环境配置LD_RUN_PATH=/usr/local/lib ./configure LDFLAGS="-L/usr/local/lib" CPPFLAGS="-I/usr/local/include" --prefix=/usr/local/python3 --enable-shared CFLAGS=-fPIC LD_RUN_PATH=/usr/local/lib make make && make install
#建立软链接ln -s /usr/local/python3/bin/python3.7 /usr/bin/python3 ln -s /usr/local/python3/bin/pip3.7 /usr/bin/pip3 #备用 alias python3="/usr/local/python3/bin/python3.7"
#运行Django项目可能需要的python依赖yum install -y python3-devel.x86_64
#安装需要的库文件
pip3 install django pip3 install simpleui pip3 install requests pip3 install xlrd
#尝试使用Django server运行项目
#在腾讯云服务器防火墙页面检查所需端口是否开放,本步骤需检查80端口iptables -I INPUT -p tcp --dport 80 -j ACCEPT #打开防火墙的TCP80窗口 cd /root/Desktop/BoLiXueSheProject python3 manage.py runserver 0.0.0.0:80 #查看相关信息 netstat -an|grep :80 netstat -ntlp ip a
4.使用Apache作为Server
#安装apache依赖apr\apr-util\pcrecd wget https://dlcdn.apache.org//apr/apr-1.7.0.tar.gz wget https://dlcdn.apache.org//apr/apr-util-1.6.1.tar.gz wget https://boli.worksoeasy.cn/static/file/pcre2-10.39.tar.gz tar -xzvf apr-1.7.0.tar.gz && cd ./apr-1.7.0 ./configure --prefix=/usr/local/apr make && make install cd tar -xzvf apr-util-1.6.1.tar.gz && cd ../apr-util-1.6.1 ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr make && make install cd tar -xzvf pcre2-10.39.tar.gz && cd ../pcre2-10.39 ./configure make && make install
#安装Apache2.4cd wget https://dlcdn.apache.org/httpd/httpd-2.4.53.tar.gz tar -xzvf httpd-2.4.53.tar.gz && cd httpd-2.4.53 ./configure --prefix=/usr/local/apache make && make install #安装mod_wsgi, Apache不带mod_wsgi模块,用于和Django通讯 cd wget https://files.pythonhosted.org/packages/b6/54/4359de02da3581ea4a17340d87fd2c5a47adc4c8e626f9809e2697b2d33f/mod_wsgi-4.9.0.tar.gz ./configure --with-apxs=/usr/local/apache/bin/apxs --with-python=/usr/local/python3/bin/python3.7 make && make install
#修改/usr/local/apache/conf/httpd.conf
#注:
#django项目路径 /root/Desktop/BoLiXueSheProject
#manage.py路径 /root/Desktop/BoLiXueSheProject/manage.py
#wsgi.py路径 /root/Desktop/BoLiXueSheProject/BoLiXueSheProject/wsgi.pyListen 80 LoadModule wsgi_module modules/mod_wsgi.so #ServerName 服务器IP:80 #出于隐私 这里所有IP的第二位用***代替,域名部分用***代替 ServerName 175.***.28.89:80 WSGIPythonPath /root/Desktop/BoLiXueSheProject WSGIScriptAlias / /root/Desktop/BoLiXueSheProject/BoLiXueSheProject/wsgi.py Alias /static /root/Desktop/BoLiXueSheProject/static <Directory /root/Desktop/BoLiXueSheProject/static> Require all granted </Directory> <Directory /root/Desktop/BoLiXueSheProject/BoLiXueSheProject> <Files wsgi.py> Require all granted </Files> </Directory> <Directory /root/Desktop/BoLiXueSheProject> Require all granted </Directory> <VirtualHost *:80> ServerName 175.***.28.89 WSGIScriptAlias / /root/Desktop/BoLiXueSheProject/BoLiXueSheProject/wsgi.py </VirtualHost>
5.启动Apache
#开启相关文件及目录权限
#本示例需开启/root 目录权限 drwxrwxrwxcd sudo chmod ugo+rwx root
#加载配置文件启动Apache /usr/local/apache/bin/apachectl -k start -f /usr/local/apache/conf/httpd.conf && netstat -ntlp #重启 /usr/local/apache/bin/apachectl -k restart && netstat -ntlp #停止 /usr/local/apache/bin/apachectl -k stop && netstat -ntlp #开机自启 systemctl enable httpd
6.启用HTTPS安全传输
#首先需要有域名,在腾讯云或其他地方申请CA证书,申请成功后下载证书文件,解压后放至Apache配置目录
#新建证书文件夹:/usr/local/apache/conf/sslcert
#内含文件:域名.key | 域名.crt | 域名.csr | root*.crt 其中域名.csr文件是认证机构生成的额外文件,用不上
#修改Apache配置文件/usr/local/apache/conf/httpd.conf
#去除ssl模块的注释,或新增一行加载ssl_module
LoadModule ssl_module modules/mod_ssl.so
#修改Apache配置文件/usr/local/apache/conf/extra/httpd-ssl.conf
#域名w******y.cn
ServerName www.w******y.cn:443
SSLCertificateFile "/usr/local/apache/conf/sslcert/w******y.cn.crt"
SSLCertificateKeyFile "/usr/local/apache/conf/sslcert/w******y.cn.key"
SSLCertificateChainFile "/usr/local/apache/conf/sslcert/root_bundle.crt"
#开放443端口 腾讯云服务器防火墙检查开放443端口
iptables -I INPUT -p tcp --dport 443 -j ACCEPT
#重启Apache
/usr/local/apache/bin/apachectl -k stop && netstat -ntlp
/usr/local/apache/bin/apachectl -k start -f /usr/local/apache/conf/httpd.conf && netstat -ntlp