centos7系统从0搭建https的服务器

1、安装apache环境依赖

yum -y install mod_ssl openssl httpd

2、创建CA证书存放目录

mkdir /etc/httpd/ca

3、修改配置文件,在下面配置文件中修改下面对应的字段,如果是被#号键注释掉之后,就放开,如果已经是放开的,就不要保持原样。

vi /etc/httpd/conf.d/ssl.conf
      5 Listen 443 https
      
     59 DocumentRoot "/var/www/html"
     60 ServerName www.example.com:443

     64 ErrorLog logs/ssl_error_log
     65 TransferLog logs/ssl_access_log
     66 LogLevel warn
     
     70 SSLEngine on
     
     75 SSLProtocol all -SSLv2 -SSLv3
     
     80 SSLCipherSuite HIGH:3DES:!aNULL:!MD5:!SEED:!IDEA
     
     92 SSLCipherSuite RC4-SHA:AES128-SHA:HIGH:MEDIUM:!aNULL:!MD5
     93 SSLHonorCipherOrder on

    100 SSLCertificateFile /etc/pki/tls/certs/localhost.crt
    
    107 SSLCertificateKeyFile /etc/pki/tls/private/localhost.key


创建Apache的CA证书
1、进入存放证书的目录

cd /etc/httpd/ca/
2、创建自己的CA证书

[root@localhost ca]# openssl req -newkey rsa:4096 -nodes -sha256 -keyout apache.xyc.com.key -out apache.xyc.com.csr
Generating a 4096 bit RSA private key
.........................................................++
.................................................................................................................................................................++
writing new private key to 'apache.xyc.com.key'
-----
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [XX]:CN
State or Province Name (full name) []:GD
Locality Name (eg, city) [Default City]:SZ
Organization Name (eg, company) [Default Company Ltd]:SXF
Organizational Unit Name (eg, section) []:yf
Common Name(eg, your name or your server's hostname) []:apache.xyc.com
Email Address []:

上面代码中执行结果如下,会需要输入一些信息,就是位置以及公司信息,这些信息就填你已有的,如果不重要仅仅只是内部测试就随便填也无所谓,上面没填的就是可选的,我在测试中都输入为空。注:可以看到我写的是1000天,也就是大概三年的时间,自己可以随意设置!
上面执行结束之后会在当前目录下生成下面文件
ca.crt ca.key

3、生成CA证书签名请求

openssl req -newkey rsa:4096 -nodes -sha256 -keyout apache.xyc.com.key -out apache.xyc.com.csr
Generating a 4096 bit RSA private key
.........................................................++
.................................................................................................................................................................++
writing new private key to 'apache.xyc.com.key'
-----
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [XX]:CN
State or Province Name (full name) []:GD
Locality Name (eg, city) [Default City]:SZ
Organization Name (eg, company) [Default Company Ltd]:SXF
Organizational Unit Name (eg, section) []:yf
Common Name (eg, your name or your server's hostname) []:apache.xyc.com
Email Address []:

Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:
[root@localhost ca]# ls
apache.xyc.com.csr  apache.xyc.com.key  ca.crt  ca.key

输入同上个命令。
4、生成注册主机的证书

[root@localhost ca]# openssl x509 -req -days 365 -in apache.xyc.com.csr -CA ca.crt -CAkey ca.key -CAcreateserial -out apache.xyc.com.crt
Signature ok
subject=/C=CN/ST=GD/L=SZ/O=SXF/OU=yf/CN=apache.xyc.com
Getting CA Private Key

注:可以看到以上截图写的是365天,有的童鞋可能不太理解,简单说一下,现在代表的意思是证书三年有效期,而客户端使用认证期限是一年,当然了写1000天就和证书一样三年后失效。
上面执行完成之后会生成下面几个文件:
5、将证书拷贝到配置文件中路径相对应的目录

//进入证书目录
cd /etc/httpd/ca/
//拷贝证书到默认指定目录
cp apache.xyc.com.crt /etc/pki/tls/certs/
cp apache.xyc.com.key /etc/pki/tls/private/

6、重启Apache服务

systemctl restart httpd

7、测试
接下来可以直接使用域名在浏览器上登陆,显示如下图:
在这里插入图片描述如果你使用了你的服务器的域名能被dns转换成IP就可以像上面那样使用域名登陆,如果在你的客户端(就是使用浏览器所在的那台PC),在hosts将某个域名配置成你的服务器IP,你也可以使用域名像示例一样在浏览器上访问服务器。如果都没有,那就可以直接在浏览器使用IP访问你的服务器,比如:https://192.168.1.1,将后面的IP换成你自己的服务器IP。

  • 8
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1. 安装Apache Web服务器 在终端中输入以下命令安装Apache Web服务器: sudo yum install httpd 安装完成后,启动Apache Web服务器: sudo systemctl start httpd 设置Apache Web服务器系统启动时自动启动: sudo systemctl enable httpd 2. 配置防火墙 为了允许Apache Web服务器通过防火墙,需要打开HTTP和HTTPS的端口。 打开HTTP端口: sudo firewall-cmd --zone=public --add-service=http --permanent 打开HTTPS端口: sudo firewall-cmd --zone=public --add-service=https --permanent 重新加载防火墙配置: sudo firewall-cmd --reload 3. 配置虚拟主机 虚拟主机可以让您在同一台服务器上托管多个网站。 首先,创建一个新目录来存储虚拟主机的文件: sudo mkdir /var/www/example.com 然后,创建一个新的虚拟主机配置文件: sudo nano /etc/httpd/conf.d/example.com.conf 在文件中添加以下内容: <VirtualHost *:80> ServerAdmin admin@example.com ServerName example.com ServerAlias www.example.com DocumentRoot /var/www/example.com ErrorLog /var/log/httpd/example.com-error.log CustomLog /var/log/httpd/example.com-access.log combined </VirtualHost> 保存并关闭文件。重启Apache Web服务器以使更改生效: sudo systemctl restart httpd 4. 安装PHP 在终端中输入以下命令安装PHP: sudo yum install php 安装完成后,重新启动Apache Web服务器: sudo systemctl restart httpd 5. 安装MySQL 在终端中输入以下命令安装MySQL: sudo yum install mysql-server 安装完成后,启动MySQL服务器: sudo systemctl start mysqld 设置MySQL服务器系统启动时自动启动: sudo systemctl enable mysqld 6. 配置MySQL 运行以下命令以保护MySQL安装: sudo mysql_secure_installation 按照提示输入MySQL root密码并回答其他问题以完成安全性设置。 7. 安装phpMyAdmin 在终端中输入以下命令以安装phpMyAdmin: sudo yum install phpmyadmin 安装完成后,打开phpMyAdmin配置文件: sudo nano /etc/httpd/conf.d/phpMyAdmin.conf 在文件中找到以下行: Require ip 127.0.0.1 将其修改为: Require all granted 保存并关闭文件。重启Apache Web服务器以使更改生效: sudo systemctl restart httpd 8. 验证安装 打开Web浏览器并输入服务器的IP地址或域名。如果一切正常,您应该看到Apache默认欢迎页面。 尝试访问虚拟主机(例如example.com),如果一切正常,您应该看到虚拟主机的文件。 最后,尝试访问phpMyAdmin(例如http://example.com/phpmyadmin)。如果一切正常,您应该能够登录并查看MySQL数据库。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值