ubuntu 20.04 部署 apache2 http和https

安装 Apache2

sudo apt update
sudo apt install apache2

安装之后,apache2服务器已经启动,并且会随系统自动启动;

Apache2 配置

Apache配置文件在/etc/apache2目录下。

主配置文件是apache2.conf,其中引用了其他配置文件。

a2query命令可以获取服务器当前的运行配置。

环境变量

envvars文件中配置了Apache用到的环境变量,这些环境变量也可以在配置文件中引用。

模块配置

所有模块配置在mods-available目录下,启用后会在mods-enabled目录下建立符号链接;
启用模块用a2enmod命令,禁用模块用a2dismod命令。

监听端口配置

ports.conf 文件中配置了Apache服务器监听的端口,包括HTTP端口和HTTPS端口
默认包括HTTP端口80,如果加载了SSL或TLS模块,还会监听443端口。
除了这里的配置,各虚拟主机配置里也会配置其对应的端口。

其他可选配置

其他可选配置在conf-available目录下,启用的配置会链接到conf-enabled中。
启用配置用a2enconf命令,禁用配置用a2disconf命令。

网站(虚拟主机)配置

网站配置包含了HTTP配置和HTTPS配置,根据需要选择。
所有网站配置都放在sites-available子目录中,而启用的网站则会链接到sites-enabled目录下。
要启动网站可以用a2ensite命令,禁用网站则用a2dissite命令。

网站配置

HTTP网站配置

默认配置中已经启用了一个http网站000-default,
其中配置了网站主目录DocumentRoot /var/www/html等信息,可参考 000-default.conf 在 sites-available 目录下配置其他网站,配置后通过 a2ensite 命令启动。

配置后可以通过 http://127.0.0.1 访问
注意使用的地址要与配置文件中的ServerName匹配

HTTPS网站

启用HTTPS必须要加载ssl模块

a2enmod ssl
systemctl restart apache2

启动SSL网站

a2ensite default-ssl
systemctl reload apache2

这里使用的是默认的SSL网站配置default-ssl,与HTTP的区别在于启用了SSL引擎SSLEngine on,并且配置了SSL需要的证书和密钥,以及其他SSL运行需要的参数。

默认的SSL配置中使用的是自签名的SSL证书,在使用浏览器打开网站时会收到安全警告,在真正使用时需要申请自己的密钥和证书。

生成自签名证书:

If you install the ssl-cert package, a self-signed certificate will be
automatically created using the hostname currently configured on your computer.
You can recreate that certificate (e.g. after you have changed '/etc/hosts' or
DNS to give the correct hostname) as user root with:

	make-ssl-cert generate-default-snakeoil --force-overwrite

To create more certificates with different host names, you can use

	make-ssl-cert /usr/share/ssl-cert/ssleay.cnf /path/to/cert-file.crt

This will ask you for the hostname and place both SSL key and certificate in
the file '/path/to/cert-file.crt'. Use this file with the SSLCertificateFile
directive in the Apache config (you don't need the SSLCertificateKeyFile in
this case as it also contains the key). The file '/path/to/cert-file.crt'
should only be readable by root. A good directory to use for the additional
certificates/keys is '/etc/ssl/private'.

如何申请和使用CA颁发的证书可参考《Let’s Encrypt SSL 证书的申请与使用》

  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

飞花丝雨

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值