Apache常见配置及问题

1、Apache的配置由httpd.conf文件配置修改。

主站点的配置(基本配置)

(1) 基本配置:

ServerRoot “/mnt/software/apache2” #你的apache软件安装的位置。其它指定的目录如果没有指定绝对路径,则目录是相对于该目录。

PidFile logs/httpd.pid #第一个httpd进程(所有其他进程的父进程)的进程号文件位置。 

Listen 80 #服务器监听的端口号。 

ServerName www.clusting.com:80 #主站点名称(网站的主机名)。 

ServerAdmin admin@clusting.com #管理员的邮件地址。 

DocumentRoot "/mnt/web/clusting" #主站点的网页存储位置。 

以下是对主站点的目录进行访问控制:

<Directory "/mnt/web/clusting"> 
Options FollowSymLinks 
AllowOverride None 
Order allow,deny 
Allow from all 
</Directory> 

在上面这段目录属性配置中,主要有下面的选项:

Options:配置在特定目录使用哪些特性,常用的值和基本含义如下:

ExecCGI: 在该目录下允许执行CGI脚本。

FollowSymLinks: 在该目录下允许文件系统使用符号连接。

Indexes: 当用户访问该目录时,如果用户找不到DirectoryIndex指定的主页文件(例如index.html),则返回该目录下的文件列表给用户。

SymLinksIfOwnerMatch: 当使用符号连接时,只有当符号连接的文件拥有者与实际文件的拥有者相同时才可以访问。

AllowOverride:允许存在于.htaccess文件中的指令类型(.htaccess文件名是可以改变的,其文件名由AccessFileName指令决定):

None: 当AllowOverride被设置为None时。不搜索该目录下的.htaccess文件(可以减小服务器开销)。 

All:.htaccess文件中可以使用所有的指令。 

Order:控制在访问时Allow和Deny两个访问规则哪个优先:

Allow:允许访问的主机列表(可用域名或子网,例如:Allow from 192.168.0.0/16)。 

Deny:拒绝访问的主机列表。 

DirectoryIndex index.html index.htm index.php #主页文件的设置(本例将主页文件设置为:index.html,index.htm和index.php)

(2) 服务器的优化 (MPM: Multi-Processing Modules)

apache2主要的优势就是对多处理器的支持更好,在编译时同过使用–with-mpm选项来决定apache2的工作模式。如果知道当前的apache2使用什么工作机制,可以通过httpd
-l命令列出apache的所有模块,就可以知道其工作方式:

prefork:如果httpd -l列出prefork.c,则需要对下面的段进行配置:

<IfModule prefork.c> 

StartServers 5 #启动apache时启动的httpd进程个数。 

MinSpareServers 5 #服务器保持的最小空闲进程数。 

MaxSpareServers 10 #服务器保持的最大空闲进程数。 

MaxClients 150 #最大并发连接数。 

MaxRequestsPerChild 1000 #每个子进程被请求服务多少次后被kill掉。0表示不限制,推荐设置为1000</IfModule> 

在该工作模式下,服务器启动后起动5个httpd进程(加父进程共6个,通过ps -ax|grep
httpd命令可以看到)。当有用户连接时,apache会使用一个空闲进程为该连接服务,同时父进程会fork一个子进程。直到内存中的空闲进程达到MaxSpareServers。该模式是为了兼容一些旧版本的程序。我缺省编译时的选项。

worker:如果httpd -l列出worker.c,则需要对下面的段进行配置:

<IfModule worker.c> 
StartServers 2 #启动apache时启动的httpd进程个数。 

MaxClients 150 #最大并发连接数。 

MinSpareThreads 25 #服务器保持的最小空闲线程数。 

MaxSpareThreads 75 #服务器保持的最大空闲线程数。 

ThreadsPerChild 25 #每个子进程的产生的线程数。 

MaxRequestsPerChild 0 #每个子进程被请求服务多少次后被kill掉。0表示不限制,推荐设置为1000</IfModule> 

