使用Apache搭建Web网站服务器

一、Apache服务器概述-安装
1、web服务器概述
web服务器也简称WWW(world wide web 万维网)服务器,主要功能是提供网上信息浏览服务

2、什么是apache?
Apache HTTPD Server 简称 Apache,是 Apache 软件基金会的一个开源的网页服务器, 可以在大多数计算机操作系统中运行,由于其多平台和安全性被广泛使用,是最流行的 Web 服务器端软件之一。它快速、可靠并且可通过简单的 API 扩展,将 Perl/Python 等解释器编 译到服务器中!Apache HTTP 服务器是一个模块化的服务器,各个功能使用模块化进行插拔! 目前支持 Windows,Linux,Unix 等平台!
Apache 软件基金会(也就是 Apache Software Foundation,简称为 ASF),是专门为运 作一个开源软件项目的 Apache 的团体提供支持的非盈利性组织,这个开源软件项目就是 Apache 项目!那么我们的 HTTPD 也只是 Apache 的开源项目之一!
主要的开源项目:HTTP Server,Ant,DB,iBATIS,Jakarta,Logging,Maven,Struts, Tomcat,Tapestry,Hadoop 等等。只是最有名的是 HTTP Server,所以现在所说的 Apache 已 经就是 HTTPD Server 的代号了! 我们还见的比较多的是 Tomcat,Hadoop 等项目
官方网站:http://www.apache.org/ httpd:http://httpd.apache.org/

3、什么是mysql?
MySQL 是一个关系型数据库管理系统,由瑞典 MySQL AB 公司开发,目前属于 Oracle 旗下公司,他也是最流行的关系型数据库管理系统!特别是在 WEB 应用方面!
关系型数据库:它通过数据、关系和对数据的约束三者组成的数据模型来存放和管理数

官方网站:http://www.mysql.com/

4、什么是PHP?
PHP(外文名:PHP: Hypertext Preprocessor,中文名:“超文本预处理器”)是一种通 用开源脚本语言。语法吸收了 C 语言、Java 和 Perl 的特点,利于学习,使用广泛,主要适 用于 Web 开发领域。PHP 独特的语法混合了 C、Java、Perl 以及 PHP 自创的语法。它可以 比 CGI 或者 Perl 更快速地执行动态网页。用 PHP 做出的动态页面与其他的编程语言相比, PHP 是将程序嵌入到 HTML(标准通用标记语言下的一个应用)文档中去执行,执行效率比 完全生成 HTML 标记的 CGI 要高许多;PHP 还可以执行编译后代码,编译可以达到加密和 优化代码运行,使代码运行更快。
简单的说,PHP 就是一个脚本解释器! 官方网站:http://php.net/
PHP 服务器安装好之后,升级切记注意,不要盲目升级!因为它会废弃很多低效的语法!

5、web服务器的工作模式和端口
工作模式是:B/S 模式
工作端口是:
正常端口:80/http
SSL 端口:443/https

6、安装服务器端httpd,并启动httpd,设置为开机自启动
[root@xuegod130 ~]# yum -y install httpd
[root@xuegod130 ~]# systemctl start httpd
[root@xuegod130 ~]# systemctl enable httpd
Created symlink from /etc/systemd/system/multi-user.target.wants/httpd.service to /usr/lib/systemd/system/httpd.service.
[root@xuegod130 ~]# ps -ef | grep httpd
root 3003 1 0 22:48 ? 00:00:00 /usr/sbin/httpd -DFOREGROUND
apache 3004 3003 0 22:48 ? 00:00:00 /usr/sbin/httpd -DFOREGROUND
apache 3005 3003 0 22:48 ? 00:00:00 /usr/sbin/httpd -DFOREGROUND
apache 3006 3003 0 22:48 ? 00:00:00 /usr/sbin/httpd -DFOREGROUND
apache 3007 3003 0 22:48 ? 00:00:00 /usr/sbin/httpd -DFOREGROUND
apache 3008 3003 0 22:48 ? 00:00:00 /usr/sbin/httpd -DFOREGROUND
[root@xuegod130 ~]# ss -antupl | grep 80
tcp LISTEN 0 128 :::80 ::? users:((“httpd”,pid=3008,fd=4),(“httpd”,pid=3007,fd=4),(“httpd”,pid=3006,fd=4),(“httpd”,pid=3005,fd=4),(“httpd”,pid=3004,fd=4),(“httpd”,pid=3003,fd=4))

