分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 网页技术

超详细 值得收藏 linux CentOS 7 配置Apache服务【转发+新增】

发布时间:2023-09-06 01:32责任编辑:白小东关键词:配置

一、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<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 [1] 安装Perl. 2 [root@linuxprobe ~]# yum -y install perl perl-CGI 3 [2] 默认情况下,在“/var/www/cgi-bin”目录下允许CGI。 ??4 可以使用Perl Scripts放在目录下。然而,它下面的所有文件都被处理为CGI。 5 # 下面的设置是CGI的设置 6 [root@linuxprobe ~]# grep -n "^ *ScriptAlias" /etc/httpd/conf/httpd.conf 7 247: ScriptAlias /cgi-bin/ "/var/www/cgi-bin/" 8 [3] 如果你想允许在其他目录中的CGI,配置如下。 ??9 例如,在“/var/www/html/cgi-enabled”中允许。10 [root@linuxprobe ~]# vi /etc/httpd/conf.d/cgi-enabled.conf11 # create new12 # processes .cgi and .pl as CGI scripts13 <Directory "/var/www/html/cgi-enabled">14 ????Options +ExecCGI15 ????AddHandler cgi-script .cgi .pl16 </Directory>17 [root@linuxprobe ~]# systemctl restart httpd18 [4] 如果SELinux被启用,并且允许CGI在不是像上面[3]的默认目录下,更改规则如下。19 [root@linuxprobe ~]# chcon -R -t httpd_sys_script_exec_t /var/linuxprobe/html/cgi-enabled20 [root@linuxprobe ~]# semanage fcontext -a -t httpd_sys_script_exec_t /var/www/html/cgi-enabled21 [5] 创建一个CGI测试页面,并使用Web浏览器从客户端PC访问它。如果显示以下页面,说明配置正确。22 [root@linuxprobe ~]# vi /var/www/html/cgi-enabled/index.cgi23 #!/usr/bin/perl24 print "Content-type: text/html\n\n";25 print "<html>\n<body>\n";26 print "<div style=\"width: 100%; font-size: 40px; font-weight: bold; text-align: center;\">\n";27 print "CGI Test Page";28 print "\n</div>\n";29 print "</body>\n</html>\n";30 [root@linuxprobe ~]# chmod 705 /var/www/html/cgi-enabled/index.cgi 
 

四、支持PHP

  • 配置httpd以使用PHP脚本
 1 [1] 安装PHP. 2 [root@linuxprobe ~]# yum -y install php php-mbstring php-pear 3 [root@linuxprobe ~]# vi /etc/php.ini 4 # line 878: 取消注释,设置时区 5 date.timezone = "Asia/Shanghai" 6 [root@linuxprobe ~]# systemctl restart httpd 7 ?8 [2] 创建一个PHP测试页面,并使用Web浏览器从客户端PC访问它。如果显示以下页面,它是确定。 9 [root@linuxprobe ~]# vi /var/www/html/index.php10 <html>11 <body>12 <div style="width: 100%; font-size: 40px; font-weight: bold; text-align: center;">13 <?php14 ???print Date("Y/m/d");15 ?>16 </div>17 </body>18 </html>
 
1 [3] 创建phpinfo测试页,确认是都开启php支持2 [root@linuxprobe ~]# echo "<?php phpinfo(); ?>" > /var/www/html/phpinfo.php

