如何在Ubuntu 20.04上安装Apache Web服务器[快速入门]

本指南详细介绍了如何在Ubuntu 20.04服务器上安装Apache Web服务器,包括更新软件包索引、安装Apache、调整防火墙规则以及设置虚拟主机,帮助你构建起自己的Web服务环境。
摘要由CSDN通过智能技术生成

介绍 (Introduction)

The Apache HTTP server is the most widely-used web server in the world. It provides many powerful features, including dynamically loadable modules, robust media support, and extensive integration with other popular software.

Apache HTTP服务器是世界上使用最广泛的Web服务器。 它提供了许多强大的功能,包括可动态加载的模块,强大的媒体支持以及与其他流行软件的广泛集成。

In this guide, we’ll explain how to install an Apache web server on your Ubuntu 20.04 server. For a more detailed version of this tutorial, please refer to How To Install the Apache Web Server on Ubuntu 20.04.

在本指南中,我们将说明如何在Ubuntu 20.04服务器上安装Apache Web服务器。 有关本教程的更多详细版本,请参阅如何在Ubuntu 20.04上安装Apache Web服务器

先决条件 (Prerequisites)

Before you begin this guide, you should have the following:

在开始本指南之前,您应该具备以下条件:

  • An Ubuntu 20.04 server and a regular, non-root user with sudo privileges. Additionally, you will need to enable a basic firewall to block non-essential ports. You can learn how to configure a regular user account and set up a firewall for your server by following our Initial Server Setup for Ubuntu 20.04 guide.

    Ubuntu 20.04服务器和具有sudo特权的常规非root用户。 此外,您将需要启用基本防火墙以阻止非必需端口。 您可以按照我们的Ubuntu 20.04初始服务器设置指南,了解如何配置常规用户帐户和为服务器设置防火墙。

When you have an account available, log in as your non-root user to begin.

如果您有可用的帐户,请以非root用户身份登录以开始。

第1步-安装Apache (Step 1 — Installing Apache)

Apache is available within Ubuntu’s default software repositories, so you can install it using conventional package management tools.

Ubuntu的默认软件存储库中提供了Apache,因此您可以使用常规的软件包管理工具进行安装。

Update your local package index:

更新您的本地软件包索引:

  • sudo apt update

    sudo apt更新

Install the apache2 package:

安装apache2软件包:

  • sudo apt install apache2

    须藤apt install apache2

步骤2 —调整防火墙 (Step 2 — Adjusting the Firewall)

Check the available ufw application profiles:

检查可用的ufw应用程序配置文件:

  • sudo ufw app list

    sudo ufw应用程序列表

   
   
Output
Available applications: Apache Apache Full Apache Secure OpenSSH

Let’s enable the most restrictive profile that will still allow the traffic you’ve configured, permitting traffic on port 80 (normal, unencrypted web traffic):

让我们启用限制性最强的配置文件,该配置文件仍将允许您配置的流量,并允许端口80上的流量(正常的未加密Web流量):

  • sudo ufw allow 'Apache'

    sudo ufw允许使用“ Apache”

Verify the change:

验证更改:

  • sudo ufw status

    sudo ufw状态

   
   
Output
Status: active To Action From -- ------ ---- OpenSSH ALLOW Anywhere Apache ALLOW Anywhere OpenSSH (v6) ALLOW Anywhere (v6) Apache (v6) ALLOW Anywhere (v6)

步骤3 —检查您的Web服务器 (Step 3 — Checking your Web Server)

Check with the systemd init system to make sure the service is running by typing:

通过键入以下内容,与systemd初始化系统一起检查以确保服务正在运行:

  • sudo systemctl status apache2

    sudo systemctl状态apache2

   
   
Output
apache2.service - The Apache HTTP Server Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor prese> Active: active (running) since Tue 2020-04-28 23:06:40 UTC; 56s ago Docs: https://httpd.apache.org/docs/2.4/ Main PID: 13785 (apache2) Tasks: 55 (limit: 1137) Memory: 5.3M CGroup: /system.slice/apache2.service ├─13785 /usr/sbin/apache2 -k start ├─13787 /usr/sbin/apache2 -k start └─13788 /usr/sbin/apache2 -k start

Access the default Apache landing page to confirm that the software is running properly through your IP address:

通过您的IP地址访问默认的Apache登录页面,以确认该软件运行正常:

