部署apache2并实现ssl自动跳转
1. YUM安装
2. 路径
httpd |
解释 |
/etc/httpd/ |
配置文件位置 |
/var/www/html/ |
站点目录位置 |
/var/log/httpd/ |
日志目录位置 |
/usr/sbin/httpd |
命令所在位置 |
3. 修改配置文件
| [root@ip-172-31-5-103 conf] |
| |
| |
| ServerRoot "/etc/httpd" |
| |
| Listen 80 |
| |
| Include conf.modules.d/*.conf |
| |
| User apache |
| Group apache |
| |
| ServerAdmin root@localhost |
| |
| ServerName cap.sinnet-cloud.cn:80 |
| |
| |
| <Directory /> |
| AllowOverride none |
| Require all granted |
| </Directory> |
| |
| |
| DocumentRoot "/var/www/sinnet-cloud.cn/html" |
| |
| |
| <Directory "/var/www"> |
| AllowOverride None |
| |
| Require all granted |
| |
| RewriteEngine on |
| RewriteCond %{SERVER_PORT} !^443$ |
| RewriteRule ^(.*)?$ https://%{SERVER_NAME}%{REQUEST_URI} [L,R] |
| |
| </Directory> |
| |
| |
| <Directory "/var/www/html"> |
| Options Indexes FollowSymLinks |
| AllowOverride None |
| Require all granted |
| </Directory> |
| |
| |
| <IfModule dir_module> |
| DirectoryIndex index.html |
| </IfModule> |
| |
| === |
| <Files ".ht*"> |
| Require all denied |
| </Files> |
| |
| |
| ErrorLog "logs/error_log" |
| |
| LogLevel warn |
| |
| <IfModule log_config_module> |
| |
| |
| |
| |
| LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined |
| LogFormat "%h %l %u %t \"%r\" %>s %b" common |
| |
| <IfModule logio_module> |
| |
| LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %I %O" combinedio |
| </IfModule> |
| |
| CustomLog "logs/access_log" combined |
| </IfModule> |
| |
| <IfModule alias_module> |
| |
| ScriptAlias /cgi-bin/ "/var/www/cgi-bin/" |
| |
| </IfModule> |
| |
| |
| <Directory "/var/www/cgi-bin"> |
| AllowOverride None |
| Options None |
| Require all granted |
| </Directory> |
| |
| <IfModule mime_module> |
| |
| TypesConfig /etc/mime.types |
| |
| AddType application/x-compress .Z |
| AddType application/x-gzip .gz .tgz |
| |
| AddType text/html .shtml |
| AddOutputFilter INCLUDES .shtml |
| </IfModule> |
| |
| AddDefaultCharset UTF-8 |
| |
| <IfModule mime_magic_module> |
| |
| MIMEMagicFile conf/magic |
| </IfModule> |
| |
| |
| EnableSendfile on |
| |
| |
| <IfModule mod_http2.c> |
| Protocols h2 h2c http/1.1 |
| </IfModule> |
| |
| |
| |
| IncludeOptional conf.d/*.conf |
| |
| IncludeOptional conf/vhost/*.conf |
| |
4. 配置虚拟主机
| [root@ip-172-31-5-103 conf] |
| |
| <VirtualHost *:80> |
| |
| DocumentRoot "/var/www/sinnet-cloud.cn/html" |
| |
| ServerName cap.sinnet-cloud.cn |
| |
| RewriteEngine on |
| |
| RewriteCond %{SERVER_PORT} !^443$ |
| |
| RewriteRule ^/?(.*)$ https://%{SERVER_NAME}/$1 [L,R] |
| 整句的意思是讲:启动rewrite模块,将所有访问非443端口的域名请求,url地址内容不变,将http://变成https://。 |
| </VirtualHost> |
5. 配置ssl
| |
| [root@ip-172-31-5-103 conf] |
| |
| |
| [root@ip-172-31-5-103 conf.d] |
| /etc/httpd/conf.d |
| [root@ip-172-31-5-103 conf.d] |
| total 24 |
| -rw-r--r--. 1 root root 2893 Jun 30 11:02 autoindex.conf |
| -rw-r--r--. 1 root root 366 Jun 30 11:02 README |
| -rw-r--r--. 1 root root 9423 Nov 15 04:42 ssl.conf |
| -rw-r--r--. 1 root root 1252 Jun 30 11:01 userdir.conf |
| |
| [root@ip-172-31-5-103 conf.d] |
| |
| <VirtualHost *:443> |
| |
| ErrorLog logs/ssl_error_log |
| TransferLog logs/ssl_access_log |
| LogLevel warn |
| SSLEngine on |
| SSLProtocol all -SSLv3 |
| SSLCipherSuite HIGH:MEDIUM:!aNULL:!MD5:!SEED:!IDEA |
| |
| |
| SSLCertificateFile /etc/digicert/cloud.cn.crt |
| SSLCertificateKeyFile /etc/digicert/zhengshu.key |
6. 设置站点目录权限并启动服务
| |
| [root@ip-172-31-5-103 conf.d] |
| |
| |
| [root@ip-172-31-5-103 conf.d] |
| |
| [root@ip-172-31-5-103 conf.d] |
| [root@ip-172-31-5-103 conf.d] |
| |
| |
| [root@ip-172-31-5-103 sinnet-cloud.cn] |
| tcp LISTEN 0 511 *:443 *:* users:(("httpd",pid=5161,fd=6),("httpd",pid=5160,fd=6),("httpd",pid=5154,fd=6),("httpd",pid=5112,fd=6),("httpd",pid=5111,fd=6),("httpd",pid=5110,fd=6),("httpd",pid=5109,fd=6),("httpd",pid=5108,fd=6),("httpd",pid=5106,fd=6)) |
| tcp LISTEN 0 511 *:80 *:* users:(("httpd",pid=5161,fd=4),("httpd",pid=5160,fd=4),("httpd",pid=5154,fd=4),("httpd",pid=5112,fd=4),("httpd",pid=5111,fd=4),("httpd",pid=5110,fd=4),("httpd",pid=5109,fd=4),("httpd",pid=5108,fd=4),("httpd",pid=5106,fd=4)) |
| |
| |