Apache服务部署

安装Apache服务程序

[root@localhost ~]# yum -y install httpd

 

已加载插件:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
正在解决依赖关系
--> 正在检查事务
---> 软件包 httpd.x86_64.0.2.4.6-67.el7.centos 将被 安装
--> 解决依赖关系完成

依赖关系解决

================================================================================
 Package       架构           版本                        源               大小
================================================================================
正在安装:
 httpd         x86_64         2.4.6-67.el7.centos         CentOS7         2.7 M

事务概要
================================================================================
安装  1 软件包

总下载量:2.7 M
安装大小:9.4 M
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
警告:RPM 数据库已被非 yum 程序修改。
  正在安装    : httpd-2.4.6-67.el7.centos.x86_64                            1/1 
  验证中      : httpd-2.4.6-67.el7.centos.x86_64                            1/1 

已安装:
  httpd.x86_64 0:2.4.6-67.el7.centos                                            

完毕!

开启httpd服务程序并将其加入到开机启动项中

 

[root@localhost ~]# systemctl start httpd       //开启httpd服务程序

[root@localhost ~]# systemctl enable httpd    //httpd加入到开机启动项中

Created symlink from /etc/systemd/system/multi-user.target.wants/httpd.service to /usr/lib/systemd/system/httpd.service.//表示已经加入开机启动项中

 

在浏览器的地址栏中输入http://127.0.0.1并按回车键,就可以看到用于提供Web服务的httpd服务程序的默认页面了,如图

出现这个图面表示Apache服务程序启动成功

 

Linux系统中的httpd的配置文件

Linux系统中的httpd的配置文件
服务目录/etc/httpd
主配置文件/etc/httpd/conf/httpd.conf
网站数据目录/var/www/html
访问日志/var/log/httpd/access_log
错误日志/var/log/httpd/error_log

 

 

在httpd服务程序主配置文件中,最为常用的参数以及用途描述:

ServerRoot服务目录
ServerAdmin管理员邮箱
User运行服务的用户
Group运行服务的用户组
ServerName网站服务器的域名
DocumentRoot网站数据目录
Listen监听的IP地址与端口号
DirectoryIndex默认的索引页页面
ErrorLog错误日志文件
CustomLog访问日志文件
Timeout	网页超时时间

 

从上面的参数可知DocumentRoot参数用于定义网站数据的保存路径,其参数的默认值是把网站数据存放到/var/www/html目录中;而当前网站普遍的首页面名称是index.html,因此可以向/var/www/html目录中写入一个文件,替换掉httpd服务程序的默认首页面,该操作会立即生效。如在执行下述操作之后,再在Firefox浏览器中刷新httpd服务程序,可以看到该程序的首页面内容已经发生了改变

 

[root@localhost ~]# echo "Welcome To httpd" > /var/www/html/index.html

[root@localhost ~]# firefox

 

 

第1步:建立网站数据的保存目录,并创建首页文件。

[root@localhost ~]# mkdir /home/wwwroot

 [root@localhost~]# echo "hello Apache" > /home/wwwroot/index.html

 

第2步:打开httpd服务程序的主配置文件,将约第119行用于定义网站数据保存路径的参数DocumentRoot修改为/home/wwwroot,同时还需要将约第124行用于定义目录权限的参数Directory后面的路径也修改为/home/wwwroot。配置文件修改完毕后即可保存并退出。注:在vim模式下输入119gg可快速切换到119行

………………省略部分输出信息………………
115 # DocumentRoot: The directory out of which you will serve your
116 # documents. By default, all requests are taken from this directory, bu t
117 # symbolic links and aliases may be used to point to other locations.
118 #
119 DocumentRoot "/home/wwwroot"
120 
121 #
122 # Relax access to content within /var/www.
123 #
124 <Directory "/home/wwwroot">
125 AllowOverride None
126 # Allow open access:
127 Require all granted
128 </Directory>

root@localhost ~]# systemctl restart httpd.service    //重启Apache服务

运行浏览器如图所示则成功

 

个人用户主页功能

 