该模式是由线程来监听客户的连接。当有新客户连接时,由其中的一个空闲线程接受连接。服务器在启动时启动两个进程,每个进程产生的线程数是固定的(ThreadsPerChild决定),因此启动时有50个线程。当50个线程不够用时,服务器自动fork一个进程,再产生25个线程。

perchild:如果httpd -l列出perchild.c,则需要对下面的段进行配置:

<IfModule perchild.c> 

NumServers 5 #服务器启动时启动的子进程数 

StartThreads 5 #每个子进程启动时启动的线程数 

MinSpareThreads 5 #内存中的最小空闲线程数 

MaxSpareThreads 10 #最大空闲线程数 

MaxThreadsPerChild 2000 #每个线程最多被请求多少次后退出。0不受限制。 

MaxRequestsPerChild 10000 #每个子进程服务多少次后被重新fork。0表示不受限制。 

</IfModule> 

该模式下,子进程的数量是固定的,线程数不受限制。当客户端连接到服务器时,又空闲的线程提供服务。
如果空闲线程数不够,子进程自动产生线程来为新的连接服务。该模式用于多站点服务器。

(3) HTTP返头回信息配置:

ServerTokens Prod #该参数设置http头部返回的apache版本信息,可用的值和含义如下:

Prod:仅软件名称,例如:apache 
Major:包括主版本号,例如:apache/2 
Minor:包括次版本号,例如:apache/2.0 
Min:仅apache的完整版本号,例如:apache/ 2.0.54 
OS:包括操作系统类型,例如:apache/2.0.54(Unix) 
Full:包括apache支持的模块及模块版本号,例如:Apache/2.0.54 (Unix) mod_ssl/2.0.54 OpenSSL/0.9.7g 
ServerSignature Off #在页面产生错误时是否出现服务器版本信息。推荐设置为Off 

(4) 持久性连接设置

KeepAlive On #开启持久性连接功能。即当客户端连接到服务器,下载完数据后仍然保持连接状态。 

MaxKeepAliveRequests 100 #一个连接服务的最多请求次数。 

KeepAliveTimeout 30 #持续连接多长时间,该连接没有再请求数据,则断开该连接。缺省为15秒。

别名设置

对于不在DocumentRoot指定的目录内的页面,既可以使用符号连接,也可以使用别名。别名的设置如下:

Alias /download/ "/var/www/download/" #访问时可以输入:http://www.custing.com/download/ 

<Directory "/var/www/download"> #对该目录进行访问控制设置 
Options Indexes MultiViews 
AllowOverride AuthConfig 
Order allow,deny 
Allow from all 
</Directory> 

CGI设置

ScriptAlias /cgi-bin/ "/mnt/software/apache2/cgi-bin/" # 访问时可以:http://www.clusting.com/cgi-bin/ 。但是该目录下的CGI脚本文件要加可执行权限!
<Directory "/usr/local/apache2/cgi-bin"> #设置目录属性 
AllowOverride None 
Options None 
Order allow,deny 
Allow from all 
</Directory> 

日志的设置

(1)错误日志的设置
ErrorLog logs/error_log #日志的保存位置 LogLevel warn #日志的级别
显示的格式日下:

[Mon Oct 10 15:54:29 2005] [error] [client 192.168.10.22] access to /download/ failed, reason: user admin not allowed access

(2)访问日志设置

日志的缺省格式有如下几种:

LogFormat "%h %l %u %t "%r" %>s %b "%{Referer}i" "%{User-Agent}i"" combined 
LogFormat "%h %l %u %t "%r" %>s %b" common #common为日志格式名称 
LogFormat "%{Referer}i -> %U" referer 
LogFormat "%{User-agent}i" agent 
CustomLog logs/access_log common 

格式中的各个参数如%h --客户端的ip地址或主机名

%l --The 这是由客户端 identd 判断的RFC 1413身份,输出中的符号 "-" 表示此处信息无效。 

%u --由HTTP认证系统得到的访问该网页的客户名。有认证时才有效,输出中的符号 "-" 表示此处信息无效。 

%t --服务器完成对请求的处理时的时间。 

"%r" --引号中是客户发出的包含了许多有用信息的请求内容。 

%>s --这个是服务器返回给客户端的状态码。 

