Ubuntu服务器编译安装Apache
1.安装编译工具和依赖
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库(可能会提示)
①APR
wgethttp://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-Util
wgethttp://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-shared=all --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-shared=all:启用所有模块
#–enable-ssl:启用SSL支持
#–with-ssl:指定SSL库路径
5.编译安装
makesudomakeinstall6.配置运行端口
apache和nginx运行端口默认为80,若同时运行需要修改运行端口
vim/usr/local/apache2/conf/httpd.conf找到Listen 80并修改为其他端口
Listen8080修改servername
ServerName 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/html"<Directory"/data/apache/html">Options Indexes FollowSymLinks AllowOverride None Require all granted</Directory>10.重启apache并浏览器访问
11.实用apache优化配置(在httpd.conf文件中修改)
①Gzip压缩
<IfModule mod_deflate.c>AddOutputFilterByType DEFLATE text/html text/css text/javascript application/javascript DeflateCompressionLevel6# 6是最佳平衡点</IfModule>②启用缓存,减小服务器压力
<IfModule mod_expires.c>ExpiresActive On ExpiresByType text/html"access plus 1 hour"ExpiresByType image/jpeg"access plus 1 year"ExpiresByType application/javascript"access plus 1 year"</IfModule>③优化KeepAlive(减少连接建立时间)
KeepAlive On KeepAliveTimeout2MaxKeepAliveRequests100④隐藏服务器版本信息
ServerTokens Prod ServerSignature Off⑤禁用不必要的HTTP方法
<LimitExcept GET POST>Require all denied</LimitExcept>⑥配置安全HTTP头部(防御XSS、点击劫持)
HeadersetX-Content-Type-Options"nosniff"HeadersetX-Frame-Options"SAMEORIGIN"HeadersetContent-Security-Policy"default-src 'self'"