httpd安装包说明:
[root@xuegod130 ~]# ls /mnt/Packages/httpd-*
/mnt/Packages/httpd-2.4.6-80.el7.centos.x86_64.rpm #apache服务主程序包,服务端必须安装
/mnt/Packages/httpd-manual-2.4.6-80.el7.centos.noarch.rpm#apache手册文档
/mnt/Packages/httpd-devel-2.4.6-80.el7.centos.x86_64.rpm#apache开发程序包
/mnt/Packages/httpd-tools-2.4.6-80.el7.centos.x86_64.rpm#apache相关工具包

7、在客户端安装字符界面下的浏览器
[root@xuegod140 ~]# yum -y install elinks

8、安装完成httpd包之后,启动服务,直接在浏览器打开,如下界面
在这里插入图片描述
字符界面浏览器:
[root@xuegod140 ~]# elinks 192.168.1.130
在这里插入图片描述

9、apache配置文件目录
apache的配置文件:
[root@xuegod130 ~]# ll /etc/httpd/conf/httpd.conf
-rw-r–r-- 1 root root 11753 4月 24 21:44 /etc/httpd/conf/httpd.conf
apache的配置文件目录:
[root@xuegod130 ~]# ll /etc/httpd/conf/
总用量 28
-rw-r–r-- 1 root root 11753 4月 24 21:44 httpd.conf
-rw-r–r-- 1 root root 13077 4月 24 21:46 magic
apache的默认工作目录:
[root@xuegod130 ~]# ll /var/www/
总用量 0
drwxr-xr-x 2 root root 6 4月 24 21:46 cgi-bin
drwxr-xr-x 2 root root 6 4月 24 21:46 html

10、apache主配置文件介绍
[root@xuegod130 ~]# vim /etc/httpd/conf/httpd.conf
ServerRoot “/etc/httpd” #httpd服务的根目录
Listen 80 #监听端口,默认本地 IP,如果指定 ip 写上 IP:80
Include conf.modules.d/.conf #当前目录下conf.modules.d 这个目录下所有conf文件都生效
Options Indexes FollowSymLinks #Options Indexes 目录浏览FollowSymLinks 用连接浏览
AllowOverride None #设置为 none,忽略.htaccess
Include conf.d/
.conf #conf.d 里面的 conf 文件也属有效配置文件
User apache #运行以哪个身份运行
Group apache #运行以哪个组的身份运行
ServerAdmin root@localhost #管理员邮箱
DocumentRoot “/var/www/html” #默认的主目录,如果改动要改动两处,Directory
<Directory “/var/www/html”>
Options Indexes FollowSymLinks AllowOverride None

LogLevel warn #日志等级
AddDefaultCharset UTF-8 #支持的语言,默认编码
#配置文件的最后是虚拟主机的字段,其中你大部分字段做个了解即可,用到的时候去查 即可

11、yum方式搭建LAMP环境
LAMP=Linux(存在)+Apache(HTTPD 已安装)+Mysql+PHP
#centos6
[root@xuegod100 ~]# yum -y install httpd mysql mysql-server php php-mysql
#centos7
[root@xuegod130 ~]# yum -y install httpd mariadb mariadb-server php php-mysql

1)启动maridb数据库,设置开机启动
[root@xuegod130 ~]# systemctl start mariadb
[root@xuegod130 ~]# systemctl enable mariadb
2)登录数据库测试
[root@xuegod130 ~]# mysql
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 3
Server version: 5.5.60-MariaDB MariaDB Server

Copyright © 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type ‘help;’ or ‘\h’ for help. Type ‘\c’ to clear the current input statement.

MariaDB [(none)]> show databases;
±-------------------+
| Database |
±-------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
±-------------------+
4 rows in set (0.00 sec)

MariaDB [(none)]> exit
Bye

3)测试apache是否支持php
[root@xuegod130 ~]# vim /var/www/html/index.php
[root@xuegod130 ~]# cat !$
cat /var/www/html/index.php

<?php phpinfo(); ?>

[root@xuegod130 ~]# systemctl restart httpd
类似于PHP 探针:

4)浏览器登录,显示如下界面,说明支持php
在这里插入图片描述
扩展:探针:一个PHP编写的文件,可以实时查看服务器硬盘资源、内存占用、网卡流量、系统负载、服务器时间等信息,定时刷新一次。以及包括服务器IP地址,Web服务器环境监测,php等信息。