%b --最后这项是返回给客户端的不包括响应头的字节数。 

"%{Referer}i" --此项指明了该请求是从被哪个网页提交过来的。 

"%{User-Agent}i" --此项是客户浏览器提供的浏览器识别信息。 一段访问日志的实例: 
192.168.10.22 - bearzhang [10/Oct/2005:16:53:06 +0800] "GET /download/ HTTP/1.1" 200 1228 
192.168.10.22 - - [10/Oct/2005:16:53:06 +0800] "GET /icons/blank.gif HTTP/1.1" 304 - 
192.168.10.22 - - [10/Oct/2005:16:53:06 +0800] "GET /icons/back.gif HTTP/1.1" 304 - 

用户认证的配置

(1)in the httpd.conf:

AccessFileName .htaccess 
......... 
Alias /download/ "/var/www/download/" 
<Directory "/var/www/download"> 
Options Indexes 
AllowOverride AuthConfig 
</Directory> 
(2) create a password file:
/usr/local/apache2/bin/htpasswd -c /var/httpuser/passwords bearzhang 
(3)onfigure the server to request a password and tell the server which users are allowed access. 
vi /var/www/download/.htaccess:

AuthType Basic 
AuthName "Restricted Files" 
AuthUserFile /var/httpuser/passwords 
Require user bearzhang 
# Require valid-user #all valid user 

虚拟主机的配置

(1)基于IP地址的虚拟主机配置

Listen 80 
<VirtualHost 172.20.30.40> 
DocumentRoot /www/example1 
ServerName www.example1.com 
</VirtualHost> 
<VirtualHost 172.20.30.50> 
DocumentRoot /www/example2 
ServerName www.example2.org 
</VirtualHost> 

(2) 基于IP和多端口的虚拟主机配置

Listen 172.20.30.40:80 
Listen 172.20.30.40:8080 
Listen 172.20.30.50:80 
Listen 172.20.30.50:8080 

<VirtualHost 172.20.30.40:80> 
DocumentRoot /www/example1-80 
ServerName www.example1.com 
</VirtualHost> 

<VirtualHost 172.20.30.40:8080> 
DocumentRoot /www/example1-8080 
ServerName www.example1.com 
</VirtualHost> 

<VirtualHost 172.20.30.50:80> 
DocumentRoot /www/example2-80 
ServerName www.example1.org 
</VirtualHost> 

<VirtualHost 172.20.30.50:8080> 
DocumentRoot /www/example2-8080 
ServerName www.example2.org 
</VirtualHost> 

(3)单个IP地址的服务器上基于域名的虚拟主机配置:

# Ensure that Apache listens on port 80 
Listen 80 

# Listen for virtual host requests on all IP addresses 
NameVirtualHost *:80 

<VirtualHost *:80> 
DocumentRoot /www/example1 
ServerName www.example1.com 
ServerAlias example1.com. *.example1.com 
# Other directives here 
</VirtualHost> 

<VirtualHost *:80> 
DocumentRoot /www/example2 
ServerName www.example2.org 
# Other directives here 
</VirtualHost> 

(4)在多个IP地址的服务器上配置基于域名的虚拟主机:

Listen 80 

# This is the "main" server running on 172.20.30.40 
ServerName server.domain.com 
DocumentRoot /www/mainserver 

# This is the other address 
NameVirtualHost 172.20.30.50 

<VirtualHost 172.20.30.50> 
DocumentRoot /www/example1 
ServerName www.example1.com 
# Other directives here ... 
</VirtualHost> 

<VirtualHost 172.20.30.50> 
DocumentRoot /www/example2 
ServerName www.example2.org 
# Other directives here ... 
</VirtualHost> 

(5)在不同的端口上运行不同的站点(基于多端口的服务器上配置基于域名的虚拟主机):

Listen 80 
Listen 8080 

NameVirtualHost 172.20.30.40:80 
NameVirtualHost 172.20.30.40:8080 

<VirtualHost 172.20.30.40:80> 
ServerName www.example1.com 
DocumentRoot /www/domain-80 
</VirtualHost> 