第1步:在httpd服务程序中,默认没有开启个人用户主页功能。为此,我们需要编辑下面的配置文件,然后在第17行的UserDir disabled参数前面加上井号(#),表示让httpd服务程序开启个人用户主页功能;同时再把第24行的UserDir public_html参数前面的井号(#)去掉(UserDir参数表示网站数据在用户家目录中的保存目录名称,即public_html目录)。最后,在修改完毕后记得保存。

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

 
………………省略部分输出信息………………

15 # permissions). 16 # 17 # UserDir disabled 18 19 # 20 # To enable requests to /~user/ to serve the user's public_html 21 # directory, remove the "UserDir disabled" line above, and uncomment 22 # the following line instead: 23 # 24 UserDir public_html

………………省略部分输出信息………………

第2步:在用户家目录中建立用于保存网站数据的目录及首页面文件。另外,还需要把家目录的权限修改为755,保证其他人也有权限读取里面的内容。

[root@localhost ~]# su - chenjin  //切换到chenjin用户
上一次登录:五 12月  8 01:11:38 CST 2017:0 上
[chenjin@localhost ~]$ mkdir public_html     //创建public_html用户家目录
[chenjin@localhost ~]$ echo "This is chenjin web" > public_html/index.html    //写入网站数据到public_html/index.html

[chenjin@localhost ~]$ chmod -Rf 755 /home/chenjin   //把家目录的权限修改为755

[chenjin@localhost ~]$ systemctl restart httpd.service   //重启Apache服务

在浏览器的地址栏中输入网址,其格式为“网址/~用户名”(其中的波浪号是必需的,而且网址、波浪号、用户名之间没有空格如:http://127.0.0.1/~chenjin/)

 

如果网站的拥有者并不希望直接将网页内容显示出来,只想让通过身份验证的用户访客看到里面的内容,这时就可以在网站中添加口令功能

第1步:先使用htpasswd命令生成密码数据库。-c参数表示第一次生成;后面再分别添加密码数据库的存放文件,以及验证要用到的用户名称(该用户不必是系统中已有的本地账户)如果出现htpasswd: cannot create file /etc/httpd/passwd则说明用户权限不够需要切换到其他用户比如:root

[root@localhost ~]# htpasswd -c /etc/httpd/passwd chenjin
New password: 
Re-type new password: 

Adding password for user chenjin

 

第2步:编辑个人用户主页功能的配置文件。把第31~35行的参数信息修改成下列内容,然后保存并退出配置文件,重启httpd服务程序即可生效。

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

30 #
31 <Directory "/home/*/public_html">
32 AllowOverride all
#刚刚生成出来的密码验证文件保存路径
33 authuserfile "/etc/httpd/passwd"
#当用户尝试访问个人用户网站时的提示信息
34 authname "My web"
35 authtype basic
#用户进行账户密码登录时需要验证的用户名称
36 require user chenjin
37 </Directory>

[root@localhost ~]# systemctl restart httpd

此后当用户再想访问某个用户的个人网站时,就必须要输入账户和密码才能正常访问

 

基于IP地址地址访问

[root@localhost ~]# mkdir -p /home/wwwroot/64
[root@localhost ~]#  echo "64" > /home/wwwroot/64/index.html

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

[root@linuxprobe ~]# vim /etc/httpd/conf/httpd.conf
………………省略部分输出信息………………
113 <VirtualHost 192.168.1.64>
114 DocumentRoot /home/wwwroot/64
115 ServerName www.chenjin.com
116 <Directory /home/wwwroot/64 >
117 AllowOverride None
118 Require all granted
119 </Directory>
120 </VirtualHost>

………………省略部分输出信息………………


[root@localhost ~]# systemctl restart httpd

 

 

 

 基于主机域名

 

第1步:手工定义IP地址与域名之间对应关系的配置文件,保存并退出后会立即生效。可以通过分别ping这些域名来验证域名是否已经成功解析为IP地址。

[root@localhost ~]# vim /etc/hosts
 

127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.1.64 www.chenjin.com


[root@localhost ~]# ping  www.chenjin.com
PING www.chenjin.com (192.168.1.64) 56(84) bytes of data.
64 bytes from www.chenjin.com (192.168.1.64): icmp_seq=1 ttl=64 time=0.085 ms
64 bytes from www.chenjin.com (192.168.1.64): icmp_seq=2 ttl=64 time=0.085 ms
64 bytes from www.chenjin.com (192.168.1.64): icmp_seq=3 ttl=64 time=0.085 ms

 

 

 

 

第2步:在/home/wwwroot中创建用于保存网站数据的目录,并向其中写入网站的首页文件。首页文件中应有明确内容的信息,方便检查效果。

[root@localhost ~]# mkdir -p /home/wwwroot/www

[root@localhost ~]# echo "chenjin.com" > /home/wwwroot/www/index.html

 

第3步:在httpd服务的配置文件中大约113行处开始,写入基于主机名的虚拟主机网站参数,然后保存并退出。记得需要重启httpd服务,这些配置才生效。

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

………………省略部分输出信息………………
113 <VirtualHost 192.168.1.64>
114 DocumentRoot "/home/wwwroot/www"
115 ServerName "www.chenjin.com"
116 <Directory "/home/wwwroot/www">
117 AllowOverride None
118 Require all granted
119 </directory> 
120 </VirtualHost>

………………省略部分输出信息………………

[root@localhost ~]# systemctl restart httpd

基于端口号

 

第1步:分别在/home/wwwroot中创建用于保存网站数据的目录,并向其中写入网站的首页文件。首页文件中应有网站内容的信息,方便能更直观地检查效果

[root@localhost ~]# mkdir -p /home/wwwroot/8080

[root@localhost wwwroot]# echo "8080" > /home/wwwroot/8080/index.html

 

第2步:在httpd服务配置文件的第43行添加用于监听8080端口的参数。在httpd服务的配置文件中大约113行处开始,追加写入基于端口号的虚拟主机网站参数,然后保存并退出。记得需要重启httpd服务,这些配置才生效

[root@localhost wwwroot]# vim /etc/httpd/conf/httpd.conf

………………省略部分输出信息……………… 
 33 #
 34 # Listen: Allows you to bind Apache to specific IP addresses and/or
 35 # ports, instead of the default. See also the <VirtualHost>
 36 # directive.
 37 #
 38 # Change this to Listen on specific IP addresses as shown below to 
 39 # prevent Apache from glomming onto all bound IP addresses.
 40 #
 41 #Listen 12.34.56.78:80
 42 Listen 80
 43 Listen 8080
 
………………省略部分输出信息……………… 

………………省略部分输出信息……………… 
113 <VirtualHost 192.168.1.64:8080>
114 DocumentRoot "/home/wwwroot/8080"
115 ServerName www.chenjin.com
116 <Directory "/home/wwwroot/8080">
117 AllowOverride None
118 Require all granted
119 </Directory> 
120 </VirtualHost>

 

 root@localhost wwwroot]# systemctl restart httpd

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值