二、配置web服务器-搭建LAMP环境
1、搭建一台测试web服务器
案例:
部门内部搭建一台WEB服务器,采用的IP地址和端口为192.168.1.63:80,首页采用index.html
文件。管理员E-mail地址为 cat@xuegod.cn,网页的编码类型采用UTF-8,所有网站资源都存放在/var/www/html目录下,并将Apache的配置文件根目录设置为/etc/httpd目录。
要求分析:
设置Apache 的根目录为/etc/httpd #默认
设置httpd 监听端口80 #默认
设置管理员E-mail地址为 kill@xuegod.cn #需要修改
设置WEB 服务器的主机名和监听端口为192.168.1.63:80 #默认80端口
设置Apache 文档目录为/var/www/html #默认
设置主页文件为 index.html #增加这个网页文件
设置服务器的默认编码为UTF-8

1)修改配置(修改前,备份一份)
[root@xuegod130 ~]# cd /etc/httpd/conf
[root@xuegod130 conf]# cp httpd.conf httpd.conf.bak
[root@xuegod130 conf]# vim httpd.conf
31 ServerRoot “/etc/httpd” #apache的根目录
32 Timeout 60 #添加此项,超时时间
42 Listen 80 #监听端口号
86 ServerAdmin kill@xuegod.cn #修改管理邮箱地址
95 ServerName 192.168.1.130:80 #服务器主机名
119 DocumentRoot “/var/www/html” #网站页面根目录
144 Options Indexes FollowSymLinks #当一个目录没有默认首页时,允许显示此目录列表,一般安全期间,需要注释此项,避免目录下的其它资料泄露
164 DirectoryIndex index.html index.php #修改默认首页
316 AddDefaultCharset UTF-8 #设置服务器的默认编码为:UTF-8

2)修改完后,保存,重启httpd服务
[root@xuegod130 conf]# !sys
systemctl restart httpd

3)取消apache默认欢迎页(登录初始页面)
[root@xuegod130 conf]# vim /etc/httpd/conf.d/welcome.conf
8 #<LocationMatch “^/+$”>
9 # Options -Indexes
10 # ErrorDocument 403 /.noindex.html
11 #
将8-9行内容全部注释掉
重启httpd服务:
[root@xuegod130 conf]# systemctl restart httpd
4)创建首页:
[root@xuegod130 conf]# cd /var/www/html/
[root@xuegod130 html]# echo “welcome to www.xuegod.cn” > index.html
在这里插入图片描述
[root@xuegod130 html]# echo “welcome to www.xuegod.cn index.php” > index.php
在这里插入图片描述
2、修改web网站根目录-配置别名-虚拟目录
修改网站默认根目录,并且设置访问权限,只有在允许的范围内可以访问
1)修改网站默认根目录为/var/www/html/bbs
119 DocumentRoot “/var/www/html/bbs”

2)允许所有人访问/var/www/html/bbs目录
131 <Directory “/var/www/html/bbs”> #子目录会继承这个目录的属性
144 Options Indexes FollowSymLinks #目录浏览
151 AllowOverride None #可以用连接
156 Require all granted #默认表示允许所有人访问,修改设置如下内容(需要注释这行内容)
156 # Require all granted #需要注释允许所有人访问,下面的才生效
157
158 Require ip 192.168.1.104 #允许104访问
159 Require not ip 192.168.1.140 #不允许140访问
160

解释:
Apache2.4的访问控制:
Require all granted #允许所有
Require all denied #拒绝所有
Require method http-method [http-method] … #允许,特定的HTTP方法
Require user userid [ userid ] … #允许,特定用户
Require group group-name [group-name] … #允许,特定用户组
Require valid-user #允许,有效用户
Require ip 192.168.1.0/24 #允许网段
Require ip 192.168.1.1 #允许特定IP
Require not ip 192.168.1.100 #不允许特定IP
Require not ip 192.168.1.0/24 #不允许网段

3)重启httpd服务
[root@xuegod130 ~]# systemctl restart httpd

4)创建目录测试首页
[root@xuegod130 ~]# mkdir /var/www/html/bbs
[root@xuegod130 ~]# ll /var/www/html/bbs -d
drwxr-xr-x 2 root root 6 5月 20 23:48 /var/www/html/bbs
[root@xuegod130 ~]# cp /var/www/html/index.html /var/www/html/bbs/
[root@xuegod130 ~]# ll !$
-rw-r–r-- 1 root root 25 5月 20 23:49 index.html

5)物理机访问(IP地址:192.168.1.104)
在这里插入图片描述
6)用Linux虚拟机访问(IP地址:192.168.1.140)
[root@xuegod140 ~]# elinks 192.168.1.130
在这里插入图片描述
使用curl命令访问网站
[root@xuegod140 ~]# curl 192.168.1.130

