千家信息网

CentOS7.2配置Apache服务实例分析

发表于:2025-02-03 作者:千家信息网编辑
千家信息网最后更新 2025年02月03日,本篇内容介绍了"CentOS7.2配置Apache服务实例分析"的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学
千家信息网最后更新 2025年02月03日CentOS7.2配置Apache服务实例分析

本篇内容介绍了"CentOS7.2配置Apache服务实例分析"的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

一、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变成allallowoverride all# line 164: 添加只能使用目录名称访问的文件名directoryindex index.html index.cgi index.php# add follows to the end# server's response header(安全性)servertokens prod# keepalive is onkeepalive on[root@linuxprobe ~]# systemctl start httpd[root@linuxprobe ~]# systemctl enable httpd[3] 如果firewalld正在运行,请允许http服务。,http使用80 / tcp[root@linuxprobe ~]# firewall-cmd --add-service=http --permanentsuccess[root@linuxprobe ~]# firewall-cmd --reloadsuccess[4] 创建一个html测试页,并使用web浏览器从客户端pc访问它。如果显示以下页面,是正确的[root@linuxprobe ~]# vi /var/www/html/index.html
welcome access linuxprobe.org,this is test page!

三、支持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.conf247: 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  options +execcgi  addhandler cgi-script .cgi .pl[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/perlprint "content-type: text/html\n\n";print "\n\n";print "
\n";print "cgi test page";print "\n
\n";print "\n\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 ~]# vi /var/www/html/index.php

[3] 创建phpinfo测试页,确认是都开启php支持
[root@linuxprobe ~]# echo "" > /var/www/html/phpinfo.php

五、支持ruby

配置httpd以将ruby脚本用作cgi
[1] 安装ruby.
[root@linuxprobe ~]# yum -y install ruby

[2] 默认情况下,在"/var/www/cgi-bin"目录下允许cgi。
可以使用perl scripts放在目录下。然而,它下面的所有文件都被处理为cgi。

# 下面的设置是cgi的设置[root@linuxprobe ~]# grep -n "^ *scriptalias" /etc/httpd/conf/httpd.conf247: 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 .rb as cgi scripts  options +execcgi  addhandler cgi-script .rb[root@linuxprobe ~]# systemctl restart httpd

[4] 如果selinux被启用,并且允许cgi在不是像上面[3]的默认目录下,更改规则如下。

[root@linuxprobe ~]# chcon -r -t httpd_sys_script_exec_t /var/www/html/cgi-enabled[root@linuxprobe ~]# semanage fcontext -a -t httpd_sys_script_exec_t /var/www/html/cgi-enabled

[5] create a cgi test page and access to it from client pc with web browser. it's ok if following page is shown.

[root@linuxprobe ~]# vi /var/www/html/cgi-enabled/index.rb#!/usr/bin/rubyprint "content-type: text/html\n\n"print "\n\n"print "
\n"print "ruby script test page"print "\n
\n"print "\n\n" [root@linuxprobe ~]# chmod 705 /var/www/html/cgi-enabled/index.rb

六、支持python

启用cgi执行并使用python脚本

[1] 安装python.[root@linuxprobe ~]# yum -y install python[2] 默认情况下,在"/var/www/cgi-bin"目录下允许cgi。 可以使用perl scripts放在目录下。然而,它下面的所有文件都被处理为cgi。# 下面的设置是cgi的设置[root@linuxprobe ~]# grep -n "^ *scriptalias" /etc/httpd/conf/httpd.conf247: 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 .py as cgi scripts  options +execcgi  addhandler cgi-script .py[root@linuxprobe ~]# systemctl restart httpd[4] 如果selinux被启用,并且允许cgi在不是像上面[3]的默认目录下,更改规则如下。[root@linuxprobe ~]# chcon -r -t httpd_sys_script_exec_t /var/www/html/cgi-enabled[root@linuxprobe ~]# semanage fcontext -a -t httpd_sys_script_exec_t /var/www/html/cgi-enabled[5]   create a cgi test page and access to it from client pc with web browser. it's ok if following page is shown.[root@linuxprobe ~]# vi /var/www/html/cgi-enabled/index.py#!/usr/bin/env pythonprint "content-type: text/html\n\n"print "\n\n"print "
\n"print "python script test page"print "\n
\n"print "\n\n" [root@linuxprobe ~]# chmod 705 /var/www/html/cgi-enabled/index.py

七、支持userdir

启用userdir,用户可以使用此设置创建网站

