linux学习之apache

来源:互联网 发布:windows.hlp官方下载 编辑:程序博客网 时间:2024/06/18 02:18
#######apache######
1.apache的安装
[root@mariadb ~]# yum install httpd -y         ##安装httpd
[root@mariadb ~]# systemctl start httpd        ##开启服务
[root@mariadb ~]# systemctl stop firewalld     ##关闭防火墙
[root@mariadb ~]# systemctl enable httpd
ln -s '/usr/lib/systemd/system/httpd.service' '/etc/systemd/system/multi-user.target.wants/httpd.service'
[root@mariadb ~]# systemctl disable firewalld
2.apache的基本配置
默认发布文件————————index.html
[root@mariadb ~]# cd /var/www/html/
[root@mariadb html]# vim index.html  ##写入测试时要显示的内容,<h1> </h1>表示设置字体
<h1>index.html</h1>
测试——————在浏览器搜索172.25.254.234

默认配置文件————————/etc/httpd/conf/httpd.conf
                  /etc/httpd/conf.d/*.conf

默认发布目录————————/var/www/html/
默认端口————————————80

3.修改
[root@mariadb ~]# getenforce ##selinux是关闭模式
Disabled

1)修改默认发布文件


[root@mariadb ~]# mkdir /westos/www/test  -p  ##新建目录
[root@mariadb ~]# vim  /westos/www/test/westos.html ##写入要显示的内容
<h1>/westos/www/test/westos.html's page</h1>
[root@mariadb html]# vim /etc/httpd/conf/httpd.conf##将默认发布文件改为先读取westos.html,index.html备用
163 <IfModule dir_module>
164     DirectoryIndex  westos.html  index.html   ##写在前面的先读
165 </IfModule>
2)修改默认发布目录
[root@mariadb ~]# vim /etc/httpd/conf/httpd.conf   ##将默认发布目录改为/westos/www/test,给授权允许任何人访问
119 #DocumentRoot "/var/www/html"
120  DocumentRoot  "/westos/www/test"
121 <Directory "/westos/www/test">
122      Require all granted           

123 </Directory>


[root@mariadb ~]# systemctl restart httpd

测试————在浏览器搜索172.25.254.234/westos.html



[root@mariadb ~]# vim /etc/httpd/conf/httpd.conf  ##设置/var/www/html为默认
119 DocumentRoot "/var/www/html"
120 #DocumentRoot  "/westos/www/test"
[root@mariadb ~]# vim /etc/httpd/conf/httpd.conf

[root@mariadb ~]# systemctl restart httpd


测试————在浏览器搜索172.25.254.234




[root@mariadb ~]# cd /var/www/html/
[root@mariadb html]# ls
index.html  mysqladmin
[root@mariadb html]# mkdir admin
[root@mariadb html]# cd admin/
[root@mariadb admin]# vim index.html
<h1>admin's page</h1>

测试————在浏览器搜索172.25.254.234/admin



3)apache的访问控制
[root@mariadb ~]# vim /etc/httpd/conf/httpd.conf
119 DocumentRoot "/var/www/html"
124 <Directory "/var/www/html/admin">               ##只允许66主机访问admin目录
125         Order Deny,Allow
126         Allow from 172.25.254.66
127         Deny from All

128  </Directory>


[root@mariadb ~]# systemctl restart httpd.service

测试————在66主机浏览器搜索172.25.254.234/admin


测试————在234主机浏览器搜索172.25.254.234/admin



[root@mariadb ~]# vim /etc/httpd/conf/httpd.conf    ##允许所有人访问admin但拒绝66主机
119 DocumentRoot "/var/www/html"
124 <Directory "/var/www/html/admin">               
125         Order Allow,Deny                       ##allow,deny 哪个在前先读哪个    
126         Allow from All
127         Deny from 172.25.254.66

128  </Directory>


[root@mariadb ~]# systemctl restart httpd.service

测试————在66主机浏览器搜索172.25.254.234/admin




测试————在234主机浏览器搜索172.25.254.234/admin


4)设定用户的访问
[root@mariadb httpd]# htpasswd -cm /etc/httpd/accessuser admin  ##设置用户密码
[root@mariadb httpd]# htpasswd -m /etc/httpd/accessuser tom
[root@mariadb httpd]# cat /etc/httpd/accessuser                 ##查看生成的密码,加密性
[root@mariadb httpd]# vim /etc/httpd/conf/httpd.conf
124 <Directory "/var/www/html/admin">
125         AuthUserfile /etc/httpd/accessuser                  ##用户认证文件
126         AuthName "Please input your name and password !!"   ##用户认证提示信息
127         AuthType basic                                      ##认证类型
128         Require valid-user                                  ##认证用户,valid-user表示认证文件中的所有用户都可以通过,user admin 表示只允许认证文件中的admin用户通过认证访问
129  </Directory>

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



测试————在66主机浏览器搜索172.25.254.234/admin




4.apache的语言支持——————php html cgi


1)html语言默认支持
2)php语言
[root@mariadb html]# yum install php -y
[root@mariadb html]# vim index.php
<?php
        phpinfo();
?>
[root@mariadb html]# systemctl restart httpd

测试————在66主机浏览器搜索172.25.254.234/index.php




4)cgi语言

[root@mariadb html]# yum install httpd-manual.noarch  -y
[root@mariadb html]# systemctl restart httpd.service

浏览器查看 http://172.25.254.234/manual/



点击进入 .htaccess files



点击进入http://172.25.254.234/manual/howto/cgi.html



[root@mariadb ~]# mkdir /var/www/html/cgi
[root@mariadb html]# cd /var/www/html/cgi
[root@mariadb cgi]# ls
[root@mariadb cgi]# vim index.cgi
#!/usr/bin/perl
print "Content-type: text/html\n\n";

print `date`;


[root@mariadb cgi]# perl index.cgi
Content-type: text/html

Mon May 15 20:59:18 EDT 2017


[root@mariadb cgi]# vim /etc/httpd/conf/httpd.conf
130  <Directory "/var/www/html/cgi">
131     Options +ExecCGI
132     AddHandler cgi-script .cgi

133  </Directory>


[root@mariadb cgi]# systemctl restart httpd.service

测试浏览器输入172.25.254.234/cgi/index.cgi(查看是否调用date函数)



#####apache的虚拟主机########
1.定义
可以让我们的一台apache服务器在被访问不同域名的时候显示不同的主页
2.建立测试页
[root@mariadb www]# mkdir virtual
[root@mariadb www]# ls
cgi-bin  html  virtual
[root@mariadb www]# mkdir virtual/news.westos.com -p    ##建立两个不同的测试页
[root@mariadb www]# mkdir virtual/money.westos.com -p
[root@mariadb www]# mkdir virtual/news.westos.com/html -p
[root@mariadb www]# mkdir virtual/money.westos.com/html -p
[root@mariadb www]# echo "<h1>money.westos.com's page</h1>" >virtual/money.westos.com/html/index.html ##默认发布文件中写入页面显示内容

[root@mariadb www]# echo "<h1>news.westos.com's page</h1>" >virtual/news.westos.com/html/index.html


3.配置
[root@mariadb conf.d]# vim default.conf
<Virtualhost    _default_:80>           ##虚拟主机开启的端口,默认端口为80
        DocumentRoot "/var/www/html"    ##虚拟主机的默认发布目录
        CustomLog  "logs/default.log" combined   ##虚拟主机日志
</Virtualhost>

[root@mariadb conf.d]# vim news.conf   ##指定域名news.westos.com的访问到指定默认发布目录中
<Virtualhost    *:80>
        ServerName "news.westos.com"
        DocumentRoot "/var/www/virtual/news.westos.com/html"
        CustomLog  "logs/news.log" combined
</Virtualhost>
<Directory "/var/www/virtual/news.westos.com/html">    ##默认发布目录的访问授权
        Require all granted

</Directory>



[root@mariadb conf.d]# vim money.conf
<Virtualhost    *:80>
        ServerName "money.westos.com"
        DocumentRoot "/var/www/virtual/money.westos.com/html"
        CustomLog  "logs/money.log" combined
</Virtualhost>
<Directory "/var/www/virtual/money.westos.com/html">
        Require all granted

</Directory>



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

4.测试
在浏览器主机中
vim /etc/hosts

172.25.254.234  www.westos.com  news.westos.com  money.westos.com


浏览器输入www.westos.com  news.westos.com  money.westos.com 进行测试显示不同的网页





####https####
1.https定义
当http运行在安全接层(SSL)之上,http被称为https,https可以提供保密性,客户和服务器鉴别以及数据完整性
2.配置
[root@mariadb ~]# yum install mod_ssl.x86_64  crypto-utils.x86_64 -y
[root@mariadb ~]# genkey www.westos.com     ##加密,获取保密密码


/usr/bin/keyutil -c makecert -g 1024 -s "CN=www.westos.com, OU=linux, O=westos, L=xi'an, ST=shannxi, C=CN" -v 1 -a -z /etc/pki/tls/.rand.6410 -o /etc/pki/tls/certs/www.westos.com.crt -k /etc/pki/tls/private/www.westos.com.key
cmdstr: makecert

cmd_CreateNewCert
command:  makecert
keysize = 1024 bits
subject = CN=www.westos.com, OU=linux, O=westos, L=xi'an, ST=shannxi, C=CN
valid for 1 months
random seed from /etc/pki/tls/.rand.6410
output will be written to /etc/pki/tls/certs/www.westos.com.crt  
output key written to /etc/pki/tls/private/www.westos.com.key


Generating key. This may take a few moments...

Made a key
Opened tmprequest for writing
/usr/bin/keyutil Copying the cert pointer
Created a certificate
Wrote 882 bytes of encoded data to /etc/pki/tls/private/www.westos.com.key
Wrote the key to:

/etc/pki/tls/private/www.westos.com.key          ##密码位置






[root@mariadb ~]# cd /etc/httpd/conf.d/
[root@mariadb conf.d]# ls
autoindex.conf  manual.conf  news.conf  README    userdir.conf
default.conf    money.conf   php.conf   ssl.conf  welcome.conf
[root@mariadb conf.d]# cp money.conf login.conf     
[root@mariadb conf.d]# vim login.conf
<Virtualhost  *:443>
            ServerName "login.westos.com"    ##指定域名
            DocumentRoot "/var/www/virtual/login.westos.com/html"  ##指向默认配置文件
            CustomLog "logs/login.log" combined                    ##日志存放位置
            SSLEngine on                                           ##开启https功能
            SSLCertificateFile /etc/pki/tls/certs/www.westos.com.crt  ##证书
            SSLCertificateKeyFile /etc/pki/tls/private/www.westos.com.key ##密钥
</virtualhost>
<Directory "/var/www/virtual/login.westos.com/html">             ##授权
       Require  all granted
</Directory>
<Virtualhost *:80>                                         ##网页重写实现自动访问https                           
               ServerName login.westos.com
               RewriteEngine on
               RewriteRule ^(/.*)$ https://%{HTTP_HOST}$1 [redirect=301]

</virtualhost>



##^(/.*)$ https://%{HTTP_HOST}$1 [redirect=301]
#^(/.*)$   客户主机在地址栏写入的所有字符,不包括换行符
#https://  定义生成的访问协议
#%{HTTP_HOST} 客户请求主机
#$1        $1的值就表示^(/.*)$的值
[root@mariadb conf.d]# mkdir  /var/www/virtual/login.westos.com/html -p  ##生成目录
[root@mariadb conf.d]# vim /var/www/virtual/login.westos.com/html/index.html ##写入浏览器要显示的内容
<h1>login.westos.com's page</h1>
[root@mariadb conf.d]# systemctl restart httpd.service


测试
1.在浏览器主机解析
vim /etc/hosts
172.25.254.234 www.westos.com news.westos.com money.westos.com login.westos.com

2.访问http://login.westos.com ,根据提示下载证书




完成后会自动挑转到 https://login.westos.com   实现网页数据加密传输



















原创粉丝点击