查看linux4222端口,linux命令:httpd 安装配置及网页虚拟主机 elinks htpasswd

LAMP:L=linux  A=apache网页服务器  M=mysql数据库服务器  P=php应用服务器

httpd:

事先创建进程,按需维持适当的进程,模板化设计,核心比较小,各种功能都模块添加,

支持运行配置,支持单独编译模块。

httpd -t用于检测httpd.conf主配置文档是否存在语法错误

[root@johntest conf]# httpd -t

Syntax OK       检测配置文档语法正常

elinks:linux系统中打开纯文本网页界面。

参数:

-dump   打开纯文本界面后直接退出不进入交互模式

-source  打开网页的源代码

[root@johntest conf]# elinks -source 10.109.134.249

testtest

haha test

htpasswd 生成访问网页用户所需的密码文件

参数:

-c:第一次创建密码文本文件需加此项,表示新建,密码文本存在后不能加-c选项,否则密码文        本文件会被重新建立并替换以前的文本文件。

-m:表示密码以md5加密格式存放在密码文本中

-D:表示删除htpasswd中的用户

[root@johntest conf]#htpasswd -c -m /etc/httpd/conf/htpasswd hadoop

New password:

Re-type new password:

Adding password for user hadoop

[root@johntest conf]#htpasswd -m /etc/httpd/conf/htpasswd tom

New password:

Re-type new password:

Adding password for user tom

[root@johntest conf]# cat htpasswd

hadoop:$apr1$8yVMk/..$8xI.A4UmnNWHuXZfB5fVD.

tom:$apr1$miCJM/..$DNbL8QzsOR6W7cx1vOHll.

[root@johntest conf]# htpasswd -D /etc/httpd/conf/htpasswd tom

Deleting password for user tom

[root@johntest conf]# cat /etc/httpd/conf/htpasswd

hadoop:$apr1$8yVMk/..$8xI.A4UmnNWHuXZfB5fVD.

httpd安装和配置(事先让selinux处于permssive,disabled):rpm包安装、源码包编译安装

httpd:   /usr/sbin/httpd(MPM:prefork)

/etc/rc.d/init.d/httpd:httpd服务脚本 port:(80/tcp);(ssl:443/tcp)

/etc/httpd/:工作根目录,相当于程序安装目录

/etc/httpd/conf/:配置文件目录

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