<VirtualHost 172.20.30.40:8080> 
ServerName www.example1.com 
DocumentRoot /www/domain-8080 
</VirtualHost> 

<VirtualHost 172.20.30.40:80> 
ServerName www.example2.org 
DocumentRoot /www/otherdomain-80 
</VirtualHost> 

<VirtualHost 172.20.30.40:8080> 
ServerName www.example2.org 
DocumentRoot /www/otherdomain-8080 
</VirtualHost> 

(6)配置多个站点,IP地址相同,但端口号不同

现在我的CentOS上,只有一个IP:192.168.0.94,我想分别使用
8080和8081两个端口配臵两个网站,编辑httpd.conf:
Listen 8080
Listen 8081

<VirtualHost 192.168.0.94:8080>
DocumentRoot /var/www/web1
DirectoryIndex index.html index.htm
HostNameLookups off
</VirtualHost>
<VirtualHost 192.168.0.94:8081>
DocumentRoot /var/www/web2
DirectoryIndex index.html index.htm
HostNameLookups off
</VirtualHost>

重启服务,即可。

(7)端口号相同,但IP地址不同,假如一个是94。

一个是95
配置虚拟服务器,当需要在一台centos服务器上配置多个站点时,使用虚拟服务器配置。

<VirtualHost 192.168.0.94>
ServerName 192.168.0.94:80
DocumentRoot /var/www/web1
DirectoryIndex index.html index.htm
</VirtualHost>

<VirtualHost 192.168.0.95>
ServerName 192.168.0.95:80
DocumentRoot /var/www/web2
DirectoryIndex index.html index.htm
</VirtualHost>

(8)基于域名的虚拟主机

配臵虚拟服务器,当需要在一台centos服务器上配臵多
个站点时,使用虚拟服务器配臵。
基于域名的虚拟主机

 NameVirtualHost 192.168.0.94<VirtualHost www.web1.com>
ServerName www.web1.com:80
DocumentRoot /var/www/web1
DirectoryIndex index.html index.htm
</VirtualHost>

<VirtualHost www.web2.com>
ServerName www.web2.com:80
DocumentRoot /var/www/web2
DirectoryIndex index.html index.htm
</VirtualHost

(9)基于域名和基于IP的混合虚拟主机的配置:

Listen 80 

NameVirtualHost 172.20.30.40 

<VirtualHost 172.20.30.40> 
DocumentRoot /www/example1 
ServerName www.example1.com 
</VirtualHost> 

<VirtualHost 172.20.30.40> 
DocumentRoot /www/example2 
ServerName www.example2.org 
</VirtualHost> 

<VirtualHost 172.20.30.40> 
DocumentRoot /www/example3 
ServerName www.example3.net 
</VirtualHost> 

SSL加密的配置

首先在配置之前先来了解一些基本概念:

证书的概念:首先要有一个根证书,然后用根证书来签发服务器证书和客户证书,一般理解:服务器证书和客户证书是平级关系。SSL必须安装服务器证书来认证。
因此:在此环境中,至少必须有三个证书:根证书,服务器证书,客户端证书。
在生成证书之前,一般会有一个私钥,同时用私钥生成证书请求,再利用证书服务器的根证来签发证书。

SSL所使用的证书可以自己生成,也可以通过一个商业性CA(如Verisign 或 Thawte)签署证书。

签发证书的问题:如果使用的是商业证书,具体的签署方法请查看相关销售商的说明;如果是知己签发的证书,可以使用openssl自带的CA.sh脚本工具。

如果不为单独的客户端签发证书,客户端证书可以不用生成,客户端与服务器端使用相同的证书。

(1) conf/ssl.conf 配置文件中的主要参数配置如下:

Listen 443 
SSLPassPhraseDialog buildin 
#SSLPassPhraseDialog exec:/path/to/program 
SSLSessionCache dbm:/usr/local/apache2/logs/ssl_scache 
SSLSessionCacheTimeout 300 
SSLMutex file:/usr/local/apache2/logs/ssl_mutex 

<VirtualHost _default_:443> 