五、支持Ruby

  • 配置httpd以将Ruby脚本用作CGI
 1 [1] 安装Ruby. 2 [root@linuxprobe ~]# yum -y install ruby 3 ?4 [2] 默认情况下,在“/var/www/cgi-bin”目录下允许CGI。 ??5 可以使用Perl Scripts放在目录下。然而,它下面的所有文件都被处理为CGI。 6 # 下面的设置是CGI的设置 7 [root@linuxprobe ~]# grep -n "^ *ScriptAlias" /etc/httpd/conf/httpd.conf 8 247: ScriptAlias /cgi-bin/ "/var/www/cgi-bin/" 9 10 [3] 如果你想允许在其他目录中的CGI,配置如下。 ?11 例如,在“/var/www/html/cgi-enabled”中允许。12 [root@linuxprobe ~]# vi /etc/httpd/conf.d/cgi-enabled.conf13 # create new14 # processes .rb as CGI scripts15 <Directory "/var/www/html/cgi-enabled">16 ????Options +ExecCGI17 ????AddHandler cgi-script .rb18 </Directory>19 [root@linuxprobe ~]# systemctl restart httpd20 21 [4] 如果SELinux被启用,并且允许CGI在不是像上面[3]的默认目录下,更改规则如下。22 [root@linuxprobe ~]# chcon -R -t httpd_sys_script_exec_t /var/www/html/cgi-enabled23 24 [root@linuxprobe ~]# semanage fcontext -a -t httpd_sys_script_exec_t /var/www/html/cgi-enabled25 26 [5] ????Create a CGI test page and access to it from client PC with web browser. It‘s OK if following page is shown.27 [root@linuxprobe ~]# vi /var/www/html/cgi-enabled/index.rb28 29 #!/usr/bin/ruby30 print "Content-type: text/html\n\n"31 print "<html>\n<body>\n"32 print "<div style=\"width: 100%; font-size: 40px; font-weight: bold; text-align: center;\">\n"33 print "Ruby Script Test Page"34 print "\n</div>\n"35 print "</body>\n</html>\n" 36 [root@linuxprobe ~]# chmod 705 /var/www/html/cgi-enabled/index.rb 
 

六、支持Python

  • 启用CGI执行并使用Python脚本
 1 [1] 安装python. 2 [root@linuxprobe ~]# yum -y install python 3 ?4 [2] 默认情况下,在“/var/www/cgi-bin”目录下允许CGI。 ??5 可以使用Perl Scripts放在目录下。然而,它下面的所有文件都被处理为CGI。 6 # 下面的设置是CGI的设置 7 [root@linuxprobe ~]# grep -n "^ *ScriptAlias" /etc/httpd/conf/httpd.conf 8 247: ScriptAlias /cgi-bin/ "/var/www/cgi-bin/" 9 10 [3] 如果你想允许在其他目录中的CGI,配置如下。 ?11 例如,在“/var/www/html/cgi-enabled”中允许。12 [root@linuxprobe ~]# vi /etc/httpd/conf.d/cgi-enabled.conf13 # create new14 # processes .py as CGI scripts15 <Directory "/var/www/html/cgi-enabled">16 ????Options +ExecCGI17 ????AddHandler cgi-script .py18 </Directory>19 [root@linuxprobe ~]# systemctl restart httpd20 21 [4] 如果SELinux被启用,并且允许CGI在不是像上面[3]的默认目录下,更改规则如下。22 [root@linuxprobe ~]# chcon -R -t httpd_sys_script_exec_t /var/www/html/cgi-enabled23 [root@linuxprobe ~]# semanage fcontext -a -t httpd_sys_script_exec_t /var/www/html/cgi-enabled24 25 [5] ????Create a CGI test page and access to it from client PC with web browser. It‘s OK if following page is shown.26 [root@linuxprobe ~]# vi /var/www/html/cgi-enabled/index.py27 28 #!/usr/bin/env python29 30 print "Content-type: text/html\n\n"31 print "<html>\n<body>\n"32 print "<div style=\"width: 100%; font-size: 40px; font-weight: bold; text-align: center;\">\n"33 print "Python Script Test Page"34 print "\n</div>\n"35 print "</body>\n</html>\n" 36 37 [root@linuxprobe ~]# chmod 705 /var/www/html/cgi-enabled/index.py

