一、准备工作:准备一台服务器(Centos6.5系统)IP192.168.2.101


1、安装服务器端:httpd

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

2、安装浏览器:

[root@http ~]# yum -y install elinks

使用方法:

[root@http ~]# elinks www.baidu.com

3、启动服务

[root@http ~]# service httpd restart

报错

 image.png

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

image.png

 

4、查看端口监听状态

[root@http ~]# netstat -anptu | grep 80

image.png

 

5、修改配置文件,实战举例


二、相关参数调优案例举例

案例一: 搭建一台测试web服务器

部门内部搭建一台WEB服务器,采用的IP地址和端口为192.168.2.101:80,首页采用index.html

文件。管理员E-mail地址为  hk@hk.cn,网页的编码类型采用UTF-8,所有网站资源都存放在/var/www/html目录下,并将Apache的配置文件根目录设置为/etc/httpd目录。

 

需要修改配置文件相关参数:

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

行号   参数

57    ServerRoot "/etc/httpd"#apache配置文件的根目录

70    Timeout 60  #超时时间,接收和发送前超时秒数

136   Listen 80    #监听的端口

262   ServerAdmin root@localhost  #设置管理员,e-mail 地址

276   ServerName 192.168.2.101:80   #服务器主机名.  按模板添加一行

image.png

293  DocumentRoot "/var/www/html"   #网站页面根目录

 

 Options Indexes FollowSymLinks   #当一个目录没有默认首页时,允许显示此目录列表

禁止 Apache 显示目录结构列表,只需将 Option 中的 Indexes 去掉即可,或者改成-Indexes

image.png

image.png

 

403    DirectoryIndex index.html index.html.var#指定默认首页    添加网页类型

image.png

 

760  AddDefaultCharset UTF-8   # 设置服务器的默认编码为: UTF-8

 

案例二:取消apache默认欢迎页:

[root@http ~]# vim /etc/httpd/conf.d/welcome.conf

原默认欢迎界面如下

image.png

 

 修改后如下但是目录结构还能显示:

image.png

332行取消目录结构后重启服务如下图:

image.png

image.png

# This configuration file enables the default "Welcome"

# page if there is no default index page present for

# the root URL.  To disable the Welcome page, comment

# out all the lines below.

#

#<LocationMatch "^/+$">                             #把红色内容进行注释

#    Options -Indexes

#    ErrorDocument 403 /error/noindex.html

#</LocationMatch>

重启:

[root@http ~]# service httpd restart

 

创建首页文件

[root@http ~]# echo "welcome to this country" > /var/www/html/index.html注:selinux 关闭  getenforce查看

image.png

 

案例三:修改网站根目录及对应参数,设置访问权限

设置根文档目录为/var/www/html/test 并显示结构目录选项,/var/www/html设置不允许查看目录选项

image.png

重启服务后测试如下图:

image.png

并没有访问/var/www/html/test/td文件的权限

image.png

 

说明:目录与访问控制:

<Directory "/var/www/html/test">  #子目录会继承这个目录的属性,所以读取不了td的目录结构,但test目录下没有index.html文件时也被限制访问目录权限(测试得到的结果)

    Options -Indexes FollowSymLinks       #Options:Indexes:目录浏览 #Followsymlinks:可以用连接

    AllowOverride None                   #不允许任何Override

    Order allow,deny

     Allow from 192.168.2.0/24                     #从哪里来的允许 

Deny from 192.168.2.0/24                      #从哪里来的拒绝

Allow from  .baidu.com

#Allow,Deny都会读取,如果有冲突和未说明的时候按照Order选项逗号后面的那个为准。

谁写到后面,谁的优先级高。

</Directory>

配置完重启apache服务

测试:

image.png

 

 

案例四: 使用别名功能,引用网站根目录以外的路径。

将/usr/local/http 目录通过虚拟目录功能添加到网站根目录。当访问http://192.168.2.101/http/ 时,就可以访问目录/usr/local/htp中的内容。

注:apache的别名也叫虚拟目录

语法:

Alias  URL路径   PATH物理路径

创建测试目录和数据

[root@http local]# mkdir /usr/local/http

[root@http local]# cp -r /boot/grub/ /usr/local/http/

[root@http local]# echo "test1"  > /usr/local/http/test.html

 

修改配置文件

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

image.png

 

注:Alias /http/ "/usr/local/http/"     #/http/ 可以随意起名  比如改/http/ 为/HK/  则访问链接: http://192.168.1.63/HK/

 

重启服务

image.png

案例五:当一个目录下没有默认首页时,访问http://192.168.2.101/http/禁止显示目录列表

修改配置文件:

[root@http local]#vim /etc/httpd/conf/httpd.conf  # 修改红色标记内容

image.png

结构目录禁止访问

image.png

页面文件还是可以访问

image.png

案例六: 打开软链接功能,通过软件链接直接引用网站根目录以外的内容

 

[root@http ~]# mkdir /wg

[root@http ~]# echo test000 > /wg/ss.html

[root@http ~]# ln -s /wg/ /var/www/html/wg

[root@http ~]# cd /var/www/html/

[root@http html]# ls

wg

[root@http html]# cat /wg/ss.html

test000

 [root@http ~]#vim /etc/httpd/conf/httpd.conf  将根目录修改为/var/www/html后重启服务

image.png

image.png

spacer.gif

 

案例七:通过用户认证的方式,对网站下/usr/local/http/目录进行保护。 设置/usr/local/http/目录,只能通过用户名密码方式访问。

 

方法一:

Alias /http/ "/usr/local/http/"

<Directory "/usr/local/http/">

    Options Indexes FollowSymLinks

    AllowOverride None

    Order deny,allow

    Allow from all

    authtype basic           #基本认证

    authname "MySQL admin software"   可以随意命名

    authuserfile /etc/httpd/conf/passwd.secret    #密文配置文件路径

   require valid-user   #有效用户

    #require user tom  #拒绝访问的用户

    #require user tom bob #拒绝访问的多个用户

   

</Directory>

image.png

重启apache 访问http://192.168.2.101/http验证

image.png

生成认证账号和密码

[root@http html]# htpasswd -cm /etc/httpd/conf/passwd.secret hk

New password:

Re-type new password:

Adding password for user hk

再次验证:

 image.png

 

方法二:

[root@http html]# vim /etc/httpd/conf/httpd.conf

<Directory "/usr/local/http/">

    Options Indexes MultiViews FollowSymLinks

    AllowOverride None

    Order allow,deny

    Allow from all

    AllowOverride AuthConfig

</Directory>

# AllowOverride AuthConfig一行表示允许对/usr/local/http/目录下的文件进行用户认证。

[root@http html]# vim /usr/local/phpmyadmin/.htaccess

authtype basic

authname "haa"

authuserfile /etc/httpd/conf/passwd.secret

#require user tom

require valid-user


配置Apache虚拟主机,实现在一台服务器上运行多个网站  :https://blog.51cto.com/13719714/2109040                                                                              

相关Mysql-proxy实现读写分离参考链接:https://blog.51cto.com/13719714/2107691

使用LAMP环境搭建wordpress论坛参考链接:https://blog.51cto.com/13719714/2108415 

源码编译LNMP请参考链接https://blog.51cto.com/13719714/2110940