http://your_server_ip

You should receive the default Ubuntu 20.04 Apache web page:

您应该收到默认的Ubuntu 20.04 Apache网页:

When using the Apache web server, you can use virtual hosts (similar to server blocks in Nginx) to encapsulate configuration details and host more than one domain from a single server. We will set up a domain called your_domain, but you should replace this with your own domain name. To learn more about setting up a domain name with DigitalOcean, please refer to our our Introduction to DigitalOcean DNS.

使用Apache Web服务器时,可以使用虚拟主机 (类似于Nginx中的服务器块)来封装配置详细信息,并在一台服务器中承载一个以上的域。 我们将建立一个名为your_domain的域,但是您应该用自己的域名替换它 。 要了解有关使用DigitalOcean设置域名的更多信息,请参阅我们的DigitalOcean DNS简介

Create the directory for your_domain:

your_domain创建目录:

sudo mkdir /var/www/your_domain

Assign ownership of the directory:

分配目录的所有权:

  • sudo chown -R $USER:$USER /var/www/your_domain

    须藤chown -R $ USER:$ USER / var / www / your_domain

The permissions of your web roots should be correct if you haven’t modified your unmask value, but you can make sure by typing:

如果您尚未修改unmask值,则您的Web根目录的权限应该正确,但是可以通过键入以下内容来确保:

  • sudo chmod -R 755 /var/www/your_domain

    须藤chmod -R 755 / var / www / your_domain

Create a sample index.html page using nano or your favorite editor:

使用nano或您喜欢的编辑器创建样本index.html页面:

  • nano /var/www/your_domain/index.html

    纳米/ var / www / your_domain /index.html

Inside, add the following sample HTML:

在其中,添加以下示例HTML:

/var/www/your_domain/index.html
/var/www/your_domain/index.html
<html>
    <head>
        <title>Welcome to Your_domain!</title>
    </head>
    <body>
        <h1>Success!  The your_domain virtual host is working!</h1>
    </body>
</html>

Save and close the file when you are finished.

完成后保存并关闭文件。

Make a new virtual host file at /etc/apache2/sites-available/your_domain.conf:

/etc/apache2/sites-available/ your_domain .conf一个新的虚拟主机文件:

  • sudo nano /etc/apache2/sites-available/your_domain.conf

    须藤纳米/ etc / apache2 / sites-available / your_domain .conf

Paste in the following configuration block, updated for our new directory and domain name:

粘贴在以下配置块中,为我们的新目录和域名更新:

/etc/apache2/sites-available/your_domain.conf
/etc/apache2/sites-available/your_domain.conf
<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    ServerName your_domain
    ServerAlias your_domain
    DocumentRoot /var/www/your_domain
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Save and close the file when you are finished.

完成后保存并关闭文件。

Enable the file with a2ensite:

使用a2ensite启用文件:

  • sudo a2ensite your_domain.conf

    须藤a2ensite your_domain .conf

Disable the default site defined in 000-default.conf:

禁用在000-default.conf定义的默认站点:

  • sudo a2dissite 000-default.conf

    须藤a2dissite 000-default.conf

Test for configuration errors:

测试配置错误:

  • sudo apache2ctl configtest

    须藤apache2ctl configtest

You should receive the following output:

您应该收到以下输出:


   
   
Output
Syntax OK

Restart Apache to implement your changes:

重新启动Apache以实施您的更改:

  • sudo systemctl restart apache2

    sudo systemctl重新启动apache2

Apache should now be serving your domain name. You can test this by navigating to http://your_domain, where you should receive something like this:

Apache现在应该提供您的域名。 您可以通过导航到http:// your_domain ,您应该在其中收到以下内容:

结论 (Conclusion)

Now that you have your web server installed, you have many options for the type of content to serve and the technologies you want to use to create a richer experience.

现在,您已经安装了Web服务器,对于要提供的内容类型以及要用来创建更丰富体验的技术,有了很多选择。

If you’d like to build out a more complete application stack, check out this article on How to configure a LAMP stack on Ubuntu 20.04.

如果您想构建更完整的应用程序堆栈,请查看有关如何在Ubuntu 20.04上配置LAMP堆栈的本文。

翻译自: https://www.digitalocean.com/community/tutorials/how-to-install-the-apache-web-server-on-ubuntu-20-04-quickstart

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值