Django Centos apache 部署笔记

2021-09-24

安装apache

sudo yum update httpd  
sudo yum install httpd  

配置防火墙

firewall-cmd --zone=public --add-port=80/tcp  
firewall-cmd --zone=public --add-port=443/tcp  
sudo firewall-cmd --reload  

启动Web Server

sudo systemctl start httpd    
sudo systemctl status httpd  

管理httpd进程

停止

sudo systemctl stop httpd  

开启

sudo systemctl start httpd  

重启

sudo systemctl restart httpd  

重新加载配置

sudo systemctl reload httpd  

禁止和开启开机自启动

sudo systemctl disable httpd  
sudo systemctl enable httpd  

检查运行中的服务器

sudo systemctl status httpd  

配置文件

配置文件路径见 https://cwiki.apache.org/confluence/display/HTTPD/DistrosDefaultLayout

centos 配置文件目录下如下:
- ServerRoot:/etc/httpd
- Primary Config Fle:/etc/httpd/conf/httpd.conf
- Other Config Files:/etc/httpd/conf.d
- Module Locations:/usr/lib/httpd/modules
- DocumentRoot:/var/www/html
- ErrorLog:/var/log/httpd/error_log
- AccessLog:/var/log/httpd/access_log
- cgi-bin:/var/www/cgi-bin (empty and disabled by default)
- binary:/usr/sbin/httpd
- runtime directory:/etc/httpd/run
- start/stop:/sbin/service httpd {start|stop|restart|condrestart|reload|status|fullstatus|graceful|help|configtest}

参考:https://www.digitalocean.com/community/tutorials/how-to-install-the-apache-web-server-on-centos-7

安装python3.9

  • 使用wget下载最新源码
  • 解压 tar -zxvf Python-3.9.7.tgz
  • 配置 ./configure prefix=/usr/local/python3 --enable-shared
  • 安装 make && make install
  • 添加软件连接 :
    ln -s /usr/local/python3/bin/python3.9 /usr/bin/python3
    ln -s /usr/local/python3/bin/pip3.9 /usr/bin/pip3
  • 创建vi /etc/ld.so.conf.d/python3.conf 内容为:/usr/local/python3/lib

安装mod_wsgi

参考:https://modwsgi.readthedocs.io/en/master/user-guides/quick-installation-guide.html

下载源码后编译
- ./configure --with-python=/usr/local/python3/bin/python3.9
- make
- make install
安装完之后提示
Libraries have been installed in:
/usr/lib64/httpd/modules

配置apache

收集项目静态文件:python manage.py collectstatic
增加httpd.conf配置

WSGIScriptAlias / /var/www/myblog/zdsweb/wsgi.py
WSGIPythonHome /var/www/myblog/env
WSGIPythonPath /var/www/myblog
<VirtualHost *:80>
        ServerAdmin info@zds.com
        ServerName 161.22.55.146
        DocumentRoot /var/www/

        Alias /static /var/www/myblog/static
        <Directory "/var/www/myblog/static">
                Options FollowSymLinks
                Order allow,deny
                Allow from all
                Require all granted
        </Directory>

        Alias /media /var/www/myblog/media
        <Directory "/var/www/myblog/media">
                Options FollowSymLinks
                Order allow,deny
                Allow from all
                Require all granted
        </Directory>
        ErrorLog /var/www/myblog/logs/apis_error.log
        CustomLog /var/www/myblog/logs/apis_access.log combined

        WSGIPassAuthorization On
        WSGIDaemonProcess zdsweb python-path=/var/www/myblog:/var/www/myblog/env/lib/python3.9/site-packages
        WSGIProcessGroup zdsweb
        WSGIScriptAlias / /var/www/myblog/zdsweb/wsgi.py


        <Directory /var/www/myblog/zdsweb>
                <Files wsgi.py>
                        Require all granted
                </Files>
        </Directory>
</VirtualHost>

启用ssl

yum install mod_ssl

然后使用 httpd -M 查可以加载的模块时这里已经发现mod_ssl已经在其中了。
编辑/etc/httpd/conf.d/ssl.conf
里面需要配置的项有: * ServerName * ServerAdmin * SSLCertificateFile * SSLCertificateKeyFile * SSLCertificateChainFile * 配置静态目录static目录的访问同django.conf里的static配置

一些坑

UnicodeEncodeError ‘ascii’ codec casys.getfilesystemencoding()n’t encode characters in position ordinal not in range(128)

在读markdown文件的时候报这个错,这个问题足足搞了两三个小时,网上的办法基本全部没用。最后发现用python3 manage.py runserver没问题。这就可能是appach和wsgi问题了,在setting.py里打印sys.getfilesystemencoding()的时候,竟然显示是ascii,而在runserver下是utf8这就是关键问题所在。

解决方法:在配置wsig的时候WSGIDaemonProcess后面增加locale参数:

WSGIDaemonProcess zdsweb lang='en_US.UTF-8' locale='en_US.UTF-8' python-path=/var/www/myblog:/var/www/myblog/env/lib/python3.9/site-packages

参考:https://itekblog.com/ascii-codec-cant-encode-characters-in-position/

Markdown文件无法用python读文件函数访问

解决:必须要放到www目录下才可以

一些问题的解决

网页错误提示:attempt to write a readonly database

解决方案:
chmod 664 db.sqlite3
chown apache:apache db.sqlite3

发表评论