Centos+Apache+mod_wsgi+Django 部署运行网站

2023-07-0212:08:39服务器及运维Comments907 views字数 9022阅读模式

CentOS 以稳定著称,每个版本通常提供 10 年的 LTS,为使用最多的服务器操作系统,本页面将搭建一个 Centos + Apache + mod_wsgi + Django 环境,用于运行网站及服务。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/49253.html

服务基本信息

登录服务器,查看基本的信息:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/49253.html

# root 为默认账号, 22 为端口号
ssh -p 22 root@111.111.111.111
# 输入密码可登录
# 安装常用组件
yum install gcc libffi-devel python-devel openssl-devel -y
# 常用命令
cat /etc/redhat-release # 查看 CentOS 版本
free -m # 内存使用情况
top # 实时,查看进程运行、CPU、内存等情况
ifconfig # 网络情况
service --status-all # 所有服务运行状态
reboot # !!! 重启

磁盘管理

# 查看磁盘以及分区、大小、使用情况
df -h # 可见挂载云硬盘,如无需要挂载
fdisk -l # 可见云硬盘
# 创建目录
mkdir /home/data

# 挂载云盘
# 初始化、分区、格式化等操作见主机商的帮助文档
fdisk -l #  查看要挂载的硬盘
fdisk /dev/sdb1 # 硬盘分区
# 进入命令行
# 第一步 输入 n,不需要帮
# 第二步 输入 p,作为主分区
# 第三步 输入 1,至做一个逻辑分区
# 然后一直下一步,出现 command,输入w,保存
mkfs.ext4 /dev/sdb1 #ext4 格式化

# 挂载
mount /dev/sdb1 /home/data
# 设置开机自动挂载,查看新挂载的磁盘id
blkid
# 找到要挂载盘的 UUID,如:
/dev/sdb1: UUID="d05f1e6f-0ed2-4b51-af3c-71fbc2b2b77d" TYPE="ext4"
# 编辑 fstab,在末尾增加一行
vi /etc/fstab
UUID=d05f1e6f-0ed2-4b51-af3c-71fbc2b2b77d /home/data ext4 defaults 0 0
# 重启并测试

修改 SSH 默认端口

注意,一定要先进行修改,并验证是否成功。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/49253.html

# 查看当前 SSH 端口
semanage port -l|grep ssh
# 修改 SSH 配置文件
vim /etc/ssh/sshd_config
# 先同时开 22 和指定的端口,验证后再注释关掉 22 ,防止无法登录!!!
Port 22
Port 41234
# 重启 ssh 服务,使生效
service sshd restart
# 退出,验证、注释 22 重启,再登录验证

vim 的使用

在配置服务器中要经常使用 vim 修改配置文件,使用方法为:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/49253.html

# 打开文件
vim /etc/ssh/sshd_config
# 按 i 进入编辑状态
# 上下左右键移动光标定位修改位置
# 修改后按 ESC,输入 : ,然后 q: 退出不保存,wq: 保存退出

时间的同步

过一段时间后一般服务器的时间会不准确,需要与标准时间同步一致:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/49253.html

# 同步时间
ntpdate us.pool.ntp.org
# 查看当前时间是否正确,CST 为中国标准时间
date
# 可在 crontab -e 中配置每周自动同步
# At 00:05 on Monday, sync time
5 0 * * 1 ntpdate us.pool.ntp.org

Apache

安装:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/49253.html

