Ubuntu服务器编译安装Apache
Ubuntu服务器编译安装Apache1.安装编译工具和依赖sudoaptupdatesudoaptinstallbuild-essential libssl-dev zlib1g-dev-y2.下载源码wgethttps://downloads.apache.org/httpd/httpd-2.4.66.tar.gztar-zxvfhttpd-2.4.66.tar.gzcdhttpd-2.4.663.安装APR、APR-Util、PCRE库可能会提示①APRwgethttp://archive.apache.org/dist/apr/apr-1.7.0.tar.gztar-xzvfapr-1.7.0.tar.gzcdapr-1.7.0 ./configure--prefix/usr/local/aprmakemakeinstall②APR-Utilwgethttp://archive.apache.org/dist/apr/apr-util-1.6.1.tar.gztar-xzvfapr-util-1.6.1.tar.gzcdapr-util-1.6.1 configure--prefix/usr/local/apr-util --with-apr/usr/local/aprmakemakeinstall③PCRE库sudoaptinstalllibpcre3-dev-yls/usr/bin/pcre-config#检查是否存在pcre-config4.配置编译选项./configure--prefix/usr/local/apache2 --enable-so --enable-rewrite --enable-mods-sharedall --enable-ssl --with-ssl/usr/lib/x86_64-linux-gnu --with-apr/usr/local/apr --with-apr-util/usr/local/apr-util --with-pcre/usr参数说明#–prefix/usr/local/apache2指定安装路径避免和系统包冲突#–enable-so启用动态模块支持#–enable-rewrite启用URL重写功能#–enable-mods-sharedall启用所有模块#–enable-ssl启用SSL支持#–with-ssl指定SSL库路径5.编译安装makesudomakeinstall6.配置运行端口apache和nginx运行端口默认为80若同时运行需要修改运行端口vim/usr/local/apache2/conf/httpd.conf找到Listen 80并修改为其他端口Listen8080修改servernameServerName localhost:80807.启动Apache/usr/local/apache2/bin/apachectl-t#验证Syntax OK/usr/local/apache2/bin/apachectl start8.浏览器访问浏览器访问IP端口返回默认网页it works“说明apache已经成功启动http://IP:80809.修改网站目录路径这里已经把网站放在了/data/apache/html需要修改配置让apache访问vim/usr/local/apache2/conf/httpd.conf修改配置文件中的DocumentRoot路径DocumentRoot/data/apache/htmlDirectory/data/apache/htmlOptions Indexes FollowSymLinks AllowOverride None Require all granted/Directory10.重启apache并浏览器访问11.实用apache优化配置在httpd.conf文件中修改①Gzip压缩IfModule mod_deflate.cAddOutputFilterByType DEFLATE text/html text/css text/javascript application/javascript DeflateCompressionLevel6# 6是最佳平衡点/IfModule②启用缓存减小服务器压力IfModule mod_expires.cExpiresActive On ExpiresByType text/htmlaccess plus 1 hourExpiresByType image/jpegaccess plus 1 yearExpiresByType application/javascriptaccess plus 1 year/IfModule③优化KeepAlive减少连接建立时间KeepAlive On KeepAliveTimeout2MaxKeepAliveRequests100④隐藏服务器版本信息ServerTokens Prod ServerSignature Off⑤禁用不必要的HTTP方法LimitExcept GET POSTRequire all denied/LimitExcept⑥配置安全HTTP头部防御XSS、点击劫持HeadersetX-Content-Type-OptionsnosniffHeadersetX-Frame-OptionsSAMEORIGINHeadersetContent-Security-Policydefault-src self