403 Forbidden

Forbidden

You don't have permission to access / on this server.

3、使用别名、引用网站根目录以为的路径
将/usr/local/phpdata 目录通过虚拟目录功能添加到网站根目录。当访问http://192.168.1.63/ phpdata/ 时,就可以访问目录/usr/local/phpdata中的内容。

注:Apache的别名也叫虚拟目录
语法:
AliasURL路径 PATH物理路径

1)创建测试数据
[root@xuegod130 ~]# mkdir /usr/local/phpdata
[root@xuegod130 ~]# echo “This Alias PHPdata” > /usr/local/phpdata/index.html
2)修改配置文件
162 alias /phpdata/ “/usr/local/phpdata/”
163 <Directory “/usr/local/phpdata/”>
164 Options Indexes FollowSymLinks #首页不存在,允许访问当前目录下其它内容
165 AllowOverride None
166 Require all granted #允许访问所有
167
注:Alias /phpdata/ “/usr/local/phpdata/” #/phpdata/ 可以随意起。比如改/phpdata/ 为/php/ 则访问链接: http://192.168.1.63/php/

3)重启httpd服务
[root@xuegod130 conf]# !sys
systemctl restart httpd

4)测试
在这里插入图片描述
字符界面浏览器测试:(配置文件修改为允许1.0网段可以访问,所以140可以访问)

Require ip 192.168.1.0/24 #允许1.0网段访问
Require not ip 192.168.2.140 #禁止2.0网段访问

在这里插入图片描述

4、实现apache打开软链接功能-禁止显示目录列表-用户认证
1)当一个目录下没有首页时,访问地址时,会自动打开当前目录下的其它内容
[root@xuegod130 bbs]# pwd
/var/www/html/bbs
[root@xuegod130 bbs]# ls
index.html
[root@xuegod130 bbs]# mv index.html index.html.bak #修改默认首页文件
在这里插入图片描述
2)修改配置文件,取消首页不存在,默认显示目录下内容的功能
144 # Options Indexes FollowSymLinks #注释这行内容
164 # Options Indexes FollowSymLinks #注释(表示全局)

3)重启httpd服务
[root@xuegod130 html]# !sys
systemctl restart httpd

4)测试
在这里插入图片描述
字符界面浏览:
[root@xuegod140 ~]# curl 192.168.1.130

403 Forbidden

Forbidden

You don't have permission to access / on this server.

5)通过用户认证-对目录进行保护
设置/usr/local/phpdata/目录,只能通过用户名和密码访问
修改配置文件:
162 alias /phpdata/ “/usr/local/phpdata/”
163 <Directory “/usr/local/phpdata/”>
164 Options Indexes FollowSymLinks
165 AllowOverride None
166 # Require all granted #注释这一行,不要默认都登录
167 authtype basic
168 authname “my web site”
169 authuserfile /etc/httpd/conf/passwd.secret
170 require valid-user
171

参数说明:
authtype basic #authtype命令:指定认证类型为:basic。
authname "my web site " #AuthName命令:指定认证区域名称。区域名称是在提示要求认证的对话框中显示给用户的。
authuserfile /etc/httpd/conf/passwd.secret #AuthUserFile命令:指定一个包含用户名和密码的文本文件,每行一对。
require命令 #指定哪些用户或组才能被授权访问。如:
require user user1 user2 (只有用户user1和user2可以访问)
require valid-user (在AuthUserFile指定的文件中任何用户都可以访问)

6)利用apache附带的程序htpasswd,生成包含用户名和密码的文档
[root@xuegod130 html]# htpasswd -cm /etc/httpd/conf/passwd.secret tom
New password: #123456
Re-type new password: #123456
Adding password for user tom
[root@xuegod130 html]# htpasswd -m /etc/httpd/conf/passwd.secret tom2
New password: #123456
Re-type new password: #123456
Adding password for user tom2

注意:
#第一个生成用户名和密码,需要创建文件,因此需要加-c参数
#第二次创建用户,不能用-c,否则会把前面的用户覆盖了

查看htpasswd参数功能:
-c Create a new file.
-m Force MD5 encryption of the password.

7)查看密码文件
[root@xuegod130 ~]# cd /etc/httpd/conf/
[root@xuegod130 conf]# ls
httpd.conf httpd.conf.bak magic passwd.secret
[root@xuegod130 conf]# cat passwd.secret #可以看到密码是加密的
tom:$apr1 0 G m i R u N 4 0GmiRuN4 0GmiRuN4B3c8H4zHjAxMCcJqNtYGi/
tom2: a p r 1 apr1 apr1f9urof13$duzU3ve9AOhF4BLKeVg0I/

