apache使用fastcgi配置python和php

该博客介绍了如何在CentOS系统中安装PHP和Apache,并通过FastCGI进行配置。首先,使用yum安装PHP和必要的开发工具,然后检查Apache模块。接着,关闭SELinux和防火墙以允许外部访问。安装flup和mod_fastcgi以支持Python。最后,讲解了如何配置Apache以使用FastCGI运行Python应用,以及如何用gunicorn作为Python应用的反向代理,给出了不同工作模式的gunicorn启动命令。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  1. centos安装php: yum install php php-devel
  2. centos自带apache2,查看apache版本rpm -qa | grep httpd,
    安装apache工具(apxs等)yum install httpd-devel
    httpd -l httpd -M 列出apache安装的扩展模块
  3. 如果要外网访问,需要关闭防火墙:
 /etc/init.d/iptables stop 
# 或者是
centos7:
systemctl stop firewalld.service # 停止
systemctl disable firewalld.service  # 禁用
之前的版本:
service iptables stop  # 停止
chkconfig iptables off  # 禁用

关闭selinux. 查看selinux状态使用/usr/sbin/sestatus -v
如果SELinuxstatus参数为enable则为开启状态
暂时关闭(不用重启)可以用 setenforce 0
4. python安装flup,apache安装mod_fastcgi
5. apache配置:

ServerName localhost:80 # 第275行左右,要修改

# 最后面加上这些
LoadModule rewrite_module modules/mod_rewrite.so
LoadModule fastcgi_module modules/mod_fastcgi.so
FastCGIExternalServer /home/qiu/workspace/p1/p1.fcgi -host 127.0.0.1:8080
NAMEVirtualHost 192.168.43.128:80

<VirtualHost 192.168.43.128:80>
    DocumentRoot /home/qiu/workspace/p1
    ServerAdmin xiaoqiu206@163.com
    ServerName python.test.com
    Alias /static /home/qiu/workspace/p1/static
    RewriteEngine On
    ErrorLog "/home/qiu/workspace/p1/log"
    RewriteRule ^/(static.*)$ /$1 [QSA,L,PT]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^/(.*)$ /p1.fcgi/$1 [L]
</VirtualHost>


<VirtualHost 192.168.43.128:80>   ##### php项目的配置
    ServerAdmin example1@126.com                         
    DocumentRoot "/home/qiu/Documents/php"                    
    ServerName php.test.com                          
    ErrorLog "logs/ce.err"
    CustomLog "logs/ce.access" common

    <Directory "/your/path/php">
        Options FollowSymLinks
        AllowOverride All
        Order deny,allow
        Allow from all
    </Directory>
</VirtualHost>

注意点:文件必须可读

python启动方式: python manage.py runfcgi protocol=fcgi method=threaded host=127.0.0.1 port=8080 deamonize=false

  1. 使用apache和gunicorn.
    使用apache的反向代理
    apache配置:
<VirtualHost 192.168.43.128:80>  # python gunicorn
    ServerAdmin xiaoqiu206@163.com
    ServerName gunicorn.test.com
    ProxyRequests Off
    <Proxy *>
        Order deny,allow
        Allow from all
    </Proxy>
    ProxyPass /static/ !
    ProxyPass / http://127.0.0.1:8888/
    ProxyPassReverse / http://127.0.0.1:8888/
    # 有的教程加上了这2行
    # ProxyPreserveHost On
    # ProxyErrorOverride Off 
    Alias /static/  /usr/local/python/lib/python2.7/site-packages/django/contrib/admin/static/ 
    <Directory "/static/">
        Order allow,deny
        Options Indexes
        Allow from all
        IndexOptions FancyIndexing
    </Directory>
</VirtualHost>

gunicorn启动方式:
进入到工程目录下,gunicorn p1.wsgi:application -b 127.0.0.1:8888
或者 gunicorn -k egg:meinheld#gunicorn_worker p1.wsgi:application -b 127.0.0.1:8888
或者 gunicorn -k gevent p1.wsgi:application -b 127.0.0.1:8888

全局的NAMEVirtualHost 192.168.43.128:80可以改写为
NAMEVirtualHost *:80
然后VirtualHost里的IP:PORT也都可以改成
VirtualHost *:80
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值