[1] 配置 httpd.[root@linuxprobe ~]# vi /etc/httpd/conf.d/userdir.conf# line 17: comment out#userdir disabled# line 24: uncommentuserdir public_html# line 31 - 35  allowoverride all# change  options none# change  require method get post options[root@linuxprobe ~]# systemctl restart httpd[2] 创建一个测试页,使用普通用户通过客户端pc与web浏览器和访问它,如果显示以下页面,就是正确的[cent@linuxprobe ~]$ mkdir public_html[cent@linuxprobe ~]$ chmod 711 /home/cent[cent@linuxprobe ~]$ chmod 755 /home/cent/public_html[cent@linuxprobe ~]$ vi ./public_html/index.html
userdir test page

浏览器访问:http://linuxprobe.org/~wang/,出现如下界面

八、设置虚拟主机

配置虚拟主机以使用多个域名。
以下示例在域名为[linuxprobe.org],虚拟域名为[virtual.host(根目录[/home/wang/public_html]]的环境中设置。
必须为此示例设置userdir的设置

[1] 配置虚拟主机[root@linuxprobe ~]# vi /etc/httpd/conf.d/vhost.conf# for original domain  documentroot /var/www/html  servername www.linuxprobe.org# for virtual domain  documentroot /home/cent/public_html  servername www.virtual.host  serveradmin webmaster@virtual.host  errorlog logs/virtual.host-error_log  customlog logs/virtual.host-access_log combined[root@linuxprobe ~]# systemctl restart httpd[2]创建测试页并使用web浏览器从客户端计算机访问它。如果显示以下页面,则是正确的:[cent@linuxprobe ~]$ vi ~/public_html/virtual.php
virtual host test page
[3]如果访问测试时看不到相应页面,可通过下面命令进行测试:[root@linuxprobe ~]# yum -y install elinks^c[root@linuxprobe ~]# elinks http://www.virtual.host/virtual.php

九、创建ssl证书

创建自己的ssl证书。但是,如果您使用您的服务器作为业务,最好购买和使用来自verisigh的正式证书等。

[root@linuxprobe ~]# cd /etc/pki/tls/certcert.pem certs/  [root@linuxprobe ~]# cd /etc/pki/tls/certs/[root@linuxprobe certs]# make server.keyumask 77 ; \/usr/bin/openssl genrsa -aes128 2048 > server.keygenerating rsa private key, 2048 bit long modulus...............................................................+++....................................................................................................+++e is 65537 (0x10001)enter pass phrase:verifying - enter pass phrase:[root@linuxprobe certs]# openssl rsa -in server.key -out server.keyenter pass phrase for server.key:writing rsa key[root@linuxprobe certs]# make server.csrumask 77 ; \/usr/bin/openssl req -utf8 -new -key server.key -out server.csryou are about to be asked to enter information that will be incorporatedinto your certificate request.what you are about to enter is what is called a distinguished name or a dn.there are quite a few fields but you can leave some blankfor some fields there will be a default value,if you enter '.', the field will be left blank.-----country name (2 letter code) [xx]:cn  #国家后缀state or province name (full name) []:shanghai #省locality name (eg, city) [default city]:shanghai #市organization name (eg, company) [default company ltd]:linuxprobe #公司organizational unit name (eg, section) []:devops #部门common name (eg, your name or your server's hostname) []:linuxprobe.org #主机名email address []:root@linuxprobe.org #邮箱please enter the following 'extra' attributesto be sent with your certificate requesta challenge password []:  #默认an optional company name []:  #默认#[root@linuxprobe certs]# openssl x509 -in server.csr -out server.crt -req -signkey server.key -days 3650signature oksubject=/c=cn/st=shanghai/l=shanghai/o=linuxprobe/ou=devops/cn=linuxprobe.org/emailaddress=root@linuxprobe.orggetting private key

十、配置ssl

[1] 配置ssl.

[root@linuxprobe ~]# yum -y install mod_ssl[root@linuxprobe ~]# vi /etc/httpd/conf.d/ssl.conf# line 59: 取消注释documentroot "/var/www/html"# line 60: 取消注释,定义域名servername linuxprobe.org:443# line 75: 改变sslprotocolsslprotocol -all +tlsv1 +tlsv1.1 +tlsv1.2# line 100: 改成刚刚创建的server.crtsslcertificatefile /etc/pki/tls/certs/server.crt# line 107: 改成刚刚创建的server.keysslcertificatekeyfile /etc/pki/tls/certs/server.key[root@www ~]# systemctl restart httpd

[2] 如果firewalld正在运行,请允许https服务。 https使用443 / tcp

[root@www ~]# firewall-cmd --add-service=https --permanentsuccess[root@www ~]# firewall-cmd --reloadsuccess

[3] 使用web浏览器通过https从客户端计算机访问测试页。下面的示例是fiorefix。显示以下屏幕,因为证书是自己创建的,但它没有ploblem,继续下一步。

十一、启用基本身份验证

启用基本身份验证以限制特定网页的访问

[1]例如,在目录[/var/www/html/auth-basic]下设置基本身份验证设置。

 [root@linuxprobe ~]# vi /etc/httpd/conf.d/auth_basic.conf# 创建新配置文件  authtype basic  authname "basic authentication"  authuserfile /etc/httpd/conf/.htpasswd  require valid-user# 添加用户:使用"-c"创建新文件(仅为初始注册添加"-c"选项)[root@linuxprobe ~]# htpasswd -c /etc/httpd/conf/.htpasswd wangnew password: # set passwordre-type new password: # confirmadding password for user wang[root@linuxprobe ~]# systemctl restart httpd[root@linuxprobe ~]# mkdir /var/www/html/auth-basic[root@linuxprobe ~]# vi /var/www/html/auth-basic/index.html# create a test page
test page for basic auth

[2] 使用web浏览器从客户端计算机访问测试页。然后需要认证,如下所示作为设置,用在[1]中添加的用户回答

十二、基本auth + pam

限制特定网页上的访问,并使用os用户通过ssl连接进行身份验证
[1] 创建证书,请参照上文所述。
[2] 例如,在[/var/www/html/auth-pam]目录下设置basic auth。

# install from epel[root@linuxprobe ~]# yum --enablerepo=epel -y install mod_authnz_external pwauth[root@linuxprobe ~]# vi /etc/httpd/conf.d/authnz_external.conf# add to the end  sslrequiressl  authtype basic  authname "pam authentication"  authbasicprovider external  authexternal pwauth  require valid-user[root@linuxprobe ~]# mkdir /var/www/html/auth-pam[root@linuxprobe ~]# vi /var/www/html/auth-pam/index.html# create a test page
test page for pam auth
[root@linuxprobe ~]# systemctl restart httpd

[3] 在客户端上使用web浏览器访问测试页面https://linuxprobe.org/auth-pam/,并与操作系统上的用户进行身份验证。

十三、使用webdav

下面是使用ssl连接配置webdav设置的示例
[1] 创建证书,请参照上文所述
[2] 例如,创建一个目录[webdav],它使得可以仅通过ssl连接到webdav目录。

[root@linuxprobe ~]# mkdir /home/webdav[root@linuxprobe ~]# chown apache. /home/webdav[root@linuxprobe ~]# chmod 770 /home/webdav[root@linuxprobe ~]# vi /etc/httpd/conf.d/webdav.conf# create newdavlockdb "/tmp/davlock"alias /webdav /home/webdav  dav on  sslrequiressl  options none  authtype basic  authname webdav  authuserfile /etc/httpd/conf/.htpasswd      require method get post options    require valid-user  # 添加用户:使用"-c"创建新文件(仅为初始注册添加"-c"选项)[root@linuxprobe ~]# htpasswd -c /etc/httpd/conf/.htpasswd wangnew password:   # set passwordre-type new password:adding password for user wang# **注意:用户wang的htpasswd已经创建过,不需要重复创建**[root@linuxprobe ~]# systemctl restart httpd

[3] 如果启用了selinux,请更改以下规则。

[root@linuxprobe ~]# chcon -r -t httpd_sys_rw_content_t /home/webdav[root@linuxprobe ~]# semanage fcontext -a -t httpd_sys_rw_content_t /home/webdav

[4] 这是pc上的webdav客户端的设置(windows 10)。
下载"carotdav",这是一个免费的webdav客户端,从以下网站⇒ ,下载后,安装并启动carotdav,然后显示以下屏幕,单击"文件"按钮并选择"webdav"。

[5]在"设置名称"字段中输入任何名称,并在"uri"字段中输入[服务器名称/ webdav目录],并输入用户名和密码

[7]配置添加如下,点击它连接到服务器。

[8] waring显示如下,它的ssl证书没有安装在您的电脑上,它没有ploblem,点击"忽略",然后去下一步。

[9] 到webdav目录下创建测试目录和文件

[root@linuxprobe tmp]# cd /home/webdav/[root@linuxprobe webdav]# mkdir linuxprobe[root@linuxprobe webdav]# mkdir linuxcool[root@linuxprobe webdav]# touch vdevops.txt[root@linuxprobe webdav]# touch linuxcool.txt

"CentOS7.2配置Apache服务实例分析"的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注网站,小编将为大家输出更多高质量的实用文章!

0