《Linux菜鸟入门2》Apache

 

●apache服务j_0057.gif


1.什么是apache


    Apache是世界使用排名第一的Web服务器软件。它可以运行在几乎所有广泛使用的计算机平台上,由于其跨平台和安全性被广泛使用,是最流行的Web服务器端软件之一。它快速、可靠并且可通过简单的API扩充,将Perl/Python解释器编译到服务器中。同时Apache音译为阿帕奇,是北美印第安人的一个部落,叫阿帕奇族,在美国的西南部。httpd是Apache超文本传输协议(HTTP)服务器的主程序

        lamp=linux apache mysql php

        lnmp=linux nginx mysql php

虽然nginx(nginx提供共享)性能更好,但是不是开源

  

    相关命令

        curl -I www.baidu.com(域名) 查看域名使用的信息

         nmap 网络扫描

        yum install nmap -y

        nmap -A www.xupt.ed        在国外此命令违法


2.配置apache


①server端配置

hostnamectl set-hostname web1.westos.com

yum install httpd -y

cd /var/www/html/

systemctl start httpd

vim /etc/httpd/conf/httpd.conf

163 <IfModule dir_module>

164     DirectoryIndex index.html file

165 </IfModule>

在配置文件中,index.html和 file文件,哪个在前,在浏览器中优先看哪个,若都没有,则浏览器显示界面为apache测试页面

 

yum install httpd-manual.noarch -y  httpd使用手册


●更改默认发布目录:


将默认发布目录/var/www/html/改为/www/westos

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

[root@web1 html]# getenforce

Enforcing

[root@web1 html]# ls -Zd .

drwxr-xr-x. root root system_u:object_r:httpd_sys_content_t:s0 .

[root@web1 html]# mkdir /www/westos -p

[root@web1 html]# semanage fcontext -a -t httpd_sys_content_t '/www/westos(/.*)?'

[root@web1 html]# restorecon -RvvF /www/

restorecon reset /www context unconfined_u:object_r:default_t:s0->system_u:object_r:default_t:s0

restorecon reset /www/westos context unconfined_u:object_r:default_t:s0->system_u:object_r:httpd_sys_content_t:s0

[root@web1 html]# cd /www/westos/

[root@web1 westos]# vim index.html 编辑内容为/www/westos's page

[root@web1 westos]# vim /etc/httpd/conf/httpd.conf 修改内容为:

119 #DocumentRoot "/var/www/html"

120 DocumentRoot "/www/westos"

121 <Directory "/www/westos">

122        require all granted

123 </Directory>

[root@web1 westos]# systemctl restart httpd.service     

在server浏览器中输入http://172.25.254.149  得到 /www/westos's page   表示更改成功

 

●端口:


[root@web1 westos]# ss -antlp | grep httpd

LISTEN     0      128                      :::80                      :::*

[root@web1 westos]# vim /etc/httpd/conf/httpd.conf

41 #Listen 12.34.56.78:80

42 Listen 80   默认为80端口

http://ip:80也可不加80一般默认

若改变为Listen 8080,则在浏览器中输入http://ip:8080

如:  http://172.25.254.149:8080


 但8080端口容易被***利用***,慎用。


●访问权限黑白名单


[root@web1 westos]# vim /etc/httpd/conf/httpd.conf

120 DocumentRoot "/www/westos"

121 <Directory "/www/westos">

122        require all granted

123        Order Deny,Allow    **谁在前,先读谁,一般Allow在前

124        Allow from 172.25.254.0/24  **允许172.25.254.x网段所有人访问

125        Deny from ALL   **拒绝所有人访问

126 </Directory>

[root@web1 westos]# systemctl restart httpd.service  

  

●设置用户访问权限


vim /etc/httpd/conf/httpd.conf

121 <Directory "/www/westos">

122        Require all granted

123        AllowOverride all

124        Authuserfile /etc/httpd/htpasswdfile   用户信息所在文件

125        Authname "Please input username and password"  提示信息

126        Authtype basic    基本认证类型

127        Require user admin    只允许使用用户admin在浏览器中访问

128 </Directory>

systemctl restart httpd.service

若要所以用户都可访问则:

vim /etc/httpd/conf/httpd.conf

127        Require valid-user

systemctl restart httpd.service

访问需要输入用户和密码


 

●apaache的虚拟主机


vim /etc/httpd/conf/httpd.conf先修改为初始状态

119 DocumentRoot "/var/www/html"

120 #DocumentRoot "/www/westos"

121 #      

122 # Relax access to content within /var/www.

 

[root@web1 westos]# cd /etc/httpd/

[root@web1 httpd]# ls

conf  conf.d  conf.modules.d  logs  modules  run

[root@web1 httpd]# htpasswd -cm htpasswdfile admin   -c表示建立

New password:

Re-type new password:

Adding password for user admin

[root@web1 httpd]# htpasswd -m htpasswdfile westos  第二次新建不用加c,若加-c会覆盖第一次建立的用户信息

New password:

Re-type new password:

Adding password for user westos

[root@web1 httpd]# cat htpasswdfile

admin:$apr1$JICcD90s$CUjJyhcTEEHIz5x1qj55z1

westos:$apr1$6rk8RbXl$CIFniY0.TwqKr.oY.tvCg1

[root@web1 httpd]# vim /etc/httpd/conf/httpd.conf

[root@web1 httpd]# ll /etc/httpd/htpasswdfile

-rw-r--r--. 1 root root 89 Dec  8 01:17 /etc/httpd/htpasswdfile

vim /etc/httpd/conf/httpd.conf

[root@web1 httpd]# systemctl restart httpd.service

[root@web1 httpd]# cd /var/www/html/

[root@web1 html]# ls

index.html

[root@web1 html]# vim index.html  这里是默认的

[root@web1 html]# mkdir /var/www/virtual/news.westos.com/html -p

[root@web1 html]# mkdir /var/www/virtual/music.westos.com/html -p

[root@web1 html]# cd /var/www/virtual/news.westos.com/html/

[root@web1 html]# vim index.html

[root@web1 html]# cd /var/www/virtual/music.westos.com/html/

[root@web1 html]# vim index.html

[root@web1 html]# cd /etc/httpd/conf.d/

[root@web1 conf.d]# ls

autoindex.conf  manual.conf  README  userdir.conf  welcome.conf

[root@web1 conf.d]# vim /etc/httpd/conf/httpd.conf

[root@web1 conf.d]# vim default.conf

[root@web1 conf.d]# vim music.conf

[root@web1 conf.d]# vim news.conf


[root@web1 conf.d]# ls

autoindex.conf  manual.conf  news.conf  userdir.conf

default.conf    music.conf   README     welcome.co

[root@web1 conf.d]# systemctl restart httpd

 

vim /etc/httpd/conf/httpd.conf                查看文件应该编辑的地方

353 IncludeOptional conf.d/*.conf

 

[root@westos named]# vim /etc/hosts 测试端:(在desktop上)

172.25.254.112 www.westos.com westos.com news.westos.com music.westos.com

firefox -->在浏览器中输入不同的域名

如果显示对应 ,则配置正确


 


 


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章