一键安装apache-2.4.38脚本

[root@lamp scripts]# cat /etc/redhat-release 
CentOS Linux release 7.5.1804 (Core) 
[root@lamp scripts]# uname -r
3.10.0-862.el7.x86_64
[root@lamp scripts]# cat apache-2.4.38_install.sh 
#!/bin/bash

#安装依赖软件
yum -y install gcc gcc-c++ make wget zlib-devel openssl-devel perl perl-devel expat-devel

#编译安装apr-1.5.2
cd /usr/local/src/
wget -c http://archive.apache.org/dist/apr/apr-1.5.2.tar.gz
tar -xf apr-1.5.2.tar.gz
cd apr-1.5.2
./configure --prefix=/usr/local/apache/apr && make && make install

#编译安装apr-util
cd /usr/local/src/
wget -c http://archive.apache.org/dist/apr/apr-util-1.5.4.tar.gz
tar -xf apr-util-1.5.4.tar.gz
cd apr-util-1.5.4
./configure --prefix=/usr/local/apache/apr-util --with-apr=/usr/local/apache/apr && make && make install

#编译安装pcre
cd /usr/local/src/
wget -c https://jaist.dl.sourceforge.net/project/pcre/pcre/8.37/pcre-8.37.tar.gz
tar -xf pcre-8.37.tar.gz
cd pcre-8.37
./configure && make && make install

#编译安装apache-2.4.38
cd /usr/local/src/
wget -c https://mirrors.aliyun.com/apache/httpd/httpd-2.4.38.tar.gz
tar -xf httpd-2.4.38.tar.gz
cd httpd-2.4.38
./configure --prefix=/usr/local/apache --with-apr=/usr/local/apache/apr/bin/apr-1-config --with-apr-util=/usr/local/apache/apr-util/bin/apu-1-config --enable-module=so --enable-mods-shared=all --enable-deflate --enable-expires --enable-headers --enable-cache --enable-file-cache --enable-mem-cache --enable-disk-cache --enable-mime-magic --enable-authn-dbm --enable-vhost-alias --enable-so --enable-rewrite --enable-ssl --with-mpm=prefork
make && make install

#创建站点目录和拷贝首页文件
mkdir -p /var/www/html
mv /usr/local/apache/htdocs/index.html /var/www/html/
cd /usr/local/apache/conf/ && mv httpd.conf httpd.conf.bak

#配置httpd.conf和创建自定义的基于域名的虚拟主机
cat > /usr/local/apache/conf/httpd.conf << EOF
ServerRoot "/usr/local/apache"
ServerName 127.0.0.1:80
Listen 80
LoadModule authn_file_module modules/mod_authn_file.so
LoadModule authn_core_module modules/mod_authn_core.so
LoadModule authz_host_module modules/mod_authz_host.so
LoadModule authz_groupfile_module modules/mod_authz_groupfile.so
LoadModule authz_user_module modules/mod_authz_user.so
LoadModule authz_core_module modules/mod_authz_core.so
LoadModule access_compat_module modules/mod_access_compat.so
LoadModule auth_basic_module modules/mod_auth_basic.so
LoadModule reqtimeout_module modules/mod_reqtimeout.so
LoadModule filter_module modules/mod_filter.so
LoadModule mime_module modules/mod_mime.so
LoadModule log_config_module modules/mod_log_config.so
LoadModule env_module modules/mod_env.so
LoadModule headers_module modules/mod_headers.so
LoadModule setenvif_module modules/mod_setenvif.so
LoadModule version_module modules/mod_version.so
LoadModule unixd_module modules/mod_unixd.so
LoadModule status_module modules/mod_status.so
LoadModule autoindex_module modules/mod_autoindex.so
LoadModule dir_module modules/mod_dir.so
LoadModule alias_module modules/mod_alias.so
<IfModule unixd_module>
User daemon
Group daemon
</IfModule>
ServerAdmin 2570583786@qq.com
<Directory />
AllowOverride none
Require all denied
</Directory>
DocumentRoot "/var/www/html"
<Directory "/var/www/html">
Options FollowSymLinks
AllowOverride None
Require all granted
</Directory>
<IfModule dir_module>
DirectoryIndex index.html index.php
</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>
# You need to enable mod_logio.c to use %I and %O
LogFormat "%h %l %u %t "%r" %>s %b "%{Referer}i" "%{User-Agent}i" %I %O" combinedio
</IfModule>
CustomLog "logs/access_log" common
</IfModule>
<IfModule alias_module>
ScriptAlias /cgi-bin/ "/usr/local/apache/cgi-bin/"
</IfModule>
<IfModule cgid_module>
</IfModule>
<Directory "/usr/local/apache/cgi-bin">
AllowOverride None
Options None
Require all granted
</Directory>
<IfModule headers_module>
RequestHeader unset Proxy early
</IfModule>
<IfModule mime_module>
TypesConfig conf/mime.types
AddType application/x-compress .Z
AddType application/x-gzip .gz .tgz
AddType application/x-httpd-php .php 
AddType application/x-httpd-php-source .php5
</IfModule>
<IfModule proxy_html_module>
Include conf/extra/proxy-html.conf
Include conf/extra/httpd-mpm.conf
Include conf/extra/httpd-default.conf
Include conf/extra/httpd-vhosts.conf
</IfModule>
<IfModule ssl_module>
SSLRandomSeed startup builtin
SSLRandomSeed connect builtin
</IfModule>

<Directory "/var/www/html">
Options FollowSymLinks
AllowOverride None 
Order allow,deny
Allow from all
</Directory>
EOF

cat >> /usr/local/apache/conf/extra/httpd-vhosts.conf << EOF
<VirtualHost *:80>
ServerAdmin 2570583786@qq.com
DocumentRoot "/var/www/html"
ServerName www.test.cn
ServerAlias test.cn
ErrorLog "logs/www_log"
CustomLog "logs/www-access_log" common
</VirtualHost>
EOF

#检查语法,然后启动httpd服务
/usr/local/apache/bin/apachectl -t
/usr/local/apache/bin/apachectl start

注意:由于软件下载链接有可能会失效,上面一键安装apache-2.4.38脚本有可能会失败,需自行检查软件下载链接是否失效!

测试:在windows客户端的hosts文件中添加域名解析记录  10.0.0.3   www.test.cn ,然后打开浏览器测试首页链接成功!

原文地址:https://www.cnblogs.com/blog-tim/p/10536247.html