在Ubuntu上使用Apache2搭建一个web服务器

398 阅读1分钟

Apache2 web服务器的安装:

  1. 第一部选安装apache2
apt install apache2 -y
  1. 安装完毕后,我们检查是否启动了Apache服务
systemctl status apache2
  1. 我们再来看看一些操作Apache的常用命令
/etc/init.d/apache2 start	//启动Apache服务
/etc/init.d/apache2 stop	//停止Apache服务
/etc/init.d/apache2 restart	//重启Apache服务
  1. 下面我们就可以开始访问我们刚刚配置的Apache服务器了。打开浏览器,输入http://127.0.0.1/ (这是主机默认的IP地址)或者如果是云主机就输入云主机的IP地址。当我们看到下面的页面时变说明Apache2 服务已经成功在我们的服务器上工作了。 在这里插入图片描述

Apache修改网站根目录及默认参数

我们来试着修改web服务器的配置,让我们可以在自己搭建的web服务器上访问到自己规定的数据:

修改根目录

/etc/apache2/sites-available 中修改 000-default.conf 中的DocumentRoot /var/www/ 修改为想要的目录 比如:DocumentRoot /var/www/html/mainpage

执行命令:

vim /etc/apache2/sites-available/000-default.conf

修改000-default.conf中的DocumentRoot /var/www/,如下 ServerAdmin webmaster@localhost http://10.11.53.202/%e7%88%b1%e5%bf%83/ DocumentRoot /var/www/html // 一般我们默认为这个作为网站的根目录,如果站点目录不在/var/www下默认是没有权限的这是我们就要修改一下apache2下的apache2.conf文件了。打开apache2.conf

<Directory "/mnt/c/users/18451/Documents/study">
        Options all	
        Require all granted
</Directory>
  1. 接下来重启apache 即可
sudo apache2ctl -k restart