linux centos 安装apache,centos7安装配置apache(httpd)

本文详细介绍了如何使用yum安装Apache及PHP模块,并配置Apache服务器来支持目录索引、PHP解析、跨域请求、虚拟主机等功能。此外,还讲解了如何设置白名单、启用gzip压缩、修改上传文件大小限制等高级配置。

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

安装:

yum -y install httpd mod_php

# 默认目录是:/var/www/html

支持目录索引:

a. 首页索引:

vim /etc/httpd/conf.d/welcome.conf

abfb9898ce1136911dab5aa5184f8983.png

b. 自定义索引样式:

vim /etc/httpd/conf/httpd.conf

46e7196fa1507a7b86aef6108b8d25e2.png

支持php:

a. 安装mod_php:

yum -y install mod_php

b. 修改配置文件:

0c8b6ee3e61bf03fa8db0750e53d52c7.png

开启pathinfo:

755056db7fc7cc34fc0a709fecba33f6.png

解决跨域:

e570026609dd82596e8d37917734f856.png

开启rewrite功能:

3f8866f36111bbe4b041ad82e750ae04.png

配置虚拟主机:

2d728299059fad1ea836f0c477e3565e.png

防盗链:

38687f7170d3e444148dc85e4777930a.png

配置rewrite:

vim /var/www/html/.htaccess

Options +FollowSymlinks

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-d

RewriteCond %{REQUEST_FILENAME} !-f

RewriteRule ^(.*)$ index.php/$1 [QSA,PT,L]

设置索引文件:

DirectoryIndex index.html index.php

设置服务器名:

ServerName dollarphp

使支持react(写.htaccess文件):

RewriteEngine On

RewriteBase /

RewriteRule ^index.html$ - [L]

RewriteCond %{REQUEST_FILENAME} !-f

RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule . /index.html [L]

开启gzip扩展:

SetOutputFilter DEFLATE

SetEnvIfNoCase Request_URI .(?:gif|jpe?g|png)$ no-gzip dont-vary

SetEnvIfNoCase Request_URI .(?:exe|t?gz|zip|bz2|sit|rar)$ no-gzip dont-vary

SetEnvIfNoCase Request_URI .(?:pdf|doc|avi|mov|mp3|rm)$ no-gzip dont-vary

AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css

AddOutputFilterByType DEFLATE application/x-javascript

白名单设置:

Order Deny,Allow

Allow from 192.168.8.1/24

Deny from All

修改上传文件大小限制:

LimitRequestBody 10485760

配置文件参考:

ServerRoot "/etc/httpd"

Listen 80

Include conf.modules.d/*.conf

User apache

Group apache

ServerAdmin root@localhost

ServerName dollarphp

AllowOverride none

Require all denied

Order Deny,Allow

Allow from 192.168.8.1/24

Deny from All

LimitRequestBody 10485760

DocumentRoot "/var/www/html"

AllowOverride None

Require all granted

Header set Access-Control-Allow-Origin *

Options Indexes FollowSymLinks

IndexStyleSheet "/css/style.css"

IndexOptions FancyIndexing HTMLTable ScanHTMLTitles FoldersFirst NameWidth=85 DescriptionWidth=128 IconWidth=16 IconHeight=16 VersionSort Charset=UTF-8

AllowOverride all

Require all granted

AcceptPathInfo on

DirectoryIndex index.php index.html

Require all denied

ErrorLog "logs/error_log"

LogLevel warn

LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined

LogFormat "%h %l %u %t \"%r\" %>s %b" common

LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %I %O" combinedio

CustomLog "logs/access_log" combined

ScriptAlias /cgi-bin/ "/var/www/cgi-bin/"

AllowOverride None

Options None

Require all granted

TypesConfig /etc/mime.types

AddType application/x-compress .Z

AddType application/x-gzip .gz .tgz

AddType text/html .shtml

AddType application/x-httpd-php .php .phtml .php3 .inc

AddOutputFilter INCLUDES .shtml

AddDefaultCharset UTF-8

MIMEMagicFile conf/magic

SetOutputFilter DEFLATE

SetEnvIfNoCase Request_URI .(?:gif|jpe?g|png)$ no-gzip dont-vary

SetEnvIfNoCase Request_URI .(?:exe|t?gz|zip|bz2|sit|rar)$ no-gzip dont-vary

SetEnvIfNoCase Request_URI .(?:pdf|doc|avi|mov|mp3|rm)$ no-gzip dont-vary

AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css

AddOutputFilterByType DEFLATE application/x-javascript

EnableSendfile on

IncludeOptional conf.d/*.conf

ServerName www.dollarphp.com

DocumentRoot "/var/www/html/dollarphp/"

DirectoryIndex index.php index.html

Header set Access-Control-Allow-Origin *

Options FollowSymLinks

AllowOverride All

Require all granted

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值