apache入门篇配置

1、apache的基本信息

apache为同步阻塞模式,稳定性高,nginx为异步非阻塞模式
apache为企业中常用的web服务,用来提供http://(超文本传输协议)
主配置目录:/etc/httpd/conf
主配置文件:/etc/httpd/conf/http.conf
子配置目录:/etc/httpd/conf.d
子配置文件:/etc/httpd/conf.d/*.conf
默认发布目录:/var/www/html/
默认发布文件:index.html
默认端口:80

2、安装部署apache

[root@localhost ~]# yum install httpd -y
[root@localhost ~]# systemctl restart httpd
[root@localhost ~]# netstat -antlupe | grep httpd
tcp6 0 0 :::80 ::: * LISTEN 0 36458 1714/httpd
[root@localhost ~]# cd /var/www/html/
[root@localhost html]# vim index.html
hello
浏览器测试:172.25.254.152
————————————————
版权声明:本文为CSDN博主「lin_made」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/lin_made/article/details/80502899
在这里插入图片描述

3、更改默认发布目录

[root@localhost html]# mkdir /linux/html/ -p
[root@localhost html]# cd /linux/html/
[root@localhost html]# vim index.html
[root@localhost html]# vim /etc/httpd/conf/httpd.conf
[root@localhost html]# systemctl restart httpd
#DocumentRoot “/var/www/html” #注释掉原来的默认发布目录
DocumentRoot “/linux/html” #写入新的发布目录
<Directory “/linux”>
require all granted

浏览器测试:172.25.254.152
在这里插入图片描述

4、更改默认发布文件

1、
[root@localhost html]# vim test.html
test
[root@localhost html]# vim /etc/httpd/conf/httpd.conf
DocumentRoot “/linux/html”
<Directory “/linux”>
require all granted
DirectoryIndex test.html #新的发布文件

浏览器测试:172.25.254.152
在这里插入图片描述
2、
[root@localhost html]# mkdir lin
[root@localhost html]# mv index.html lin/
[root@localhost html]# vim /etc/httpd/conf/httpd.conf
<Directory “/linux/html/lin”>
DirectoryIndex index.html

[root@localhost html]# systemctl restart httpd
浏览器测试:172.25.254.152/lin
在这里插入图片描述

5、更改默认端口

[root@localhost html]# vim /etc/httpd/conf/httpd.conf
listen 8080 #默认端口改为8080
[root@localhost html]# systemctl restart httpd
[root@localhost html]# netstat -antlupe | grep httpd
访问测试:172.25.254.152:8080
在这里插入图片描述

6、主机访问权限设置

[root@localhost html]# vim index.html
[root@localhost html]# mv index.html lin/
[root@localhost conf.d]# vim /etc/httpd/conf/httpd.conf
<Directory “/var/www/html/lin”>
order Allow,Deny #顺序为先读allow,再读deny
Allow from All
Deny from 172.25.254.155 #不允许172.25.254.155访问

[root@localhost html]# systemctl restart httpd
访问:172.25.254.152
在这里插入图片描述

7、用户访问权限设置

[root@localhost lin]# cd /etc/httpd/
[root@localhost httpd]# ls
conf conf.d conf.modules.d logs modules run
[root@localhost httpd]# htpasswd -cm apacheuser admin
New password:
Re-type new password:
Adding password for user admin
[root@localhost httpd]# htpasswd -m apacheuser tom
New password:
Re-type new password:
Adding password for user tom
[root@localhost httpd]# vim /etc/httpd/conf/httpd.conf
<Directory “/var/www/html/lin”>
AuthUserFile /etc/httpd/apacheuser
AuthName “Please input user and password!!”
AuthType basic
#Require user admin 允许admin
Require valid-user 允许所有用户

访问测试172.25.254.152

8、多节点测试(apache的虚拟主机)

真实主机:
[root@foundation40 ~]# vim /etc/hosts
172.25.254.152 www.linux.com news.linux.com music.linux.com login.linux.com #本地解析
虚拟机:
[root@localhost httpd]# cd conf.d/
[root@localhost conf.d]# vim default.conf

DocumentRoot /var/www/html
CustomLog “logs/default.log” combined

[root@localhost conf.d]# mkdir /var/www/virtual/linux.com/news -p #建立news的发布目录
[root@localhost conf.d]# mkdir /var/www/virtual/linux.com/music -p #建立music的发布目录
[root@localhost conf.d]# vim /var/www/virtual/linux.com/news/index.html
news
[root@localhost conf.d]# vim /var/www/virtual/linux.com/music/index.html
music
[root@localhost conf.d]# vim news.conf
<VirtualHost *:80>
ServerName news.linux.com
DocumentRoot “/var/www/virtual/linux.com/news”
CustomLog “logs/default.log” combined

<Directory “/var/www/virtual/westos.com/news/”>
Require all granted

[root@localhost conf.d]# vim music.conf
<VirtualHost *:80>
ServerName music.linux.com
DocumentRoot “/var/www/virtual/linux.com/music”
CustomLog “logs/default.log” combined

<Directory “/var/www/virtual/westos.com/music/”>
Require all granted

[root@localhost conf.d]# systemctl restart httpd
访问测试music.linux.com news.linux.com
在这里插入图片描述
在这里插入图片描述

9、证书生成

[root@localhost conf.d]# yum install mod_ssl -y
[root@localhost conf.d]# systemctl restart httpd
[root@localhost conf.d]# yum install crypto-utils.x86_64
[root@localhost conf.d]# genkey www.linux.com
[root@localhost conf.d]# vim ssl.conf
SSLCertificateFile /etc/pki/tls/certs/www.linux.com.crt
SSLCertificateKeyFile /etc/pki/tls/private/www.linux.com.key
访问:https://www.linux.com

10、网页重写

[root@localhost conf.d]# mkdir /var/www/virtual/linux.com/login -p
[root@localhost conf.d]# vim /var/www/virtual/linux.com/login/index.html
login
[root@localhost conf.d]# vim login.conf
<VirtualHost *:443>
ServerName login.linux.com
DocumentRoot “/var/www/virtual/linux.com/login”
CustomLog “logs/default.log” combined
SSLEngine on
SSLCertificateFile /etc/pki/tls/certs/www.linux.com.crt
SSLCertificateKeyFile /etc/pki/tls/private/www.linux.com.key

<Directory “/var/www/virtual/westos.com/login/”>
Require all granted

<VirtualHost :80>
ServerName login.linux.com
RewriteEngine on
RewriteRule ^(/.
)$ https://%{HTTP_HOST}$1 [redirect=301] #访问80端口时自动转为https

[root@localhost conf.d]# systemctl restart httpd
访问:login.linux.com

12、apache支持的语言

1、php
[root@localhost conf.d]# cd /var/www/html/
[root@localhost html]# vim index.php

<?php phpinfo(); ?>

[root@localhost html]# vim /etc/httpd/conf/httpd.conf
177 DirectoryIndex index.php index.html
[root@localhost html]# systemctl restart httpd
[root@localhost html]# yum install php -y
[root@localhost html]# systemctl restart httpd
访问172.25.254.152
在这里插入图片描述
2、cgi
[root@localhost html]# mkdir cgi
[root@localhost html]# vim cgi/index.cgi
#!/usr/bin/perl
print “Content-type: text/html\n\n”;
print date;
[root@localhost html]# cd /etc/httpd/conf.d/
[root@localhost conf.d]# chmod +x /var/www/html/cgi/index.cgi
[root@localhost conf.d]# vim default.conf

DocumentRoot /var/www/html
CustomLog “logs/default.log” combined

<Directory “/var/www/html/cgi”>
Options +ExecCGI
AddHandler cgi-script .cgi
DirectoryIndex index.cgi

[root@localhost conf.d]# systemctl restart httpd
访问:172.25.254.152/cgi
在这里插入图片描述

12、squid正向代理

在可以上网的主机
[root@foundation155 ~]# yum install squid -y
[root@foundation155 ~]# vim /etc/squid/squid.conf
http_access allow all
cache_dir ufs /var/spool/squid 100 16 256
[root@foundation155 ~]# systemctl restart squid.service
在不能上网的主机设置,测试

13、squid反向代理

在apache主机上安装部署apache
在squid主机
[root@lin ~]# yum install squid.x86_64
[root@lin ~]# vim /etc/squid/squid.conf
http_access allow all
http_port 80 vhost vport
cache_peer 172.25.254.152 parent 80 0 proxy-only
cache_dir ufs /var/spool/squid 100 16 256
[root@lin ~]# systemctl restart squid
第三台主机访问:172.25.254.252

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值