7、支持Userdir

  • 启用userdir,用户可以使用此设置创建网站
 1 [1] 配置 httpd. 2 [root@linuxprobe ~]# vi /etc/httpd/conf.d/userdir.conf 3 # line 17: comment out 4 #UserDir disabled 5 # line 24: uncomment 6 UserDir public_html 7 # line 31 - 35 8 ?9 <Directory "/home/*/public_html">10 ????AllowOverride All11 # change12 13 ????Options None14 # change15 16 ????Require method GET POST OPTIONS17 </Directory>18 [root@linuxprobe ~]# systemctl restart httpd19 20 [2] 创建一个测试页,使用普通用户通过客户端PC与Web浏览器和访问它,如果显示以下页面,就是正确的21 [cent@linuxprobe ~]$ mkdir public_html22 23 [cent@linuxprobe ~]$ chmod 711 /home/cent24 25 [cent@linuxprobe ~]$ chmod 755 /home/cent/public_html26 27 [cent@linuxprobe ~]$ vi ./public_html/index.html28 29 <html>30 <body>31 <div style="width: 100%; font-size: 40px; font-weight: bold; text-align: center;">32 UserDir Test Page33 </div>34 </body>35 </html>
 

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

8、设置虚拟主机

  • 配置虚拟主机以使用多个域名。 
    以下示例在域名为[linuxprobe.org],虚拟域名为[virtual.host(根目录[/home/wang/public_html]]的环境中设置。 
    必须为此示例设置Userdir的设置
 1 [1] 配置虚拟主机 2 [root@linuxprobe ~]# vi /etc/httpd/conf.d/vhost.conf 3 # for original domain 4 ?5 <VirtualHost *:80> 6 ???DocumentRoot /var/www/html 7 ???ServerName www.linuxprobe.org 8 </VirtualHost> 9 # for virtual domain10 11 <VirtualHost *:80>12 ???DocumentRoot /home/cent/public_html13 ???ServerName www.virtual.host14 ???ServerAdmin webmaster@virtual.host15 ???ErrorLog logs/virtual.host-error_log16 ???CustomLog logs/virtual.host-access_log combined17 </VirtualHost>18 [root@linuxprobe ~]# systemctl restart httpd19 20 [2]创建测试页并使用Web浏览器从客户端计算机访问它。如果显示以下页面,则是正确的:21 [cent@linuxprobe ~]$ vi ~/public_html/virtual.php22 <html>23 <body>24 <div style="width: 100%; font-size: 40px; font-weight: bold; text-align: center;">25 Virtual Host Test Page26 </div>27 </body>28 </html>29 [3]如果访问测试时看不到相应页面,可通过下面命令进行测试:30 [root@linuxprobe ~]# yum -y install elinks^C31 [root@linuxprobe ~]# elinks http://www.virtual.host/virtual.php
 

9、创建SSL证书

  • 创建自己的SSL证书。但是,如果您使用您的服务器作为业务,最好购买和使用来自Verisigh的正式证书等。
 1 [root@linuxprobe ~]# cd /etc/pki/tls/cert 2 cert.pem ?certs/ ????3 [root@linuxprobe ~]# cd /etc/pki/tls/certs/ 4 [root@linuxprobe certs]# make server.key 5 umask 77 ; ?6 /usr/bin/openssl genrsa -aes128 2048 > server.key 7 Generating RSA private key, 2048 bit long modulus 8 ...............................................................+++ 9 ....................................................................................................+++10 e is 65537 (0x10001)11 Enter pass phrase:12 Verifying - Enter pass phrase:13 [root@linuxprobe certs]# openssl rsa -in server.key -out server.key14 Enter pass phrase for server.key:15 writing RSA key16 [root@linuxprobe certs]# make server.csr17 umask 77 ; 18 /usr/bin/openssl req -utf8 -new -key server.key -out server.csr19 You are about to be asked to enter information that will be incorporated20 into your certificate request.21 What you are about to enter is what is called a Distinguished Name or a DN.22 There are quite a few fields but you can leave some blank23 For some fields there will be a default value,24 If you enter ‘.‘, the field will be left blank.25 -----26 Country Name (2 letter code) [XX]:CN ???#国家后缀27 State or Province Name (full name) []:Shanghai ?#省28 Locality Name (eg, city) [Default City]:Shanghai ?#市29 Organization Name (eg, company) [Default Company Ltd]:LinuxProbe ?#公司30 Organizational Unit Name (eg, section) []:DevOps ?#部门31 Common Name (eg, your name or your server‘s hostname) []:linuxprobe.org ?#主机名32 Email Address []:root@linuxprobe.org ?#邮箱33 34 Please enter the following ‘extra‘ attributes35 to be sent with your certificate request36 A challenge password []: ???#默认37 An optional company name []: ???#默认38 #39 [root@linuxprobe certs]# openssl x509 -in server.csr -out server.crt -req -signkey server.key -days 365040 Signature ok41 subject=/C=CN/ST=Shanghai/L=Shanghai/O=LinuxProbe/OU=DevOps/CN=linuxprobe.org/emailAddress=root@linuxprobe.org42 Getting Private key

10、配置SSL

 1 [1] 配置SSL. 2 [root@linuxprobe ~]# yum -y install mod_ssl 3 [root@linuxprobe ~]# vi /etc/httpd/conf.d/ssl.conf 4 # line 59: 取消注释 5 DocumentRoot "/var/www/html" 6 # line 60: 取消注释,定义域名 7 ServerName linuxprobe.org:443 8 # line 75: 改变SSLProtocol 9 SSLProtocol -All +TLSv1 +TLSv1.1 +TLSv1.210 11 # line 100: 改成刚刚创建的server.crt12 SSLCertificateFile /etc/pki/tls/certs/server.crt13 # line 107: 改成刚刚创建的server.key14 SSLCertificateKeyFile /etc/pki/tls/certs/server.key15 [root@www ~]# systemctl restart httpd16 17 [2] 如果Firewalld正在运行,请允许HTTPS服务。 HTTPS使用443 / TCP18 [root@www ~]# firewall-cmd --add-service=https --permanent19 success20 [root@www ~]# firewall-cmd --reload21 success22 [3] 使用Web浏览器通过HTTPS从客户端计算机访问测试页。下面的示例是Fiorefix。显示以下屏幕,因为证书是自己创建的,但它没有ploblem,继续下一步。
 

11、启用基本身份验证

  • 启用基本身份验证以限制特定网页的访问
 1 [1]例如,在目录[/var/www/html/auth-basic]下设置基本身份验证设置。 2 ?[root@linuxprobe ~]# vi /etc/httpd/conf.d/auth_basic.conf 3 # 创建新配置文件 4 <Directory /var/www/html/auth-basic> 5 ????AuthType Basic 6 ????AuthName "Basic Authentication" 7 ????AuthUserFile /etc/httpd/conf/.htpasswd 8 ????require valid-user 9 </Directory>10 # 添加用户:使用“-c”创建新文件(仅为初始注册添加“-c”选项)11 [root@linuxprobe ~]# htpasswd -c /etc/httpd/conf/.htpasswd wang12 13 New password: # set password14 15 Re-type new password: # confirm16 17 Adding password for user wang18 [root@linuxprobe ~]# systemctl restart httpd19 [root@linuxprobe ~]# mkdir /var/www/html/auth-basic20 21 [root@linuxprobe ~]# vi /var/www/html/auth-basic/index.html22 # create a test page23 24 <html>25 <body>26 <div style="width: 100%; font-size: 40px; font-weight: bold; text-align: wanger;">27 Test Page for Basic Auth28 </div>29 </body>30 </html>31 32 [2] 使用Web浏览器从客户端计算机访问测试页。然后需要认证,如下所示作为设置,用在[1]中添加的用户回答

[3] 访问成功 

12、基本Auth + PAM

  • 限制特定网页上的访问,并使用OS用户通过SSL连接进行身份验证
 1 [1] 创建证书,请参照上文所述。 2 [2] 例如,在[/var/www/html/auth-pam]目录下设置Basic Auth。 3 # install from EPEL 4 [root@linuxprobe ~]# yum --enablerepo=epel -y install mod_authnz_external pwauth 5 [root@linuxprobe ~]# vi /etc/httpd/conf.d/authnz_external.conf 6 # add to the end 7 ?8 <Directory /var/www/html/auth-pam> 9 ????SSLRequireSSL10 ????AuthType Basic11 ????AuthName "PAM Authentication"12 ????AuthBasicProvider external13 ????AuthExternal pwauth14 ????require valid-user15 </Directory>16 17 [root@linuxprobe ~]# mkdir /var/www/html/auth-pam18 19 [root@linuxprobe ~]# vi /var/www/html/auth-pam/index.html20 # create a test page21 22 <html>23 <body>24 <div style="width: 100%; font-size: 40px; font-weight: bold; text-align: center;">25 Test Page for PAM Auth26 </div>27 </body>28 </html>29 30 [root@linuxprobe ~]# systemctl restart httpd31 [3] ?在客户端上使用Web浏览器访问测试页面https://linuxprobe.org/auth-pam/,并与操作系统上的用户进行身份验证。
 


13、使用WebDAV

  • 下面是使用SSL连接配置WebDAV设置的示例
 1 [1] 创建证书,请参照上文所述 2 [2] 例如,创建一个目录[webdav],它使得可以仅通过SSL连接到WebDAV目录。 3 [root@linuxprobe ~]# mkdir /home/webdav 4 [root@linuxprobe ~]# chown apache. /home/webdav 5 [root@linuxprobe ~]# chmod 770 /home/webdav 6 [root@linuxprobe ~]# vi /etc/httpd/conf.d/webdav.conf 7 # create new 8 DavLockDB "/tmp/DavLock" 9 Alias /webdav /home/webdav10 <Location /webdav>11 ????DAV On12 ????SSLRequireSSL13 ????Options None14 ????AuthType Basic15 ????AuthName WebDAV16 ????AuthUserFile /etc/httpd/conf/.htpasswd17 ????<RequireAny>18 ????????Require method GET POST OPTIONS19 ????????Require valid-user20 ????</RequireAny>21 </Location>22 23 # ?添加用户:使用“-c”创建新文件(仅为初始注册添加“-c”选项)24 [root@linuxprobe ~]# htpasswd -c /etc/httpd/conf/.htpasswd wang25 New password: ????# set password26 Re-type new password:27 Adding password for user wang28 # **注意:用户wang的htpasswd已经创建过,不需要重复创建**29 [root@linuxprobe ~]# systemctl restart httpd30 31 [3] ?如果启用了SELinux,请更改以下规则。 ?32 [root@linuxprobe ~]# chcon -R -t httpd_sys_rw_content_t /home/webdav33 [root@linuxprobe ~]# semanage fcontext -a -t httpd_sys_rw_content_t /home/webdav34 35 [4] ?这是PC上的WebDAV客户端的设置(Windows 10)。36 下载“CarotDAV”,这是一个免费的WebDAV客户端,从以下网站? http://www.rei.to/carotdav_en.html ,下载后,安装并启动CarotDAV,然后显示以下屏幕,单击“文件”按钮并选择“WebDAV”。 
1 [5]在“设置名称”字段中输入任何名称,并在“URI”字段中输入[服务器名称/ webdav目录],并输入用户名和密码
1 [7]配置添加如下,点击它连接到服务器。
1 [8] waring显示如下,它的SSL证书没有安装在您的电脑上,它没有ploblem,点击“忽略”,然后去下一步。
1 [9] 到webdav目录下创建测试目录和文件2 [root@linuxprobe tmp]# cd /home/webdav/3 [root@linuxprobe webdav]# mkdir linuxprobe4 [root@linuxprobe webdav]# mkdir linuxcool5 [root@linuxprobe webdav]# touch vdevops.txt6 [root@linuxprobe webdav]# touch linuxcool.txt
 

尊重他人劳动成果,看到的原文地址:http://blog.csdn.net/wh211212/article/details/52982917

超详细 值得收藏 linux CentOS 7 配置Apache服务【转发+新增】

原文地址:https://www.cnblogs.com/sunshine-H/p/8110608.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved