如何在Ubuntu 18.04lts上安装和配置Apache

本教程将在Ubuntu18.04LTS Linux操作系统上安装Apache Web服务器。

配置SSL虚拟主机

如果不需要SSL,可以跳过此步骤。但对于任何网站来说,安全性始终是最重要的。

默认的Apache https监听端口443。确保没有其他服务使用同一端口。现在,我们需要启用Apache ssl模块,这在默认情况下是禁用的。

sudo a2enmod ssl

在本教程中,我已经按照这些说明为我们的域生成了一个自签名的SSL证书。

然后创建一个新的虚拟主机文件:

sudo vim /etc/apache2/sites-available/example.com_ssl.conf

内容如下:

<VirtualHost *:443>
    ServerAdmin admin@example.com
    DocumentRoot /var/www/example.com
	
    ServerName example.com
    ServerAlias www.example.com
	
    <Directory /var/www/example.com>
           #Allowoverride all    ###Uncomment if required
    </Directory>

    SSLEngine on
    SSLCertificateFile /etc/pki/tls/certs/example.com.crt
    SSLCertificateKeyFile /etc/pki/tls/certs/example.com.key
	
    ErrorLog ${APACHE_LOG_DIR}/example.com_ssl-error.log
    CustomLog ${APACHE_LOG_DIR}/example.com_ssl-access.log combined
</VirtualHost>

以下是用于配置SSL virtualhost的三个选项:

SSLEngine-将其设置为“on”
SSLCertificateFile-设置SSL证书的路径
SSLCertificateKeyFile-这是用于生成SSL证书的私钥文件

使用以下命令启用虚拟主机并重新加载Apache服务:

sudo a2ensite example.com_ssl
sudo systemctl reload apache2.service

测试Apache安装程序

使用以下命令查看Ubuntu 18.04linux系统上安装的Apache版本。

apache2 -v

Server version: Apache/2.4.41 (Ubuntu)
Server built:   2019-10-15T19:53:42

现在使用服务器的IP地址或指向服务器IP的域访问Apache服务器。我们将在web浏览器上看到一个默认的Apache页面。这意味着Apache web服务器已经成功地安装在你的Ubuntu 18.04系统上。

总结

全部完成后,我们已经在Ubuntu 18.04仿生Linux系统上安装并保护了Apache服务器。

安全加固Apache服务器

编辑Apache安全配置文件

sudo vim /etc/apache2/conf-enabled/security.conf

这里有多个与安全相关的设置。添加或更新以下设置。这些设置对于生产服务器非常有用。

ServerTokens Prod
ServerSignature Off
TraceEnable Off
Header always append X-Frame-Options SAMEORoirN
Header always set X-XSS-Protection: "1; mode=block"
Header always set X-Content-Type-Options: "nosniff"
Header always set Strict-Transport-Security "max-age=31536000; includeSubDomains"
Header always edit Set-Cookie ^(.*)$ ;HttpOnly;Secure

现在编辑SSL配置文件。设置服务器范围的SSL协议和SSLCipherSuite,以使用安全密码服务网站。

sudo vim /etc/apache2/mods-enabled/ssl.conf

SSLProtocol -all +TLSv1.2
SSLCipherSuite HIGH:!aNULL:!MD5

进行更改后,重新启动Apache服务以应用新配置。

sudo systemctl reload apache2.service

管理Apache服务

Apache服务由systemctl命令行管理。安装后,使用以下命令检查Apache服务的状态。

sudo systemctl status apache2.service

下面是通过命令行停止、启动或重新启动Apache服务的其他命令。

sudo systemctl stop apache2.service
sudo systemctl start apache2.service
sudo systemctl restart apache2.service

创建新的VirtualHost

让我们在Apache服务器上创建第一个虚拟主机。在本教程中,我们使用的示例域名是”example.com”. 在这里,我们将在80号端口为example.com网站创建虚拟主机。

创建目录并创建index.html文件:

sudo mkdir -p /var/www/example.com
sudo echo "Welcome" > /var/www/example.com/index.html

然后创建Virtualhost配置文件:

sudo vim /etc/apache2/sites-available/example.com.conf

在配置文件中添加以下内容。

<VirtualHost *:80>
    ServerAdmin admin@example.com
    DocumentRoot /var/www/example.com
    ServerName example.com
    ServerAlias www.example.com
    <Directory /var/www/example.com>
           #Allowoverride all    ###Uncomment if required
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/example.com_error.log
    CustomLog ${APACHE_LOG_DIR}/example.com_access.log combined
</VirtualHost>

保存Virtualhost配置文件,然后启用Virtualhost并使用以下命令重新加载Apache服务:

sudo a2ensite example.com
sudo systemctl reload apache2.service

在Ubuntu 18.04上安装Apache

首先,通过SSH登录Ubuntu 18.04系统并更新Apt缓存。然后安装Apache2 HTTP服务器包,如下所示:

sudo apt update
sudo apt install apache2

要安装最新版本的Apache,请使用以下PPA。

sudo add-apt-repository ppa:ondrej/apache2
sudo apt update
sudo apt install apache2

日期:2019-05-19 01:26:18 来源:oir作者:oir