yum install httpd
yum install httpd-devel
# 如果 yum 报错可试:
sudo yum clean all
sudo yum --disablerepo="epel" update nss
# 安装 mod_ssl 用于 https 访问
sudo yum -y install mod_ssl
# apache 的路径为:
/etc/httpd
# 创建一个目录存放网站配置
/home/apache/
# 在 /etc/httpd/conf/httpd.conf 尾部增加引用这些配置
Include /home/apache/*.conf
# 后期各网站的配置文件放在 /home/apache/ 目录下
# 重启生效
# 如出现以下错误:
Starting httpd: httpd: apr_sockaddr_info_get() failed for GairuoHost
# 查看主机名,如 GairuoHost
hostname
# 增加主机名
vi /etc/hosts
127.0.0.1 localhost.localdomain localhost GairuoHost

操作:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/49253.html

# 查看 apache 版本
httpd -v
# 设置为开机自动启动
/sbin/chkconfig httpd on
# 重启
service httpd restart
# 关闭默认错误页
/var/www/error.off.bygr
# 备份复制 /etc/httpd/conf/httpd.conf 为 httpd.conf.bak
# 修改原 httpd.conf 文件只有以下一条,后期在 /home/apache/ 中进行维护
Include /home/apache/*.conf
# 将原 httpd.conf 复制在 /home/apache/
# 并创建相应网站的配置文件,可一个网站创建一个文件,如
httpd-gairuo.conf
httpds-gairuo.conf

Python 环境

Conda 多 Python 版本环境:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/49253.html

# https://mirrors.tuna.tsinghua.edu.cn/anaconda/miniconda/
# https://docs.conda.io/en/latest/miniconda.html
# 下载文件上传到服务器,安装
bash Miniconda3-latest-Linux-x86_64.sh
# 一路按提示,同意,填写安装目录
/home/miniconda3
# 如无 conda 命令,则初始化
eval "$(/home/miniconda3/bin/conda shell.bash hook)"
conda init
# 写入环境变量
export PATH="/home/miniconda3/bin:$PATH"
# 创建新环境,<环境名称>, python 版本
conda create -n py39 python=3.9
# 删除环境
conda remove -n py39 --all
# 进入、激活环境
conda activate py39
# 退出环境
conda deactivate
# 查看所有虚拟环境及当前环境
conda info -e
# python 环境的路径为:
/home/miniconda3/envs/py39/bin/python

安装库包:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/49253.html

# 国内网络慢,可指定源下载安装
pip install django -i https://pypi.tuna.tsinghua.edu.cn/simple
# 注:psycopg2-binary 后需要安装 postgresql 才可使用
pip install psycopg2-binary -i https://pypi.tuna.tsinghua.edu.cn/simple

# 其他可能用到的库包
Django             4.0.3
django-ckeditor    6.2.0
django-mdeditor    0.1.20
Markdown           3.3.6
mod-wsgi           4.7.1
Pillow             9.0.1
psycopg2-binary    2.9.3
Pygments           2.11.2
pymemcache         3.5.1
WeRoBot            1.13.1
wheel              0.37.1

更多依赖可参考:Django 开发速查手册文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/49253.html

mod_wsgi 环境

安装库包:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/49253.html

# 进入、激活环境
conda activate py39
# 国内网络慢,可指定源下载安装
pip install mod-wsgi==4.7.1 -i https://pypi.tuna.tsinghua.edu.cn/simple
# 安装找不到的包
conda install -c conda-forge mod-wsgi==4.7.1
# 导出 apache 所需的 mod_wsgi 模块(必须在指定环境下操作)
mod_wsgi-express install-module
# 会输出 so 文件路径为(配置 apache 时备用):
# 如果指定路径没有,可直接到包所在 Python 三方库 site-package 下相应目录查找,文件复制到目录
# 或者直接使用在 Python 包中的文件路径
LoadModule wsgi_module "/usr/lib64/httpd/modules/mod_wsgi-py39.cpython-39-x86_64-linux-gnu.so"
WSGIPythonHome "/home/miniconda3/envs/py39"

LoadModule wsgi_module 可能的路径是:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/49253.html

home/miniconda3/envs/py39/lib/python3.9/site-packages/mod_wsgi/server/mod_wsgi-py39.cpython-39-x86_64-linux-gnu.so文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/49253.html

数据库 Postgresql

安装:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/49253.html

# 更新源
yum install https://download.postgresql.org/pub/repos/yum/10/redhat/rhel-7-x86_64/pgdg-centos10-10-2.noarch.rpm -y
# 查看相应源
yum list | grep postgresql
# 安装服务端
yum install postgresql11-contrib postgresql11-server -y
# 卸载
yum remove postgresql*
# 安装 postgresql Python 支持包 , 注意进入指定虚拟环境(已安装可忽略)
pip install psycopg2-binary -i https://pypi.tuna.tsinghua.edu.cn/simple

配置:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/49253.html

# 查看版本
psql --version
# 查看环境路径,安装在 /usr/pgsql-11
which psql
# 初始化数据库
service postgresql-11 initdb
# 重启 postgresql-11
service postgresql-11 restart
# 开机自动启动
/sbin/chkconfig postgresql-11 on
# 进行数据服务 bash-4.1$
su postgres
# 进入数据操作 postgres=#
psql
# \h 帮助,\l 列出所有数据表,\c tabl_name 进入指定库
# \du 查看所有用户,exit 退出
# 修改管理员密码
ALTER USER postgres WITH PASSWORD '密码字符串';
# 修改认证方式为md5
vim /var/lib/pgsql/11/data/pg_hba.conf
# 修改
# IPv4 local connections:
host    all             all             127.0.0.1/32            md5
# IPv6 local connections:
host    all             all             ::1/128                 md5
# 添加
host    all             all             all                     md5
# 修改允许的 IP
vim /var/lib/pgsql/11/data/postgresql.conf
# 修改默认端口
port = 8898 # 原5432
# 允许外部 IP 访问
listen_addresses = '*'
# 创建目录,用于存放数据
/home/pgsql_data
# 一个库可以放一个文件夹,如:
/home/pgsql_data/gairuo
# 利用 pgAdmin 创建单独的网站访问用户名
# group roles 中添加 gairuo_dbuser 访问 gairuo.com 的数据库
# Tablespaces 中添加 garuo 空间,Owner 选择 gairuo_dbuser,
# Location 填写 /home/pgsql_data/gairuo
# 如提示无权限则:
chown postgres:postgres /home/pgsql_data/gairuo

注:可开一个新的超管账号,然后关掉 postgres 超管权限,防止暴力破解密码。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/49253.html

Memcached

用于缓存管理:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/49253.html

# 安装 python 库(已安装可忽略)
pip install pymemcache
# 安装 memcached 服务
yum  -y install memcached
# 验证安装memcached
memcached -h
# 配置Memcached
vi /etc/sysconfig/memcached
# 文件中内容如下(可根据需求配置):
PORT="11211" //端口
USER="memcached" //使用的用户名
MAXCONN="1024" //同时最大连接数
CACHESIZE="64" //使用的内存大小 单位为MB
OPTIONS="" //附加参数
# 启动 Memcached 服务
chkconfig --level 2345 memcached on # 开机启动
service memcached restart # 重新启动
# 检测 memcached 服务
memcached-tool 127.0.0.1:11211 stats

Apache 配置

在 /home/apache/ 下添加 httpd-vhosts.conf 文件,也可一个网站创建一个,文件内容:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/49253.html

静态网站:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/49253.html

NameVirtualHost *:80
Listen 80
# 增加站点
<VirtualHost *:80>
    ServerName www.gairuo.com
    Header append Vary User-Agent
    DocumentRoot /home/www/gairuo/www/
    # 可开启 Rewrite
    RewriteEngine on
    RewriteRule ^(.*)?$ https://www.gairuo.com$1 [R=permanent,L]
</VirtualHost>

Django 站点配置,LoadModule 路径见上文说明:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/49253.html

LoadModule wsgi_module "/usr/lib64/httpd/modules/mod_wsgi-py39.cpython-39m-x86_64-linux-gnu.so"
WSGIPythonHome "/home/miniconda3/envs/py39"

LoadModule ssl_module modules/mod_ssl.so
Listen 443
NameVirtualHost *:443

WSGIApplicationGroup %{GLOBAL}
WSGIPythonHome "/home/miniconda3/envs/py39"
WSGIPythonPath
WSGIPythonPath /home/www/gairuo/www:/home/miniconda3/envs/py37/lib/python3.7/site-packages/
WSGISocketPrefix /var/run/wsgi

# gairuo.https
<VirtualHost *:443>
#    ServerAdmin no-send-9@gairuo.com
    ServerName www.gairuo.com
    Header append Vary User-Agent
    DocumentRoot /home/www/gairuo/www/

    # Https 证书
    SSLEngine on
    SSLProtocol all -SSLv2
    SSLCipherSuite HIGH:MEDIUM:!aNULL:!MD5
    SSLCertificateFile /home/apache/cert/www.gairuo.com/2_www.gairuo.com.crt
    SSLCertificateKeyFile /home/apache/cert/www.gairuo.com/3_www.gairuo.com.key
    SSLCertificateChainFile /home/apache/cert/www.gairuo.com/1_root_bundle.crt

    Alias /favicon.ico /home/www/gairuo/www/static/favicon.ico
    Alias /robots.txt /home/www/gairuo/www/static/robots.txt
    Alias /static/admin /home/miniconda3/envs/py37/lib/python3.7/site-packages/django/contrib/admin/static/admin/
    Alias /static/ /home/www/gairuo/www/static/
    Alias /file/ /home/www/gairuo/www/file/

    <Directory /home/www/gairuo/www/static>
        Order allow,deny
        Allow from all
    </Directory>

    WSGIScriptReloading On
    WSGIDaemonProcess www.gairuo.com python-path=/home/www/gairuo/www:/home/miniconda3/envs/py39/lib/python3.9/site-packages/
    WSGIProcessGroup www.gairuo.com
    WSGIScriptAlias / /home/www/gairuo/www/gr/wsgi.py process-group=www.gairuo.com
</VirtualHost>

禁止直接通过服务器的 IP 地址直接访问配置:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/49253.html

# ServerName 为服务器的 IP 地址
<VirtualHost *:80>
    ServerName 1.2.3.4
    <Location />
        Order Allow,Deny
        Deny from all
    </Location>
</VirtualHost>

Django 配置

如果网站出现 400 错误, 需要在网站配置 settings.py 中加允许访问,* 为任意域名 IP,测试用:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/49253.html

ALLOWED_HOSTS = [
    'www.gairuo.com',  # Allow domain and subdomains
    '*', # Also allow FQDN and subdomains
]
# 如 500 或者以下错误:
RuntimeError: populate() isn't reentrant
# 可检查相关目录是否有写入权限
# 检查修改 settings.py 中的静态文件路径配置
MEDIA_ROOT = "/home/www/gairuo/file/"
MEDIA_URL = "/static/file/"

压缩备份

  • linux 安装 zip 命令:apt-get install zip 或 yum install zip
  • 压缩:
    • tar -czvf abc.tar.gz /home/www/abc (文件会存在当前目录下)
    • tar -czvf gairuo.tar.gz gairuo
  • 解压:
    • tar -xzvf gairuo.tar.gz -C gairuocom (解压缩到 gairuocom 目录,可以指定文件夹)

相关问题

以下是一些常见的问题及解决方案,供参考,一定要结合实际场景进行处理,不要轻信以下解决方案,操作前先备份,不然会造成损失(我不负责 -_- )。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/49253.html

CondaHTTPError

如果提示以下解决:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/49253.html

CondaHTTPError: HTTP 000 CONNECTION FAILED for url https://repo.anaconda.com/pkgs/main/linux-64/current_repodata.json文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/49253.html

添加清华源:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/49253.html

conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
conda config --set show_channel_urls yes

vim ~/.condarc 进入修改:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/49253.html

channels:
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge/
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/msys2/
show_channel_urls: true

解决方案:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/49253.html

  1. 删除 channels 配置文件 的 - defaults 这一行
  2. https 替换为 http 问题解决(优先尝试)
  3. 同时操作以上

GLIBC_2.17 not found

服务器上安装 conda 时,可能提示这个错误 /lib64/libc.so.6: version GLIBC_2.17 not found,是因为 glibc 版本太低,glibc 是 GNU 发布的 libc 库,即 c 运行库。glibc 是 linux 系统中最底层的 api,几乎其它任何运行库都会依赖于 glibc。解决办法以下:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/49253.html

# 查看当前库
strings /lib64/libc.so.6 |grep GLIBC_
# 下载高版本的 glibc 库
wget https://ftp.gnu.org/gnu/glibc/glibc-2.17.tar.gz
# 或者前往 https://ftp.gnu.org/gnu/glibc/ 下载上传到服务器

# 解压
tar -xvf glibc-2.17.tar.gz
# 开始编译安装
# 进入glibc-2.17目录中
cd glibc-2.17
# 创建build目录
mkdir build
# 进入build目录中
cd build
# 执行./configure
../configure --prefix=/usr --disable-profile --enable-add-ons --with-headers=/usr/include --with-binutils=/usr/bin
# 安装,时间稍长,可能有 5 分钟左右
make && make install
# 查看共享库
ls -l /lib64/libc.so.6
# 可以看到已经建立了软链接
lrwxrwxrwx. 1 root root 12 Jan 13 01:49 /lib64/libc.so.6 -> libc-2.17.so
# 再次查看
strings /lib64/libc.so.6 |grep GLIBC_
文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/49253.html
  • 本站内容整理自互联网,仅提供信息存储空间服务,以方便学习之用。如对文章、图片、字体等版权有疑问,请在下方留言,管理员看到后,将第一时间进行处理。
  • 转载请务必保留本文链接:https://www.cainiaoxueyuan.com/yunwei/49253.html

Comment

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定