实战Apache安装配置

实验环境:RHEL7.0   server1.example.com  172.25.254.1

实验内容:   1.Apache安装

                    2.Apache主配置文件

                    3.更改默认访问目录

                    4.更改默认端口

                    5.访问目录权限设置

                    6.基于用户的身份认证配置(加密网页)

                    7.更改默认访问页面

                    8.添加访问php,cgi等网页

                    9.虚拟主机

                    10.HTTPS自定义签名证书

                    11.网页重写      

1.Apache安装

1.1 安装apache软件包

[[email protected] ~]# yum install httpd httpd-manual

 1.2启动apache服务

[[email protected] ~]# systemctl start httpd;systemctl enable httpd

ln -s ‘/usr/lib/systemd/system/httpd.service‘ ‘/etc/systemd/system/multi-user.target.wants/httpd.service‘

    1.3查看监听端口

[[email protected] ~]# netstat -antple|grep httpd

tcp6       0      0 :::80                   :::*                    LISTEN      0          119746     1428/httpd

[[email protected] ~]# ss -antple |grep httpd

LISTEN     0      128                      :::80                      :::*      users:(("httpd",1433,4),("httpd",1432,4),("httpd",1431,4),("httpd",1430,4),("httpd",1429,4),("httpd",1428,4)) ino:119746 sk:ffff88003bfd6800 <->

2.Apache主配置文件:/etc/httpd/conf/httpd.conf

ServerRoot "/etc/httpd"  #用于指定Apache的运行目录

Listen 80                # 监听端口

User apache              #运行apache程序的用户和组

Group apache

ServerAdmin [email protected]    #管理员邮箱

DocumentRoot "/var/www/html"  #网页文件的存放目录

<Directory "/var/www/html">   #<Directory>语句块自定义目录权限

Require all granted

</Directory>

ErrorLog "logs/error_log"      #错误日志存放位置

AddDefaultCharset UTF-8        #默认支持的语言

