CentOS 配置Apache服务(httpd)
一、Apache简介
Apache HTTP Server(简称Apache)是Apache软件基金会的一个开放源代码的网页服务器软件,可以在大多数电脑操作系统中运行,由于其跨平台和安全性(尽管不断有新的漏洞被发现,但由于其开放源代码的特点,漏洞总能被很快修补。因此总合来说,其安全性还是相当高的。)。被广泛使用,是最流行的Web服务器软件之一。它快速、可靠并且可通过简单的API扩充,将Perl/Python等解释器编译到服务器中。
软件图标
二、安装Apache httpd
安装httpd以配置Web服务器, HTTP使用80 / TCP
[1] 安装 httpd.
[root@linuxprobe ~]# yum -y install httpd
# 删除默认欢迎页面
[root@linuxprobe ~]# rm -f /etc/httpd/conf.d/welcome.conf
[2] 配置httpd,将服务器名称替换为您自己的环境
[root@linuxprobe ~]# vi /etc/httpd/conf/httpd.conf
# line 86: 改变管理员的邮箱地址
ServerAdmin root@linuxprobe.org
# line 95: 改变域名信息
ServerName www.linuxprobe.org:80
# line 151: none变成All
AllowOverride All
# line 164: 添加只能使用目录名称访问的文件名
DirectoryIndex index.html index.cgi index.php
# add follows to the end
# server's response header(安全性)
ServerTokens Prod
# keepalive is ON
KeepAlive On
[root@linuxprobe ~]# systemctl start httpd
[root@linuxprobe ~]# systemctl enable httpd
[3] 如果Firewalld正在运行,请允许HTTP服务。,HTTP使用80 / TCP
[root@linuxprobe ~]# firewall-cmd --add-service=http --permanent
success
[root@linuxprobe ~]# firewall-cmd --reload
success
[4] 创建一个HTML测试页,并使用Web浏览器从客户端PC访问它。如果显示以下页面,是正确的
[root@linuxprobe ~]# vi /var/www/html/index.html
<html>
<body>
<div style="width: 100%; font-size: 40px; font-weight: bold; text-align: center;">
Welcome access LinuxProbe.org,This is Test Page!
</div>
</body>
</html>
三、支持Perl
启用CGI执行并使用Perl脚本
[1] 安装Perl.
[root@linuxprobe ~]# yum -y install perl perl-CGI
[2] 默认情况下,在“/var/www/cgi-bin”目录下允许CGI。
可以使用Perl Scripts放在目录下。然而,它下面的所有文件都被处理为CGI。
# 下面的设置是CGI的设置
[root@linuxprobe ~]# grep -n "^ *ScriptAlias" /etc/httpd/conf/httpd.conf
247: ScriptAlias /cgi-bin/ "/var/www/cgi-bin/"
[3] 如果你想允许在其他目录中的CGI,配置如下。
例如,在“/var/www/html/cgi-enabled”中允许。
[root@linuxprobe ~]# vi /etc/httpd/conf.d/cgi-enabled.conf
# create new
# processes .cgi and .pl as CGI scripts
<Directory "/var/www/html/cgi-enabled">
Options +ExecCGI
AddHandler cgi-script .cgi .pl
</Directory>
[root@linuxprobe ~]# systemctl restart httpd
[4] 如果SELinux被启用,并且允许CGI在不是像上面[3]的默认目录下,更改规则如下。
[root@linuxprobe ~]# chcon -R -t httpd_sys_script_exec_t /var/linuxprobe/html/cgi-enabled
[root@linuxprobe ~]# semanage fcontext -a -t httpd_sys_script_exec_t /var/www/html/cgi-enabled
[5] 创建一个CGI测试页面,并使用Web浏览器从客户端PC访问它。如果显示以下页面,说明配置正确。
[root@linuxprobe ~]# vi /var/www/html/cgi-enabled/index.cgi
#!/usr/bin/perl
print "Content-type: text/html\n\n";
print "<html>\n<body>\n";
print "<div style=\"width: 100%; font-size: 40px; font-weight: bold; text-align: center;\">\n";
print "CGI Test Page";
print "\n</div>\n";
print "</body>\n</html>\n";
[root@linuxprobe ~]# chmod 705 /var/www/html/cgi-enabled/index.cgi
四、支持PHP
配置httpd以使用PHP脚本
[1] 安装PHP.
[root@linuxprobe ~]# yum -y install php php-mbstring php-pear
[root@linuxprobe ~]# vi /etc/php.ini
# line 878: 取消注释,设置时区
date.timezone = "Asia/Shanghai"
[root@linuxprobe ~]# systemctl restart httpd
[2] 创建一个PHP测试页面,并使用Web浏览器从客户端PC访问它。如果显示以下页面,它是确定。
[root@linuxprobe ~]# v