/etc/httpd/conf.d/*.conf也是属于主配置文件中的

/etc/httpd/modules/:模块目录

/etc/httpd/logs/ --> /var/log/httpd/:日志目录

日志文件有两类:访问日志access_log,错误日志:err_log

/var/www/:网页页面存放目录

/var/www/html/:静态网页存放目录

/var/www/cgi-bin:动态网页存放目录

一、httpd安装:

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

[root@johntest ~]# rpm -ql httpd  查看httpd安装的目录

/etc/httpd

/etc/httpd/conf

/etc/httpd/conf.d

/etc/httpd/conf.d/README

/etc/httpd/conf.d/proxy_ajp.conf

/etc/httpd/conf.d/welcome.conf

[root@johntest ~]#service httpd start 启动httpd进程

[root@johntest ~]# netstat -tlnp |grep :80  查看httpd进程端口

tcp    0    0 :::80     :::*          LISTEN      4222/htt

[root@johntest ~]# ps aux |grep httpd 查看httpd启动的进程

root      4222  0.0  1.2 236100 11204 ?        Ss   10:01   0:00 /usr/sbin/httpd

apache    4224  0.0  0.8 236236  7292 ?        S    10:01   0:00 /usr/sbin/httpd

apache    4225  0.0  0.8 236236  7224 ?        S    10:01   0:00 /usr/sbin/httpd

apache    4226  0.0  0.8 236236  7244 ?        S    10:01   0:00 /usr/sbin/httpd

apache    4227  0.0  0.8 236236  7252 ?        S    10:01   0:00 /usr/sbin/httpd

apache    4228  0.0  0.8 236236  7244 ?        S    10:01   0:00 /usr/sbin/httpd

apache    4229  0.0  0.8 236236  7244 ?        S    10:01   0:00 /usr/sbin/httpd

apache    4230  0.0  0.7 236236  6636 ?        S    10:01   0:00 /usr/sbin/httpd

apache    4231  0.0  0.7 236236  6632 ?        S    10:01   0:00 /usr/sbin/httpd

[root@johntest ~]# cd /etc/httpd/

[root@johntest httpd]# tree conf.d/

conf.d/

|-- README

|-- manual.conf

|-- perl.conf

|-- php.conf

|-- proxy_ajp.conf

|-- python.conf

|-- squid.conf

|-- ssl.conf

|-- webalizer.conf

`-- welcome.conf   默认httpd网页显示内容/etc/httpd/conf.d/welcome.conf

二、httpd配置:

配置文档路径:/etc/httpd/conf/httpd.conf   配置文档分三个部分

[root@johntest conf]# grep "Section" httpd.conf

### Section 1: Global Environment   全局环境段

### Section 2: 'Main' server configuration  主服务器段

### Section 3: Virtual Hosts   虚拟主机段

*第2部分与第3部分不能同时生效.(默认启用主服务器配置)

http:指令不区分字符大小写,value则根据需求有可能要区分大小写

以#开头后直接接字符的表示可以启用段,以# 开头且后跟空格后再接字符表示纯注释段

配置文件由2部分组成:  directive(指令)  value(值)

[root@johntest conf]# vim httpd.conf

全局环境段解析:

--------------------------------------------------------------------------

#

# This is the main Apache server configuration file.  It contains the

# configuration directives that give the server its instructions.

# See for detailed information.

# In particular, see

#

# for a discussion of each configuration directive.

#

ServerTokens  OS

指令     值

Timeout   120         #超时时间(秒)

KeepAlive   Off         #是否开启常链接

MaxKeepAliveRequests   100  #保持常连接的数量0为无限制

KeepAliveTimeout  15      #保持常连接的时间(秒)

StartServers       8 #服务器一开启就启动8个空闲进程

MinSpareServers     5 #至少保证5个空闲进程接收用户请求

MaxSpareServers     20 #最大空闲进程数,超过该数值直接结束其他空闲进程

ServerLimit      256 #该项限制用户最大请求数,如需提高接收用户最大请求数,必须先关闭           httpd服务进程后再把该数值提高,并把MaxClients数值提高,重启服务进程才生效。

MaxClients        256 #接收用户的最大请求数

MaxRequestsPerChild  4000 #一个进程最多响应请求数,超过该数字,该进程将直接被强制结束

StartServers       2  #服务器一开启就启动2个空闲进程数

MaxClients        150 #接收用户的最大请求数

MinSpareThreads     25  #最小空闲线程数(所有进程一起的线程数)

MaxSpareThreads     75  #最大空闲线程数(所有进程一起的线程数)

ThreadsPerChild     25  #每个进程生成多少个线程数

MaxRequestsPerChild  0   #没一个进程响应多少个请求,因为是通过线程响应所以这里没做限制

Listen 80      #指定监听端口

Listen 192.168.1.10:8080  #指定监听特定IP的端口

Include conf.d/*.conf   #/etc/httpd/conf.d/*.conf所有文件都嵌入到该配置文档中

User apache    #默认用户

Group apache    #默认用户组

------------------------------------------------------------------------------------

主服务器段各选项的解析:

ServerAdmin root@localhost   #管理员邮箱

#ServerName 192.168.1.100:80 #服务器主机名称

DocumentRoot "/var/www/html"#文档根目录,存放网页文档的路径

Options Indexes FollowSymLinks #Indexes:允许索引目录 ;FollowSymLinks:允许访问符号链接指向的原文件;Includes:允许执行服务端包含(SSI)脚本;none:不支持任何选项;all:支持所有选项

# AllowOverride controls what directives may be placed in .htaccess files.

# It can be "All", "None", or any combination of the keywords:

#   Options FileInfo AuthConfig Limit

AllowOverride None #是否允许覆盖控制规则,AllowOverride有几个选项 All,None,AuthConfig

(AllowOverride AuthConfig  执行下面的Auth控制规则

AuthType Basic     规则类型

AuthName "Restricted Site..."   规则名称

AuthUserFile "/etc/httpd/conf/htpasswd"  需要该文本中的密码设置才能访问,如何建立密码文本                              请参考本文前部分htpasswd命令使用方法。

Require valid-user   valid-user表示htpasswd文档中的所有用户都能访问,如果只是允许文档中                 一部分用户访问,则需要配置 Require user username

)

Order allow,deny  #order用于定义基于主机的访问功能的,IP,网络地址或主机定义访问控制机制

Allow from all    #运行所有主机访问

地址的表示方式:IP:192.168.0.0/16  hostname:www.a.comdomainname:john.com

Usage:

1、仅允许192.168.0.0/24主机段访问

Order allow,deny

Allow from 192.168.0.0/24

2、拒绝172.16.100.177 192.168.0.1主机访问

Order deny,allow

Deny from 172.16.100.177 192.168.0.1

实例:设置访问网页需要通过用户名和密码访问

[root@johntest conf]#htpasswd -c -m /etc/httpd/conf/htpasswd hadoop   #首先生成密码文件

New password:

Re-type new password:

Adding password for user hadoop

[root@johntest conf]# htpasswd -m /etc/httpd/conf/htpasswd tom

New password:

Re-type new password:

Adding password for user tom

[root@johntest conf]# cat ht

htpasswd         httpd.conf       httpd.conf.back

[root@johntest conf]# cat htpasswd

hadoop:$apr1$8yVMk/..$8xI.A4UmnNWHuXZfB5fVD.

tom:$apr1$miCJM/..$DNbL8QzsOR6W7cx1vOHll.

[root@johntest conf]# vim httpd.conf    #修改配置文件,基于用户认证

f4703eaf2b5543178388459dea8cb16f.png

[root@johntest conf]# httpd -t   #检查配置文件是否有语法错误

Syntax OK

[root@johntest conf]# service httpd restart       #重启服务进程

停止 httpd:                                               [确定]

启动 httpd:                                               [确定]

再访问该站点时,提示输入用户名和密码:

8768d40305de87ecdee0735aa6c9d2a7.png

输入htpasswd密码文本中的正确用户和密码后才能访问:

6e699ca1df781701dfcf2a1d6479d688.png

[root@johntest conf]#vim httpd.conf    #修改配置文件,基于组用户认证

8158fe3f70397346581cbd723cd8c9fa.png

[root@johntest conf]#vim htgroup   #建立组用户文件,内容如下:

myusers: hadoop john     #hadoop为htpasswd中存在的用户,john为htpasswd中不存在的用户,这                   里tom用户没在myusers组用户中

实验结果得知,只有hadoop用户能正常访问,john和tom用户无法访问。

是否启用用户家目录网页设置:

c658583a4a2ec7b71ca0c4a732e441a4.png

[root@johntest conf]# useradd hadoop  #新建hadoop用户

[root@johntest conf]# su - hadoop     #切换至hadoop用户

[hadoop@johntest ~]$ mkdir public_html  #新建public_html目录

[hadoop@johntest ~]$ cd public_html/

[hadoop@johntest public_html]$ vim index.html  #建立索引网页

haha hadoop publichtml

[hadoop@johntest public_html]$ cd

[hadoop@johntest ~]$ chmod o+x /home/hadoop #更改hadoop家目录权限,是其他人可以进入目录

[root@johntest conf]# ll /home

总计 40

drwx------ 4 donggen donggen  4096 12-29 09:52 donggen

drwx-----x 5 hadoop  hadoop   4096 01-13 08:48 hadoop  #其他人增加进入目录的权限,否则无                                        法访问里面的网页

drwx------ 4 john    john     4096 12-29 08:13 john

drwx------ 2 root    root    16384 12-07 00:53 lost+found

[root@johntest conf]# service httpd restart

service httpd restart

停止 httpd:                                               [确定]

启动 httpd:                                               [确定]

[root@johntest conf]# httpd -t

Syntax OK

DirectoryIndex index.html index.html.var #默认打开的第一个网页为index.html,依次类推,第一                             个打不开,会自动搜索下一个默认网页。

Alias /icons/ "/var/www/icons/" # 路径别名

Alias /luntan "/bbs/forum"      #表示访问http://10.109.134.249/luntan网页就是访                              问/bbs/forum下的index.html网页

MPM:Multi Path Modules,它提供了可以选择的多处理模块(MPM),用来绑定到网络端口上,接受请求, 以及调度子进程处理请求。

mpm_winnt:windows响应请求的工作模型

prefork:一个请求用一个进程响应;(默认使用的处理请求的模型)

worker:一个请求用一个线程响应,(启动多个进程,每个进程生成多个线程)

event:一个进程处理多个请求

*Apache(httpd)支持多种方式的虚拟主机配置:切记想要虚拟主机配置生效,必须先注释掉主配置文件         httpd.conf文档中的DocumentRoot这行内容

一、定义所谓虚拟主机是指在一台服务器里运行几个网站,提供WEB、FTP、Mail等服务。

二、虚拟主机的实现方法有三种:

基于IP的方法,基于主机名的方法和基于端口的法官法。    ①基于IP的方法:  在服务器里绑定多个IP,然后配置WEB服务器,把多个网站绑定在不同的IP上。访问不同的IP,就看到不同的网站。    ②基于端口的方法:    一个IP地址,通过不同的端口实在不同网站的访问。    ③基于主机名的方法:    设置多个域名的A记录,使它们解析到同一个IP地址上,即同一个服务器上。然后,在服务器上配置WEB服务端,添加多个网站,为每个网站设定一个主机名。因为HTTP协议访问请求里包含有主机名信息,当WEB服务器收到访问请求时,就可以根据不同的主机名来访问不同的网站。

三、虚拟主机实现的基本配置:必须先取消掉主配置文档中DocumentRoot该行。

打开web服务的主配置文档:vim /etc/httpd/conf/httpd.confDocumentRoot 注释掉该行

然后在/etc/httpd/conf.d/目录下新建虚拟主机配置文档virtual.conf,具体内容如下三种情况:

①基于IP虚拟主机的实现:     基于多个IP地址:     配置虚拟主机:

[root@johntest conf.d]# vim /etc/httpd/conf.d/virtual.conf #新建虚拟主机的配置文档,内容如下

    DocumentRoot "/var/www/xuelinux.com"    ServerName  hello.xuelinux.com   

    DocumentRoot "/var/www/weblinux.com"    ServerName  haha.weblinux.com   

[root@johntest conf.d]# httpd -t   #检测配置文件是否有语法错误

Warning: DocumentRoot [/var/www/xuelinux.com] does not exist  #暂时该目录不存在

Warning: DocumentRoot [/var/www/weblinux.com] does not exist #暂时该目录不存在

Syntax OK

[root@johntest conf.d]# mkdir -pv /var/www/{xuelinux.com,weblinux.com} #创建上面2个目录

mkdir: 已创建目录 “/var/www/xuelinux.com”

mkdir: 已创建目录 “/var/www/weblinux.com”

[root@johntest conf.d]# vim /var/www/xuelinux.com/index.html #新建第一个网页的内容

xuelinux.com

hello.xuelinux.com

[root@johntest conf.d]# vim /var/www/weblinux.com/index.html #新建第二个网页的内容

weblinux.com

haha.weblinux.com

[root@johntest conf.d]# httpd -t  #检测配置文件无语法错误

Syntax OK

root@johntest conf.d]# service httpd restart  #重启httpd服务

停止 httpd:                                    [OK]

启动 httpd:                                    [OK]

[root@johntest conf.d]# ip addr add 10.109.134.248/22 dev eth0 #给网卡eth0增加辅助IP地址

[root@johntest conf.d]# ip addr show   #查看网卡所有IP地址信息

1: lo: mtu 16436 qdisc noqueue

link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00

inet 127.0.0.1/8 scope host lo

inet6 ::1/128 scope host

valid_lft forever preferred_lft forever

2: eth0: mtu 1500 qdisc pfifo_fast qlen 1000

link/ether 00:0c:29:10:ef:0f brd ff:ff:ff:ff:ff:ff

inet 10.109.134.249/22 brd 10.109.135.255 scope global eth0

inet 10.109.134.248/22 scope global secondary eth0 #辅助IP地址设置成功

inet6 fe80::20c:29ff:fe10:ef0f/64 scope link

valid_lft forever preferred_lft forever

3: sit0: mtu 1480 qdisc noop

link/sit 0.0.0.0 brd 0.0.0.0

浏览器中输入IP地址进行实验效果的验证:

000d6ff17a015fb2eedf70841e3f589f.png

367eb5085be44233e1347a6f781e4563.png

至此,基于IP地址的虚拟主机配置访问正常

②基于端口:

[root@johntest conf.d]#vim /etc/httpd/conf.d/virtual.conf #编辑虚拟主机配置文件

DocumentRoot "/var/www/xuelinux.com"ServerName  hello.xuelinux.com

     DocumentRoot "/var/www/testlinux.com"ServerName  test.xuelinux.com

[root@johntest conf.d]#vim /etc/httpd/conf/httpd.conf #编辑httpd主配置文件,使其监听8080端口

2bf292c5aaa11df42ddc705fd49bd591.png

[root@johntest conf.d]# httpd -t

Warning: DocumentRoot [/var/www/testlinux.com] does not exist  #该目录暂时不存在

Syntax OK

[root@johntest conf.d]# mkdir -p /var/www/testlinux.com  #新建该目录

[root@johntest conf.d]# vim /var/www/testlinux.com/index.html #添加网页索引文件index.html

testlinux.com

Hi.testlinux.com

[root@johntest conf.d]# service httpd restart  #重启httpd服务

停止 httpd:                                     [OK]

启动 httpd:                                     [OK]

浏览器输入10.109.134.249:8080测试成功

c6cade616f1046308455a0c839c60e9f.png

③基于主机名:

[root@johntest conf.d]#vim /etc/httpd/conf.d/virtual.conf #编辑虚拟主机配置文件

NameVirtualHost 10.109.134.249:80   #必须增加该行才能启用基于主机名称的虚拟主机

   DocumentRoot "/var/www/a.com"ServerName  www.a.com

ErrorLog  "/var/logs/httpd/namelinux.com-error_log"   #网页错误日志保存路径    CustomLog /var/logs/httpd/namelinux.com-access_log common #访问网页日志记录保存路径

common 表示记录访问日志模式   

    ServerName    www.b.com    DocumentRoot  "/var/www/b.com"   

[root@johntest conf.d]# httpd -t

Warning: DocumentRoot [/var/www/a.com] does not exist

Warning: DocumentRoot [/var/www/b.com] does not exist

Syntax OK

[root@johntest conf.d]# mkdir -pv /var/www/{a.com,b.com}

mkdir: 已创建目录 “/var/www/a.com”

mkdir: 已创建目录 “/var/www/b.com”

需在测试链接的客户端host文件中添加以下2行

10.109.134.249   www.a.com10.109.134.249   www.b.com

浏览器输入www.a.comwww.b.com测试成功

46ca8c354f827ffe877b42e836514a02.png

四、案例综合实现建立http服务器,要求:    1)提供两个基于名称的虚拟主机:    (a)www1.ilinux.org,页面文件目录为/var/www/html/www1;错误日志 为/var/log/httpd/www1.err,访问      日志为/var/log/httpd/www1.access;    (b)www2.ilinux.org,页面文件目录为/var/www/html/www2;错误日志为/var/log/httpd/www2.err,访问日      志为/var/log/httpd/www2.access;    (c)为两个虚拟主机建立各自的主页文件index.html,内容分别为其对应的主机名;    2)www1主机仅允许192.168.0.0/24网络中的客户机访问;www2主机可以被所有主机访问;     为http服务提供第3个虚拟主机,要求:    1)www3.ilinux.org,页面文件目录为/var/www/html/www3;错误日志为/var/log/httpd/www3.err,访问日志      为/var/log/httpd/www3.access;    2)为此虚拟主机提供基本认证功能,并为其提供两个虚拟用户webuser1和webuser2,    密码均为redhat,要求允许此两用户在提供密码的情况下访问此站点;    配置过程如下:   ①安装web服务:yum -y install httpd   ②进入主配置文档vim /etc/httpd/conf/httpd.conf

打开web服务的主配置文档:vim /etc/httpd/conf/httpd.conf    DocumentRoot 注释掉该行

然后在/etc/httpd/conf.d/目录下新建虚拟主机配置文档virtual.conf,内容如下:

    DocumentRoot "/var/www/html/www1"    ServerName  www1.ilinux.org    Errorlog /var/log/httpd/www1.err    CustomLog /var/log/httpd/www1.access common        Options Indexes    AllowOverride None    Order allow,deny    Allow from 192.168.0.0/24                DocumentRoot "/var/www/html/www2"    ServerName  www2.ilinux.org    Errorlog /var/log/httpd/www2.err    CustomLog /var/log/httpd/www2.access common        Options Indexes    AllowOverride None    Order allow,deny    Allow from all                DocumentRoot /var/www/html/www3    ServerName www3.ilinux.org    ErrorLog /var/log/httpd/www3.err    CustomLog /var/log/httpd/www3.access combined        Options Indexes    AllowOverride AuthConfig    AuthName "AuthConfig"    AuthType basic    AuthUserFile /etc/httpd/.htpasswd    require user webuser1 webuser2    Order allow,deny    Allow from all            htpasswd -cm /etc/httpd/.htpasswd webuser1    htpasswd -m /etc/httpd/.htpasswd webuser2   ③分别在/var/www/html目录下创建www1,www2,www3目录    vim /var/www/html/www1/index.html    This is www1 test!    vim /var/www/html/www2/index.html    This is www2 test!    vim /var/www/html/www3/index.html    This is www3 test!   ④service httpd start 启动web服务   ⑤进行实验效果的验证:浏览器中分别输入www1.ilinux.org  www2.ilinux.org www3.ilinux.org

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值