# General setup for the virtual host 
DocumentRoot "/usr/local/apache2/htdocs" 
ServerName www.example.com:443 
ServerAdmin you@example.com 
ErrorLog /usr/local/apache2/logs/error_log 
TransferLog /usr/local/apache2/logs/access_log 

SSLEngine on 
SSLCipherSuite ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP:+eNULL 

SSLCertificateFile /usr/local/apache2/conf/ssl.crt/server.crt 
SSLCertificateKeyFile /usr/local/apache2/conf/ssl.key/server.key 
CustomLog /usr/local/apache2/logs/ssl_request_log "%t %h %{SSL_PROTOCOL}x %{SSL_CIPHER}x "%r" %b" 

</VirtualHost> 

(2) 创建和使用自签署的证书:

a.Create a RSA private key for your Apache server 
/usr/local/openssl/bin/openssl genrsa -des3 -out /usr/local/apache2/conf/ssl.key/server.key 1024 

b. Create a Certificate Signing Request (CSR) 
/usr/local/openssl/bin/openssl req -new -key /usr/local/apache2/conf/ssl.key/server.key -out /usr/local/apache2/conf/ssl.key/server.csr 

c. Create a self-signed CA Certificate (X509 structure) with the RSA key of the CA 
/usr/local/openssl/bin/openssl req -x509 -days 365 -key /usr/local/apache2/conf/ssl.key/server.key -in /usr/local/apache2/conf/ssl.key/server.csr -out /usr/local/apache2/conf/ssl.crt/se`这里写代码片`rver.crt 

/usr/local/openssl/bin/openssl genrsa 1024 -out server.key 
/usr/local/openssl/bin/openssl req -new -key server.key -out server.csr 
/usr/local/openssl/bin/openssl req -x509 -days 365 -key server.key -in server.csr -out server.crt 

(3) 创建自己的CA(认证证书),并使用该CA来签署服务器的证书。

mkdir /CA 
cd /CA 
cp openssl-0.9.7g/apps/CA.sh /CA 
./CA.sh -newca 
openssl genrsa -des3 -out server.key 1024 
openssl req -new -key server.key -out server.csr 
cp server.csr newreq.pem 
./CA.sh -sign 
cp newcert.pem /usr/local/apache2/conf/ssl.crt/server.crt 
cp server.key /usr/local/apache2/conf/ssl.key/

2、CentOS 7下yum安装Apache及不解析php问题的解决

首先,说一下问题发生的场景:

在CentOS 7下用 yum 安装 apache ,因为 CentOS 的源自带 php 5.4 不能符合环境要求,而不想用其他源,所以选择源码编译安装 php 5.6

安装完毕后,apache 不解析 php ,不解析的现象是浏览器直接显示或下载了 php 文件的源代码

过程略带说一下了,具体步骤自行搜索,然后重点说下容易踩的几个坑

yum 安装 apache 后,必须安装依赖包 httpd-devel ,否则是不存在文件 apxs 的,而 apxs 的路径在编译 php 时需要配置

yum install httpd yum install httpd-devel

查看 apsx 所在路径

rpm -ql httpd-devel|grep apxs /usr/bin/apxs //此行为 grep 结果,不同系统的路径可能不同,以实际结果为准,下同 /sur/share/man/man1/apxs.1.gz

编译php时,加入 apxs 路径参数,作用是促使生成 libphp5.so

./configure 
--with-apxs2=/usr/bin/apxs

安装

make && make install

配置服务、启动服务、环境变量,此处略

修改apache配置文件,Centos7.4 下的apache2.4的配置文件路径为

vim /etc/httpd/conf/httpd.conf

//在LoadModule后面添加:LoadModule php5_module modules/libphp5.so //不添加则访问.php文件将会变成下载 //在DirectoryIndex后面添加:index.php //在AddType application/x-gzip .gz .tgz后面添加:AddType application/x-httpd-php .php //.php前面有一个空格

常用命令

#设置开机启动:systemctl enable httpd.service 
#启动服务: systemctl start httpd.service
#停止服务: systemctl stop httpd.service 
#重启服务: systemctl reload httpd.service

3、apache日志轮询

对apache日志,进行定期备份
备份/usr/local/apache2/logs/access_log日志

