ubuntu apache2 https

本文详细介绍如何为Apache Web服务器配置SSL证书,包括启用SSL模块、创建自签名证书、配置Apache使用SSL及重启服务等步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. enable the module ssl by:

sudo a2enmod ssl

2.after you have enabled module ssl , you will have to restart the web server for the change to be recognized:

sudo service apache2 restart

now ,the web server is able to handle ssl

3. create a directory to place the certificate files that will be maked:

sudo mkdir /etc/apache2/ssl

4.create our key and certificate 

sudo openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout /etc/apache2/ssl/apache.key -out /etc/apache2/ssl/apache.crt

let's go over what this means :

  • openssl: This is the basic command line tool provided by OpenSSL to create and manage certificates, keys, signing requests, etc.
  • req: This specifies a subcommand for X.509 certificate signing request (CSR) management. X.509 is a public key infrastructure standard that SSL adheres to for its key and certificate managment. Since we are wanting to create a new X.509 certificate, this is what we want.
  • -x509: This option specifies that we want to make a self-signed certificate file instead of generating a certificate request.
  • -nodes: This option tells OpenSSL that we do not wish to secure our key file with a passphrase. Having a password protected key file would get in the way of Apache starting automatically as we would have to enter the password every time the service restarts.
  • -days 365: This specifies that the certificate we are creating will be valid for one year.
  • -newkey rsa:2048: This option will create the certificate request and a new private key at the same time. This is necessary since we didn't create a private key in advance. The rsa:2048 tells OpenSSL to generate an RSA key that is 2048 bits long.
  • -keyout: This parameter names the output file for the private key file that is being created.
  • -out: This option names the output file for the certificate that we are generating.

 

  when you hit "ENTER" , you will be asked some questions , answer it ....

  then, the key and certificate will be created and placed in the /etc/apache2/ssl directory

 

  now ! ! ! ! ! ! ! ! ! !  ! we will configure apache to use ssl

  open the file with your editor

  

sudo vi /etc/apache2/site-available/default-ssl.conf

what we should modify is the content with red

<IfModule mod_ssl.c>
    <VirtualHost _default_:443>
        ServerAdmin admin@example.com
        ServerName your_domain.com
        ServerAlias www.your_domain.com
        DocumentRoot /var/www/html
        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
        SSLEngine on
        SSLCertificateFile /etc/apache2/ssl/apache.crt
        SSLCertificateKeyFile /etc/apache2/ssl/apache.key
        <FilesMatch "\.(cgi|shtml|phtml|php)$">
                        SSLOptions +StdEnvVars
        </FilesMatch>
        <Directory /usr/lib/cgi-bin>
                        SSLOptions +StdEnvVars
        </Directory>
        BrowserMatch "MSIE [2-6]" \
                        nokeepalive ssl-unclean-shutdown \
                        downgrade-1.0 force-response-1.0
        BrowserMatch "MSIE [17-9]" ssl-unclean-shutdown
    </VirtualHost>
</IfModule>

save and exit the file when you finished

 

then we enable it by:

sudo a2ensite default-ssl.conf

restart our web server

sudo service apache2 restart

now you can test it in your explorer 

https://server_domain_name_or_IP:443

转载于:https://www.cnblogs.com/lwmp/p/6383181.html

### 如何在Ubuntu上配置Apache服务器启用HTTPS #### 安装必要的软件包 为了使 Apache 支持 HTTPS 协议,在 Ubuntu 上除了安装 Apache 本身之外,还需要确保已安装 `openssl` 和 `mod_ssl` 模块。通过下面的命令可以完成这些操作: ```bash sudo apt update && sudo apt install apache2 openssl libapache2-ssl -y ``` 这一步骤会更新本地仓库缓存并安装所需的组件[^1]。 #### 创建自签名证书(可选) 如果打算使用正式的SSL/TLS证书,则跳过此部分;如果是测试环境或内部网络应用,可以通过 OpenSSL 工具创建一个临时性的自签名证书文件: ```bash sudo mkdir /etc/ssl/private/ sudo chmod 700 /etc/ssl/private/ sudo openssl req -keyout /etc/ssl/private/apache-selfsigned.key \ -out /etc/ssl/certs/apache-selfsigned.crt ``` 上述命令将会引导用户输入一些关于组织的信息来生成一对密钥和对应的 X.509 证书[^3]。 #### 修改默认站点配置以支持 SSL 编辑 `/etc/apache2/sites-available/default-ssl.conf` 文件,确保其中包含了正确的路径指向之前创建好的私钥与公钥(.crt)文件的位置,并设置好 ServerName 参数: ```nginx <IfModule mod_ssl.c> <VirtualHost _default_:443> ServerAdmin webmaster@localhost DocumentRoot /var/www/html SSLEngine on SSLCertificateFile /etc/ssl/certs/apache-selfsigned.crt SSLCertificateKeyFile /etc/ssl/private/apache-selfsigned.key </VirtualHost> </IfModule> ``` 保存更改后退出编辑器[^4]。 #### 启用相关模块和服务 为了让 Apache 能够识别新添加的内容以及处理加密连接请求,需激活几个重要的功能模块: ```bash sudo a2enmod ssl sudo a2ensite default-ssl sudo systemctl restart apache2 ``` 最后重启 Apache 来加载新的配置项[^2]。 #### 配置防火墙允许 HTTPS 流量 假如系统启用了 UFW (Uncomplicated Firewall),那么应该开放第443号TCP端口以便外部设备能够访问到安全版本的服务: ```bash sudo ufw allow 'OpenSSH' sudo ufw allow 'Apache Full' sudo ufw reload ``` 以上就是整个过程的大致描述,现在应当可以在浏览器地址栏里键入 https://your_server_ip 或者域名来进行验证了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值