ubuntu 20.04 部署 apache2 http和https

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6

安装 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'.
阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6
标签: Ubuntu