IncludeOptional conf.d/*.conf  #加载其它配置文件

DirectoryIndex index.html      #默认主页名称

3.更改默认访问目录

   1)改安全上下文

[[email protected] ~]# getenforce 

Enforcing

[[email protected] ~]# mkdir -p /www/html

[[email protected] ~]# ls -ldZ /var/www/html/

drwxr-xr-x. root root system_u:object_r:httpd_sys_content_t:s0 /var/www/html/

[[email protected] ~]# ls -ldZ /www/html/

drwxr-xr-x. root root unconfined_u:object_r:default_t:s0 /www/html/

[[email protected] ~]# semanage fcontext -a -t httpd_sys_content_t ‘/www/html(/.*)?‘

[[email protected] ~]# restorecon -FvvR /www/html/

restorecon reset /www/html context unconfined_u:object_r:default_t:s0->system_u:object_r:httpd_sys_content_t:s0

[[email protected] ~]# ls -ldZ /www/html/

drwxr-xr-x. root root system_u:object_r:httpd_sys_content_t:s0 /www/html/

[[email protected] ~]# systemctl restart httpd

    2) 改配置

[[email protected] ~]# vim /etc/httpd/conf/httpd.conf 

#DocumentRoot "/var/www/html"

DocumentRoot "/www/html"

<Directory />

Require all granted

</Directory>

    3)测试

[[email protected] ~]# vim /www/html/index.html

hello,willis.

[[email protected] ~]# systemctl restart httpd.service



    4.更改默认端口

   1)查看可更改端口

[[email protected] ~]# semanage port -l |grep http      # selinux标签

http_cache_port_t              tcp      8080, 8118, 8123, 10001-10010

http_cache_port_t              udp      3130

http_port_t                    tcp      80, 81, 443, 488, 8008, 8009, 8443, 9000

pegasus_http_port_t            tcp      5988

pegasus_https_port_t           tcp      5989

   2)配置

[[email protected] ~]# vim /etc/httpd/conf/httpd.conf 

#Listen 12.34.56.78:80

Listen 8080

  3)访问

[[email protected] ~]# systemctl restart httpd.service



  5.访问目录权限

[[email protected] ~]# vim /etc/httpd/conf/httpd.conf 

DocumentRoot "/www/html"

<Directory />

Require all granted

order allow,deny          #读取顺序,后读取的覆盖前读取的

Allow from all            #允许所有用户访问

Deny from 172.25.254.2  #拒绝172.25.254.2访问

</Directory>

[[email protected] ~]# systemctl restart httpd.service


测试:172.25.254.2主机访问172.25.254.1



    6.配置基于用户的身份验证(帐号密码访问网页)

Apache无格式文件用户身份验证

在此配置中,用户账户和密码存储在本地.htpasswd文件中。处于安全原因,该文件不能

保存在网站的DocumentRoot中,而应保存在Web服务器不提供服务的一些目录中。特殊

的htpasswd命令用于在.htpasswd文件中管理用户。

[[email protected] ~]# vim /etc/httpd/conf/httpd.conf    #还原端口与目录配置

[[email protected] ~]# cd /var/www/html/

[[email protected] html]# mkdir admin

[[email protected] html]# cd admin/

[[email protected] admin]# vim index.html

hello,boy.

[[email protected] admin]# cd /etc/httpd/conf

[[email protected] conf]# ls

httpd.conf  magic

[[email protected] conf]# htpasswd -cm htpasswd admin     #用两个账户创建Apache密码文件

New password:

Re-type new password:

Adding password for user admin

[[email protected] conf]# htpasswd -m htpasswd willis

New password:

Re-type new password:

htpasswd: password verification error

[[email protected] conf]# htpasswd -cm htpasswd willis

New password:

Re-type new password:

Adding password for user willis

[[email protected] conf]# ls

htpasswd  httpd.conf  magic

[[email protected] conf]# cat htpasswd 

admin:$apr1$rEBIJilB$qGzEG6c4NYvOxg2qZLSfk/

willis:$apr1$qFoQCa7F$5IZqbqG5d5hVclspCl6R/0

[[email protected] conf]# vim /etc/httpd/conf/httpd.conf 

DocumentRoot "/var/www/html"

<Directory "/var/www/html/admin">

AuthUserfile  /etc/httpd/conf/htpasswd

AuthName   "Please input your user name and password "

AuthType basic

Require valid-user

# Require user admin

</Directory>

[[email protected] conf]# systemctl restart httpd.service 

测试:





    7.更改默认访问页面

[[email protected] ~]# cd /var/www/html/

[[email protected] html]# ls

admin  index.html

[[email protected] html]# vim  test

This is a test page.

[[email protected] html]# vim /etc/httpd/conf/httpd.conf 

<IfModule dir_module>

# DirectoryIndex index.html

DirectoryIndex  test   index.html       #先访问test,

</IfModule>

[[email protected] html]# systemctl restart httpd.service

测试:



8.可访问php,cgi等网页

1) 访问 .php  

[[email protected] html]# yum install php -y    ##安装php软件包,其中包含mod_php模块:

[[email protected] html]# pwd

/var/www/html

[[email protected] html]# vim index.php       #写php测试页

<?php

phpinfo();

?>

[[email protected] html]# systemctl restart httpd.service 

测试:



2)可访问 .cgi

通用网关接口(CGI)是网站上放置动态内容的最简单的方法。CGI脚本可用于许多目的,但是谨慎控制使用哪个CGI脚本以及允许谁添加和运行这些脚本十分重要。编写质量差的CGI脚本可能为外部攻击者提供了破坏网站及其内容安全性的途径。因此,在Web服务器级别和SELinux策略级别,都存在用于限制CGI脚本使用的设置。

[[email protected] html]# pwd

/var/www/html

[[email protected] html]# mkdir scripts

[[email protected] html]# cd scripts/

[[email protected] scripts]# vim index.cgi    #测试内容,可从文档中拷贝

#!/usr/bin/perl

print "Content-type: text/html\n\n";

print `date`;

[[email protected] scripts]# chmod +x index.cgi     #权限

[[email protected] scripts]# setenforce 0     #安全上下文

或者

semanage fcontext -l | grep httpd

/var/www/perl(/.*)?         all files   system_u:object_r:httpd_sys_script_exec_t:s0

semanage fcontext -a -t  httpd_sys_script_exec_t ‘/var/www/html/scripts(/.*)?‘

restorecon  -FvvR   /var/www/html/scripts

[[email protected] scripts]# vim /etc/httpd/conf/httpd.conf     #配置内容,可从文档中拷贝

DocumentRoot "/var/www/html"

<Directory "/var/www/html/scripts">

Options +ExecCGI

AddHandler cgi-script .cgi

</Directory>

[[email protected] scripts]# systemctl restart httpd.service

测试:http://172.25.254.1/scripts/index.cgi  







  9.虚拟主机

虚拟主机允许您从一个httpd服务器同时为多个网站提供服务。在本节中,我们将了解基于名称的虚拟主机其中多个主机名都指向同一个IP地址,但是Web服务器根据用于到达站点的主机名提供具有不同内容的不同网站。

[[email protected] scripts]# vim /etc/hosts   #浏览器访问地址的主机修改DNS配置文件

172.25.254.1   www.qq.com

172.25.254.1   news.qq.com

172.25.254.1   sport.qq.com

[[email protected] scripts]# cd /var/www/

[[email protected] www]# mkdir virtual/news/html -p

[[email protected] www]# mkdir virtual/sport/html -p

[[email protected] www]# echo new\‘s page > virtual/news/html/index.html

[[email protected] www]# echo sport\‘s page > virtual/sport/html/index.html


[[email protected] www]# vim /etc/httpd/conf.d/default.conf 

<Virtualhost _default_:80>      #定义默认虚拟主机的块

Documentroot "/var/www/html"  #在<VirtualHost>块内部,指定从中提供内容的目录。

Customlog "logs/default.log" combined

</Virtualhost>

[[email protected] www]# vim  /etc/httpd/conf.d/news.conf

<Virtualhost *:80>           #定义虚拟主机的块

Servername  news.qq.com    #指定服务器名称。在使用基于名称的虚拟主机的情况下,此处的名称必须与客户端请求完全的匹配。。

Documentroot "/var/www/virtual/news/html"   #指定从中提供内容的目录。

Customlog "logs/news.log" combined

</Virtualhost>

<Directory "/var/www/virtual/news/html">

Require  all granted         #授权

</Directory>

[[email protected] www]# vim  /etc/httpd/conf.d/sport.conf

<Virtualhost *:80>

Servername  sport.qq.com

Documentroot "/var/www/virtual/sport/html"

Customlog "logs/sport.log" combined

</Virtualhost>

<Directory "/var/www/virtual/sport/html">

Require  all granted        #授权

</Directory>



测试访问:







    10.HTTPS自定义自签名证书

如果加密的通信非常重要,而经过验证的身份不重要,管理员可以通过生成self-signed certificate来避免与认证机构进行交互所带来的复杂性。

使用genkey实用程序(通过crypto-utils软件包分发),生成自签名证书及其关联的私钥。为了简化起见,genkey将在“正确”的位置(/etc/pki/tls目录)创建证书及其关联的密钥。相应地,必须以授权用户(root)身份运行该实用程序。

[[email protected] www]# yum install crypto-utils mod_ssl -y   #生成自签名证书crypto-utils软件包

[[email protected] www]# genkey Apache.example.com    #调用genkey,同时为生成的文件指定唯一名称

1)记录生成的证书(Apach.example.com .crt)和关联的私钥(Apach.example.com .key)的位置

2) 继续使用对话框,并选择合适的密钥大小。(默认的2048位密钥为推荐值)

3) 在生成随机数时比较慢,敲键盘和移动鼠标可以加速

4) 拒绝向认证机构(CA)发送证书请求(CSR)

5) 拒绝加密私钥

6) 为服务器提供合适的身份。Common Name必须与服务器的主机全名完全匹配。



[[email protected] www]# vim /etc/httpd/conf.d/ssl.conf

SSLCertificateFile /etc/pki/tls/certs/Apache.example.com.crt

SSLCertificateKeyFile /etc/pki/tls/private/Apache.example.com.key

[[email protected] www]# systemctl restart httpd.service

如要进行确认,请使用https协议(https://serverX.example.com)通过Web客户端(如Firefox)访问Web服务器。

Web客户端可能会发出它不认可证书发行者的警告。这种情况适用自签名证书。要求Web客户端绕过证书认证。(对于Firefox,请选择“I Understand the Risks” [我了解风险]、“Add Exception” [添加例外]和“Confirm Security Exception”[确认安全例外]。)

测试:





11.网页重写

[[email protected] www]# vim /etc/hosts    #浏览器访问地址的主机修改DNS配置文件

172.25.254.1   login.qq.com

[[email protected] www]# pwd

/var/www

[[email protected] www]# mkdir virtual/login/html -p

[[email protected] www]# echo login\‘s page > virtual/login/html/index.html

[[email protected] www]# vim /etc/httpd/conf.d/login.conf

<Virtualhost *:443>

Servername  login.qq.com

Documentroot "/var/www/virtual/login/html"

Customlog "logs/login.log" combined

SSLEngine  on

SSLCertificateFile /etc/pki/tls/certs/Apach.example.com.crt

SSLCertificateKeyFile /etc/pki/tls/private/Apach.example.com.key

</Virtualhost>

<Directory "/var/www/virtual/login/html">

Require  all granted        #授权

</Directory>

<Virtualhost *:80>

ServerName login.qq.com

RewriteEngine on

RewriteRule ^(/.*)$ https://%{HTTP_HOST}$1 [redirect=301]

</Virtualhost>

测试:

时间: 2024-08-08 01:29:01

实战Apache安装配置的相关文章

Apache安装配置步骤

注释:这里以Linux 红帽商业版为例~~~~~~~纯手打啊 Apache安装配置步骤 准备:关闭其他虚拟设备 #/etc/init.d/libvirtd stop #/etc/init.d/xend stop #chkconfig libvirtd off #chkconfig xend off 一.安装步骤 1.把光驱载入到系统当中: 2.安装 二.配置步骤 1.配置IP地址: 2.进入/etc/httpd/conf目录,使用cp命令备份配置文件(httpd.conf),并用vim打开 3.

Mac 使用自带php和Apache 安装配置Xdebug 开启本地调试模式

Mac 安装配置php xdebug 本地调试 0.原理图 https://paper.seebug.org/308/ 测试demo构建方法 新建空白项目,目录选择Apache默认项目目录 1.下载xdebug https://xdebug.org/files/xdebug-2.9.0.tgz 具体自己的版本要根据??的方法得出 2.使用官方检测指导工具 https://xdebug.org/wizard 将phpinfo()打印内容的网页源码复制到框里,点击检测,会得到提示 然后根据提示进行操

CentOS Apache 安装 配置 启动

下载Apache安装包  httpd-2.4.23.tar.gz 下载地址:http://apache.fayea.com/httpd/ Apache 安装要求 必须安装APR.APR-Util.PCRE,gcc-c++等包 编译命令:(除了指定Apache的安装目录外,还要安装apr.apr-util.pcre,并指定参数) [[email protected] software]# tar -zxvf httpd-2.4.23.tar.gz [[email protected] softwa

LAMP_02_WIN下Apache安装配置

1.下载http://httpd.apache.org/download 2.配置 下载完解压后有readme,首先进行阅读其中的VC运行库必须安装,否则会出现各种奇葩问题用命令行安装服务 发现报错,先删除服务进入相应目录修改文件,35行指示路径 需要注意这个配置文件里面很多路径均为该路径,为了方便,将apache放到D盘根目录下安装 重新安装 启动服务 浏览器打开localhost:80 后续可以在服务中开启关闭apache服务 或者通过 bin目录下的ApacheMonitor.exe来开启

apache 安装配置 以及php-fpm结合apache配置

解决依赖关系 yum -y install pcre-devel 下载apr  apr-util:http://apr.apache.org/download.cgi 编译安装apr:  ./configure --prefix=/usr/local/apr make && make install 编译安装apr-util: ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr  make &&

apache安装配置

官方网站:apache.orghttp://www.taobao.com/index.html为例1.http请求的内容    请求行        请求方法:get(获取):请求方法,其他常见方法:post(提交表单.单选.端选...).put(提交评价).delete(删除服务器上的某个内容(商家))        index.html:请求的文件        http/1.1版:请求的协议    请求头        host:请求的主机(源IP)        accept:是否接受请

apache 安装配置 (centos)

1. 使用yum包安装Apache软件 [[email protected] ~]# yum -y install httpd* [[email protected] ~]# rpm -qa | grep httpd --查看安装的http包 httpd-manual-2.4.6-67.el7.centos.6.noarch httpd-tools-2.4.6-67.el7.centos.6.x86_64 httpd-2.4.6-67.el7.centos.6.x86_64 httpd-deve

apache 安装及配置

近期想用apache运行网站,在网上查询windows 版本的中文说明文档有特别少,所以将学习到的在这里做个笔记,以便日后学习以及大家相互交流. 相关文档:http://httpd.apache.org/docs/2.4/ 指令:http://httpd.apache.org/docs/2.4/mod/core.html 首先在官网下载apache :http://www.apachelounge.com/download/,其中有两个版本,根据你的版本来下载相应的版本,然后解压放在你的安装目录

Linux Apache php MySQL 安装配置(Centos 6.4 yum安装)

一.yum准备 1.Yum(全称为 Yellow dog Updater, Modified)是一个在Fedora和RedHat以及CentOS中的Shell前端软件包管理器. 基于RPM包管理,能够从指定的服务器自动下载RPM包并且安装,可以自动处理依赖性关系,并且一次安装所有依赖的软件包,无须繁琐地一次次下载.安装. 2.在安装软件时报’Couldn't resolve host 'mirrorlist.centos.org‘ 解决:打开文件/etc/resolv.conf在其中添加: (添