linux——Apache服务配置及php的使用,linuxapache


Apache

一、服务简介

Apache是世界使用排名第一的Web服务器软件。它可以运行在几乎所有广泛使用的计算机平台上,由于其跨平台和安全性被广泛使用,是最流行的Web服务器端软件之一。

二、服务配置
[root@foundation70 Desktop]# yum install httpd -y
[root@foundation70 Desktop]# yum install httpd-manual -y
[root@foundation70 Desktop]# systemctl start httpd
[root@foundation70 Desktop]# systemctl enable httpd
[root@foundation70 Desktop]# systemctl stop firewalld.service 
[root@foundation70 Desktop]# systemctl disable firewalld.service 
在浏览器上测试

http://主机ip

http://主机ip/manual

三、apache的基础信息
主配置目录: /etc/httpd/conf/
主配置文件: /etc/httpd/conf/httpd.conf/
子配置目录: /etc/httpd/conf.d/
子配置文件: /etc/httpd/conf.d/*.conf
默认发布目录:/var/www/html
默认端口:    80

[root@foundation70 Desktop]# ss -anutlpe |grep httpd   #查看端口号

默认安全上下文 httpd_sys_content_t
程序开启默认用户: apache
apache日志:   /etc/httpd/logs/*
三apache服务的管理
1.更改默认端口
[root@localhost Desktop]# vim /etc/httpd/conf/httpd.conf 
42 Listen 8080
[root@localhost Desktop]# systemctl restart httpd

如下图:直接以默认端口访问无效,添加上设置的端口号即可以成功访问

2.默认发布文件以及修改
定义:默认发布文件就是访问 apache服务的时候没有指定名称就能访问的文件
[root@localhost Desktop]# cd /var/www/html/
[root@localhost html]# ls
[root@localhost html]# touch index.html     #配置文件默认发布文件就是index.html

[root@localhost html]# echo hello world > index.html

修改默认发布文件
[root@localhost html]# vim /etc/httpd/conf/httpd.conf 
164  DirectoryIndex index.html test.html #按顺序访问,当index.html文件不在时访问test.html
[root@localhost html]# touch test.html
[root@localhost html]# echo hello freya > test.html
[root@localhost html]# mv index.html /mnt   #把优先级高的index.html文件移除默认发布目录
[root@localhost html]# systemctl restart httpd

如下图,默认发布文件访问了test.html

3.修改默认发布目录
[root@localhost html]# mkdir /freya  #新建默认发布目录
[root@localhost html]# vim /freya/index.html 
<h1>new directory </h1>  #该发布目录下的默认发布文件(apache服务支持html)
[root@localhost html]# vim /etc/httpd/conf/httpd.conf 
119 #DocumentRoot "/var/www/html"  #注释原有的默认发布目录
120 DocumentRoot "/freya"                  #添加新的默认目录
121 <Directory  "/freya"> 
122       Require all granted
123 </Directory>                                     #为该目录授权
[root@localhost html]# systemctl restart httpd  
[root@localhost html]# semanage fcontext -a -t httpd_sys_content_t '/freya(/.*)?'
[root@localhost html]# storecon -RvvF /freya  #为该新目录更改安全上下文或者直接关闭selinux “[root@localhost html]# setenforce  0"

如下图:访问apache服务默认发布目录更改,显示的是新目录下的默认发布文件

4.apache的虚拟主机
(1)作用:从 web 概念上来讲,访问一个 web 服务,其实就是访问某个主机上的某个端口,这个端口默认是 80,通常需要访问不同的域名,不同的端口,以便于实现对不同网站的访问。这个时候就需要配置虚拟主机。也就是做到了一个服务器一个 apache 提供多个网站服务。
(2)建立一个站点为freya.test.com
[root@localhost ~]# vim /etc/httpd/conf.d/adefault.conf

#在子配置目录下添加一个叫做adefault的配置文件,该配置文件的作用是令非指定的站点跳转到/var/www/html发布目录下。如下图

[root@localhost ~]# mkdir /var/www/virtual/freya.test.com/html -p #创建该站点目录
[root@localhost ~]# vim /var/www/virtual/freya.test.com/html/index.html 
freya.test.com's page
#为该站点添加默认发布文件的内容
[root@localhost ~]# vim /etc/httpd/conf.d/freya.conf 
#在子配置目录下添加一个叫做freya的配置文件,该配置文件的作用是令非指定的站点跳转到/var/www/virtual/freya.test.com/html发布目录下,访问该目录下的默认发布文件index.html
如下图combined表示四种日志类型。

[root@localhost html]# systemctl restart httpd
[root@localhost html]# vim /etc/hosts               #为要利用的浏览器添加域名解析
172.25.254.112 freya.test.com   linux.test.com

测试结果:

非指定域名默认会跳转到apache的发布目录下

指定域名会跳转到指定发布目录下,访问该目录下的默认发布文件

5.apache内部的访问控制
(1)针对主机的访问控制
[root@localhost ~]# mkdir /var/www/html/test
[root@localhost ~]# vim /var/www/html/test/index.html
<h1>test'page</h1>
[root@localhost ~]# vim /etc/httpd/conf.d/adefault.conf 
如下图,限制了ip为172.25.254.70的主机访问/var/www/html/test目录的权力,Order allow,deny表示先允许所有主机,再限定指定主机。

测试

(2)用户访问方式的控制(为了安全起见,我们需要用户在访问某一页面的时候需要认证)
[root@localhost ~]# mkdir /var/www/html/admin
[root@localhost ~]# vim /var/www/html/admin/index.html
<h1>admin'page</h1>                       #设置用户登陆时访问的页面
[root@localhost ~]# htpasswd -cm /etc/httpd/userpass admin  #为某个用户设定密码
New password:                                   #添加密码
Re-type new password: 
Adding password for user admin
[root@localhost ~]# vim /etc/httpd/conf.d/adefault.conf 

添加下图内容

[root@localhost ~]# systemctl restart httpd

测试

6.apache支持的语言
(1)html apache默认支持的语言,可以在apache的默认发布文件中直接用该语言
(2)php
[root@localhost html]# yum install php -y

[root@localhost html]# vim /var/www/html/index.php #在默认发布目录上添加一个显示php信息的测试文件。

[root@localhost html]# systemctl restart httpd

测试结果:

(3)cgi
[root@localhost ~]# mkdir /var/www/html/cgi
[root@localhost ~]# vim /var/www/html/cgi/index.cgi
[root@localhost ~]# chmod +x /var/www/html/cgi/index.cgi

执行以下该脚本,检测是否正确

[root@localhost ~]# vim /etc/httpd/conf.d/adefault.conf

[root@localhost ~]# systemctl restart httpd

测试结果:

7.https 
(1)https定义和作用:是以安全为目标的HTTP通道,简单讲是HTTP的安全版。即HTTP下加入SSL层,HTTPS的安全基础是SSL,因此加密的详细内容就需要SSL。HTTPS存在不同于HTTP的默认端口及一个加密/身份验证层(在HTTP与TCP之间)。
(2)https的配置
先安装如下如下两个软件
[root@localhost ~]# yum install mod_ssl -y
[root@localhost ~]# yum install crypto-utils -y
配置钥匙和证书 genkey 域名
例如 genkey www.freya.com

具体步骤如下图

 

当钥匙和证书制作完成后,会在shell中显示证书和钥匙的目录如下图圈注,将证书和钥匙的目录添加到指定配置文件/etc/httpd/conf.d/ssl.conf

[root@localhost ~]# vim /etc/httpd/conf.d/ssl.conf
101 SSLCertificateFile /etc/pki/tls/certs/www.freya.com.0.csr
108 SSLCertificateKeyFile /etc/pki/tls/private/www.freya.com.key

(3)设定https虚拟主机并设定网页重写
[root@localhost ~]# mkdir /var/www/virtual/login.freya.com/html -p
[root@localhost ~]# vim /var/www/virtual/login.freya.com/html/index.html
<h1>Welcome freya</h1>
[root@localhost ~]# vim /etc/httpd/conf.d/login.conf
[root@localhost ~]# systemctl restart httpd

相关内容

    暂无相关文章