8)重启httpd服务
[root@xuegod130 conf]# !sys
systemctl restart httpd

9)测试
在这里插入图片描述

三、apache虚拟主机基于-IP-域名-端口三种搭建方式
配置Apache虚拟主机,实现在一台服务器上运行多个网站
Apache虚拟主机实现有三种方法:
1)通过不同的IP地址
2)通过不同的域名
3)通过不同的端口号

1、通过不同IP地址
1)因为需要两个IP地址,给网卡增加一个临时的IP地址
[root@xuegod140 ~]# ifconfig ens33:1 192.168.1.200/24

2)创建虚拟主机的配置文件
[root@xuegod130 ~]# cd /etc/httpd/conf.d/
[root@xuegod130 conf.d]# vim bbs.conf
<VirtualHost 192.168.1.130:80>
ServerAdmin webmaster@dummy-host.example.com
DocumentRoot /var/www/html/
ServerName dummy-host.example.com
ErrorLog logs/dummy-host.example.com-error_log
CustomLog logs/dummy-host.example.com-access_log common

<VirtualHost 192.168.1.200:80>
ServerAdmin webmaster@dummy-host.example.com
DocumentRoot /var/www/html/bbs/
ServerName dummy-host.example.com
ErrorLog logs/dummy-host.example.com-error_log
CustomLog logs/dummy-host.example.com-access_log common

3)重启httpd服务
[root@xuegod130 conf.d]# !sys
systemctl restart httpd

4)测试
修改数据,形成不同
[root@xuegod130 html]# cat index.html
welcome to www.xuegod.cn
[root@xuegod130 html]# echo “welcome to www.xuegod.cn bbs” > bbs/index.html
[root@xuegod130 html]# cat bbs/index.html
welcome to www.xuegod.cn bbs
在这里插入图片描述
在这里插入图片描述

2、通过不同域名
这种访问在企业比较常见,其他两种访问可以了解下即可
在域名管理后台,修改DNS配置,www.xuegod.com与bbs.xuegod.com解析成相同的IP。
1)修改配置文件
<VirtualHost *:80>
ServerAdmin webmaster@dummy-host.example.com
DocumentRoot /var/www/html/
ServerName www.xuegod.cn
ErrorLog logs/www.xuegod.cn-error_log
CustomLog logs/www.xuegod.cn-access_log common

<VirtualHost *:80>
ServerAdmin webmaster@dummy-host.example.com
DocumentRoot /var/www/html/bbs/
ServerName bbs.xuegod.cn
ErrorLog logs/bbs.xuegod.cn-error_log
CustomLog logs/bbs.xuegod.cn-access_log common

2)重启httpd服务
[root@xuegod130 conf.d]# !sys
systemctl restart httpd

3)测试
因为我们这里做的是测试环境,所以直接在本地的hosts文件增加域名和IP对应记录
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

3、基于端口配置虚拟主机
1)修改apache的主配置文件,添加监听端口
42 Listen 80
43 Listen 8088
2)修改虚拟主机配置文件
<VirtualHost *:80>
ServerAdmin webmaster@dummy-host.example.com
DocumentRoot /var/www/html/
ServerName www.xuegod.cn
ErrorLog logs/www.xuegod.cn-error_log
CustomLog logs/www.xuegod.cn-access_log common

<VirtualHost *:8088>
ServerAdmin webmaster@dummy-host.example.com
DocumentRoot /var/www/html/bbs/
ServerName bbs.xuegod.cn
ErrorLog logs/bbs.xuegod.cn-error_log
CustomLog logs/bbs.xuegod.cn-access_log common

3)重启httpd服务
[root@xuegod130 conf.d]# !sys
systemctl restart httpd

4)测试
在这里插入图片描述
在这里插入图片描述
字符浏览器测试:
[root@xuegod140 conf.d]# curl www.xuegod.cn
welcome to www.xuegod.cn
[root@xuegod140 conf.d]# curl bbs.xuegod.cn
#这里因为实验环境,模式还是一个网卡,所以还是将网址重定向到www.xuegod.cn,正确情况应该是无法打开网页
welcome to www.xuegod.cn
[root@xuegod140 conf.d]# curl bbs.xuegod.cn:8088
welcome to www.xuegod.cn bbs

  • 30
    点赞
  • 204
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值