#vi 
/usr/local/apache2/logs/access_log {    #存放日志的路径
        daily            #每天备份日志一次
        create             #以天为,创建的日志名
        rotate 30          #切割的日志存放时间为30

备份

/var/log/wtmp {
     monthly          #一个月创建一次
     create 0644 root utmp    #权限0644 .root用户
          minsize 1M     #存放文件最小为1M
     rotate 1     #存放一天
}

在这里插入图片描述
方法二、在logrotate中,把apache日志加入轮替

#vi /etc/logrotate.conf
/var/log/btmp
    missingok
    monthly
    create 0600 root utmp
    rotate 1
}    

在下面添加
/usr/local/apache2/logs/access_log {
   daily
   create
   rotate 30
}

#logrotate -f /etc/logrotate.conf     #强制执行配置文件里面的信息
#cd /usr/local/apache2/logs
#ls         #查看以日期为名字备份的日志文件

在这里插入图片描述

3、添加配置虚拟主机(实测)

在配置文件里面搜索virtual下面添加

#cd /etc/httpd/conf
#sudo vim httpd.conf
<VirtualHost *:80>
        ServerName www.we.com
        DocumentRoot /data/www    
        <Directory "/data/www/">
                Options Indexes FollowSymLinks
                AllowOverride None
                Require all granted
        </Directory>
</VirtualHost>

在这里插入图片描述
重启apache服务

#service httpd restart

新建目录

#mkdir -p /data/www
#cd /data/www
#vi index.conf
123

在/etc/hosts文件里面添加指向www.we.com域名,就可以在浏览器里面访问了

192.168.1.168 www.we.com

在这里插入图片描述
测试是否添加成功,ping通了,说明配置成功了。

#ping www.we.com

在这里插入图片描述
关闭防火墙

#临时关闭
#setenforce 0     #在浏览器里面输出网址www.we.com查看显示界面

#永久关闭
#vi /etc/selinux/config
SELINUX=enforcing
改成disabled

配置多个虚拟主机

在这里插入图片描述

4、Apache的虚拟主机配置伪静态操作

设置伪静态
在配置文件里面,搜索LoadModule

# vi httpd.conf
LoadModule rewrite_module modules/mod_rewrite.so
#service httpd restart

在这里插入图片描述
设置伪静态规则
将所有的请求都转发到www.we.com上。.htmp和html文件
在这里插入图片描述
重启服务

#service httpd restart

浏览器里面访问
在这里插入图片描述

apache 有几种工作模式,分别介绍下其特点,并说明什么情况下采用不同的工作模式?

apache 主要有三种工作模式:prefork、worker、event

prefork 的特点是:(预派生)
1.这种模式可以不必在请求到来时再产生新的进程,从而减小了系统开销
2.可以防止意外的内存泄漏
3.在服务器负载下降的时候会自动减少子进程数

worker 的特点是:
支持混合的多线程多进程的多路处理模块如果对于一个高流量的 HTTP 服务器 worker MPM 是一个比较好的选择,因为 workerMPM 占用的内存要比 prefork 要小。

Event MPM:
event 模式是在 2.4 版本中才可以稳定运行。这是 Apache 最新的工作模式,它和 worker 模式很像,
不同的是在于它解决了 keep-alive 长连接的时候占用线程资源被浪费的问题,在 event 工作模式中,会有一些专门的线程用来管理这些 keep-alive 类型的线程,当有真实请求过来的时候,将请求传递给服务器的线程,执行完毕后,又允许它释放。这增强了在高并发场景下的请求处理。

扩展

通过 apache 访问日志 access.log 统计 IP 和每个地址访问的次数
按访问量列出前 10 名。日志格式样例如下

#curl http://192.168.1.63 #自己产生一些日志
#cat /var/log/httpd/access_log | awk '{ print $1 }' | uniq -c|sort
-rn|head -10

运行参数优化

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述






参考链接 :
Apache的配置由httpd.conf文件配置修改。
https://www.jianshu.com/p/7a46b205da37

CentOS 7下yum安装Apache及不解析php问题的解决 : https://www.jianshu.com/p/83d5fea3349f

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值