本文共 5797 字,大约阅读时间需要 19 分钟。
操作系统:Centos 8
web应用:apache 内网IP:192.168.3.21 shell执行:root
以root或具有sudo权限的用户身份登录执行如下操作。
主配置文件路径:/etc/httpd/conf/httpd.conf
[root@linuxcool ~]# yum install httpd-devel.x86_64 httpd.x86_64 httpd-tools.x86_64 -y
验证httpd是否安装成功
[root@linuxcool ~]# httpd -vServer version: Apache/2.4.6 (CentOS)Server built: Apr 2 2020 13:13:23
启动httpd服务
[root@linuxcool ~]# systemctl start httpd.service[root@linuxcool ~]# systemctl status httpd.service● httpd.service - The Apache HTTP Server Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled) Active: active (running) since Fri 2020-05-22 12:23:03 CST; 25s ago Docs: man:httpd(8) man:apachectl(8) Main PID: 952 (httpd) Status: "Total requests: 0; Current requests/sec: 0; Current traffic: 0 B/sec" Tasks: 6 Memory: 3.6M CGroup: /system.slice/httpd.service ├─952 /usr/sbin/httpd -DFOREGROUND ├─953 /usr/sbin/httpd -DFOREGROUND ├─954 /usr/sbin/httpd -DFOREGROUND ├─955 /usr/sbin/httpd -DFOREGROUND ├─956 /usr/sbin/httpd -DFOREGROUND └─957 /usr/sbin/httpd -DFOREGROUNDMay 22 12:23:03 linuxcool systemd[1]: Starting The Apache HTTP Server...May 22 12:23:03 linuxcool httpd[952]: AH00558: httpd: Could not reliably determine the server's fully qualified domain name, u...messageMay 22 12:23:03 linuxcool systemd[1]: Started The Apache HTTP Server.Hint: Some lines were ellipsized, use -l to show in full.
http://192.168.3.21/
#cd /var/www/html#touch index.html#vi index.html输入以下内容:Welcome
This is apache Site!
打开浏览器,访问站点地址即可见:
#ln –s /home/user/public_html user
这样我们就可以通过 http://192.168.5.1/user 的方式访问用户页面了。
创建网站目录
[root@linuxcool ~]# mkdir -p /var/www/web1[root@linuxcool ~]# mkdir -p /var/www/web2
web1添加index.html
[root@linuxcool ~]# vim /var/www/web1/index.htmlWelcome to Web1 Success! Web1 home page!
web2添加index.html
[root@linuxcool ~]# vim /var/www/web2/index.htmlWelcome to Web1 Success! Web1 home page!
授权web目录权限
[root@linuxcool ~]# chttps://mp.weixin.qq.com/s/cEPsHaFWnuNgSXDjmXcSYQ[root@linuxcool ~]# chown -R apache: /var/www/web2/
创建虚拟主机文件
#web1[root@linuxcool ~]# vim /etc/httpd/conf.d/web1.confServerName web1.comServerAlias www.web1.comServerAdmin webmaster@example.comDocumentRoot /var/www/web1 Options -Indexes +FollowSymLinksAllowOverride AllErrorLog /var/log/httpd/example.com-error.logCustomLog /var/log/httpd/example.com-access.log combined
#web2
[root@linuxcool ~]# vim /etc/httpd/conf.d/web2.confServerName web2.comServerAlias www.web2.comServerAdmin webmaster@example.comDocumentRoot /var/www/web2 Options -Indexes +FollowSymLinksAllowOverride AllErrorLog /var/log/httpd/example.com-error.logCustomLog /var/log/httpd/example.com-access.log combined
重启httpd服务
[root@linuxcool ~]# systemctl restart httpd.service
验证httpd虚拟主机结果
http://www.web1.com1、设置静态文件过期时间为1天
ExpiresActive on ExpiresBytype text/css "access plus 1 days" ExpiresByType text/javascript "access plus 1 days" ExpiresByType application/x-javascript "access plus 1 days" ExpiresByType image/* "access plus 1 days"
2、开启gzip压缩
DeflateCompressionLevel 6 AddOutputFilterByType DEFLATE text/html AddOutputFilterByType DEFLATE text/css AddOutputFilterByType DEFLATE text/javascript AddOutputFilterByType DEFLATE application/javascript AddOutputFilterByType DEFLATE application/x-javascript BrowserMatch ^Mozilla/4 gzip-only-text/html BrowserMatch ^Mozilla/4\.0[678] no-gzip BrowserMatch \bMSIE !no-gzip !gzip-only-text/html #SetEnvIfNoCase Request_URI .(?:html|htm)$ no-gzip dont-varySetEnvIfNoCase #SetEnvIfNoCase Request_URI .(?:gif|jpe?g|png)$ no-gzip dont-vary SetEnvIfNoCase Request_URI .(?:exe|t?gz|zip|bz2|sit|rar)$ no-gzip dont-vary SetEnvIfNoCase Request_URI .(?:pdf|doc)$ no-gzip dont-vary
3、配置/upload附件目录
#不代理upload路径ProxyPass /upload !
4、访问附件目录/upload,不要提示403禁止,提示404不存在,防止泄露站点结构。
#访问/upload路径不提示403禁止错误,提示404不存在。
RedirectMatch 404 ^/upload/$
5、禁用TRACE方法
修改httpd.conf文件,添加如下代码,重启。
TraceEnable off
6、设置首页重定向
RewriteEngine onRewriteCond %{ REQUEST_URI} ^/$RewriteRule ^/$ /sso/ [R=permanent,L]
7、设置proxy超时时间
Timeout 600ProxyTimeout 600
程序目录:/usr/local/apache2
配置
主配置文件:/usr/local/apache2/conf/httpd.conf虚拟主机配置:/usr/local/apache2/conf/vhost
一个项目一个域名一个配置。
增加域名
脚本:/root/vhost.shroot@ubuntu:~# /root/vhost.shPlease input domain:(Default domain: www.cyyun.com):
运行脚本,输入需要添加的域名例如www.baidu.com,结束后会在/usr/local/apache2/conf/vhost生成www.baidu.com.conf配置文件。配合tomcat修改以下两段的IP和端口。
ProxyPass / ajp://192.168.122.100:8019/ProxyPassReverse / ajp://l92.168.122.100:8019/
新增域名添加解析后网址类似如http://www.baidu.com:10080/。网站目录是/home/wwwroot/www.cyyun.com,日志是/var/log/apache2/www.baidu.com-access_log。
重启apache2
/etc/init.d/apache2 restart
参考链接 :
在CentOS 8上安装与配置Apache虚拟主机 :https://mp.weixin.qq.com/s/cEPsHaFWnuNgSXDjmXcSYQRHEL7 源码安装Apache服务 https://mp.weixin.qq.com/s/2C2PLJ3SKLNbKvjav5P1Dg
转载地址:http://vtxi.baihongyu.com/