Linux系统应用:Web服务器(Apache2)的简单配置

常用的Web服务器有Apache、IIS、 Tomcat 、Nginx、Lighttpd、IBM Websphere等,其中应用最广泛的是Linux下的Apache(Apache也应用在Windows平台下),是世界使用排名第一的Web服务器软件;它可运行在几乎所有广泛使用的计算机平台上,由于其跨平台和安全性被广泛使用,是最流行的Web服务器端软件之一。而Windows平台下最常用的Web服务器是IIS。

防火墙配置命令ufw

安装ufw

apt-get install ufw

常用命令

ufw enable			开启防火墙
ufw disable			关闭防火墙
ufw status			查看防火墙状态
ufw allow [port]	允许端口port被外部访问
ufw deny [port]		拒绝端口port被外部访问

安装Apache

命令行安装

apt-get install apache2

查看是否安装成功

dpkg -l | grep apache

访问Apache服务器:

(1)在终端执行ifconfig命令查看Linux系统的IP地址(如192.168.0.1),并打开浏览器输入localhost或者http://192.168.0.1查看Apache服务器是否可以访问

(2)若在外部访问(如Windows系统),需要在终端将防火墙关闭(或者允许Apache的默认端口80被外部访问),在浏览器输入Linux系统的IP地址,查看Apache服务器是否可以被访问

配置Apache

(一)默认网站首页存放主目录路径

配置文件路径:/etc/apache2/sites-available/000-default.conf

参数:DocumentRoot [path]

# 000-default.conf

ServerAdmin webmaster@localhost
DocumentRoot /home/serein/Desktop/Apache

同时要在/etc/apache2/apache2.conf中将该目录路径授权,否则访问时会出现没有权限的错误。

# apache2.conf

<Directory [path]>
	AllowOverride None
	Require all granted
</Directory>

(二)默认网站首页

配置文件路径:/etc/apache2/mods-available/dir.conf

参数:DirectoryIndex [file1] [file2] …

可以对应多个文件,若前面的网页找不到则往后查找

# dir.conf

<IfModule mod_dir.c>
	DirectoryIndex index.html index.cgi index.pl index.php index.xhtml index.htm
</IfModule>

(三)端口号

配置文件路径:/etc/apache2/ports.conf

参数:Listen [port]

# If you just change the port or add more ports here, you will likely also
# have to change the VirtualHost statement in
# /etc/apache2/sites-enabled/000-default.conf

Listen 80

配置文件路径:/etc/apache2/site-available/000-default.conf

参数:<VirtualHost *:[port]>

# 000-default.conf

<VirtualHost *:777>
	# The ServerName directive sets the request scheme, hostname and port that
	# the server uses to identify itself. This is used when creating
	# redirection URLs. In the context of virtual hosts, the ServerName
	

两个配置文件都要修改,监听端口号才会生效!

(四)虚拟目录

配置文件路径:/etc/apache2/apache2.conf

参数:Alias [虚拟目录] [物理目录]

虚拟目录的作用是隐藏真实的物理路径,输入虚拟路径即可访问对应物理路径的内容

# apache2.conf

# 虚拟目录
Alias /mytest /home/test_index

# 对物理路径授权
<Directory /home/test_index>
	AllowOverride None
	Require all granted
</Directory>

查看自定义配置后的的服务器首页

1.使用ufw命令允许你配置的监听端口号被外部访问(或直接关闭防火墙)。

2.重启Apache服务。

service apache2 restart

3.在Windows系统或其他主机的浏览器中输入Linux系统的IP地址及你配置的端口号(默认为80)。
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/Dae_Lzh/article/details/124668458