安装Apache
- Apache是一个基金会的名字,httpd才是我们要安装的软件包,早期它的名字就叫apache
- Apache官网www.apache.org
- wget http://mirrors.cnnic.cn/apache/httpd/httpd-2.4.29.tar.gz
- wget http://mirrors.cnnic.cn/apache/apr/apr-1.6.3.tar.gz
- wget http://mirrors.cnnic.cn/apache/apr/apr-util-1.6.1.tar.bz2
- apr和apr-util是一个通用的函数库,它让httpd可以不关心底层的操作系统平台,可以很方便地移植(从linux移植到windows)
- tar zxvf httpd-2.4.29.tar.gz
- tar zxvf apr-util-1.6.1.tar.bz2
- tar zxvf apr-1.6.3.tar.gz
- cd /usr/local/src/apr-1.6.3
- ./configure --prefix=/usr/local/apr
- make && make install
- cd /usr/local/src/apr-util-1.6.1
- ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
- make && make install
- cd /usr/local/src/httpd-2.4.29
- ./configure \ //这里的反斜杠是脱义字符,加上它我们可以把一行命令写成多行 --prefix=/usr/local/apache2.4
--with-apr=/usr/local/apr
--with-apr-util=/usr/local/apr-util
--enable-so
--enable-mods-shared=most - make && make install
- ls /usr/local/apache2.4/modules
- /usr/local/apache2.4/bin/httpd -M //查看加载的模块
Apache介绍
- Apache是一个基金会的名字,它最早就是httpd起家的,因为httpd使用的人很多,很流行,所以当时就以基金会的名字来命名的web服务软件 ,在早期的时候,名字就叫做Apache,而不是叫httpd,后来在http的2.0版本开始,就改名叫httpd,但是很多人还是习惯叫做Apache
- Apache的主流版本,在之前是 1.3版本比较流行,后来出了2.0版本,2.2版本,2.4版本,现在主流版本是 2.4版本
Apache(2.4版本)
- 2.2版本和2.4版本的区别
- 安装的方法不同,涉及到一个安依赖软件apr版本不一样
- apr和apr-util是一个通用的函数库,它让httpd可以不关心底层的操作系统平台,可以很方便地进行移植(从linux移植到windows)
- 2.2版本和2.4版本所依赖的apr版本是不同的
- 而centos系统,默认自带的apr,也就是yum安装的apr和2.4版本是不匹配的,所以无法使用yum安装的apr,所以需要自己去手动编译
- Apache2.4版本编译起来麻烦,就是因为需要手动编译 apr 和 apr-util 这两个包
安装过程
- 首先切换到/usr/local/src目录下
[root@hf-01 ~]# cd /usr/local/src
[root@hf-01 src]#
- 下载Apache 2.4源码包、apr、apr-util这三个包
wget http://mirrors.cnnic.cn/apache/httpd/httpd-2.4.29.tar.gz
wget http://mirrors.cnnic.cn/apache/apr/apr-1.6.3.tar.gz
wget http://mirrors.cnnic.cn/apache/apr/apr-util-1.6.1.tar.bz2
- 在下载完成后,查看下下载的包
[root@hf-01 src]# ls
apr-1.6.3.tar.gz httpd-2.4.29.tar.gz
apr-util-1.6.1.tar.bz2 mariadb-10.2.6-linux-glibc_214-x86_64.tar.gz
httpd-2.2.34 mysql-5.6.35-linux-glibc2.5-x86_64.tar.gz
httpd-2.2.34.tar.gz
[root@hf-01 src]#
- 解压下载的包,并查看
[root@hf-01 src]# tar zxvf httpd-2.4.29.tar.gz
[root@hf-01 src]# tar zxvf apr-1.6.3.tar.gz
[root@hf-01 src]# tar xjvf apr-util-1.6.1.tar.bz2
[root@hf-01 src]# ls
apr-1.6.3 httpd-2.2.34.tar.gz
apr-1.6.3.tar.gz httpd-2.4.29
apr-util-1.6.1 httpd-2.4.29.tar.gz
apr-util-1.6.1.tar.bz2 mariadb-10.2.6-linux-glibc_214-x86_64.tar.gz
httpd-2.2.34 mysql-5.6.35-linux-glibc2.5-x86_64.tar.gz
[root@hf-01 src]#
- 在解压完成后,首先安装apr
- 切换到/usr/local/src/apr-1.6.3,并安装apr
[root@hf-01 src]# cd apr-1.6.3
[root@hf-01 apr-1.6.3]# ./configure --prefix=/usr/local/apr
- 在安装的时候,有时会出现以下情况
- 只需要安装gcc包即可——>yum install -y gcc
[root@hf-01 apr-1.6.3]# ./configure --prefix=/usr/local/apr
checking build system type... x86_64-pc-linux-gnu
checking host system type... x86_64-pc-linux-gnu
checking target system type... x86_64-pc-linux-gnu
Configuring APR library
Platform: x86_64-pc-linux-gnu
checking for working mkdir -p... yes
APR Version: 1.6.3
checking for chosen layout... apr
checking for gcc... no
checking for cc... no
checking for cl.exe... no
configure: error: in `/usr/local/src/apr-1.6.3':
configure: error: no acceptable C compiler found in $PATH
See `config.log' for more details
[root@hf-01 apr-1.6.3]#
[root@hf-01 apr-1.6.3]# yum install -y gcc
- 在安装完成后,可以用 echo $? 检查是否安装成功
[root@hf-01 apr-1.6.3]# echo $?
0
[root@hf-01 apr-1.6.3]#
- 执行make && make install 命令
[root@hf-01 apr-1.6.3]# make && make install
- 查看下apr,会看到有四个目录
[root@hf-01 apr-1.6.3]# ls /usr/local/apr/
bin build-1 include lib
[root@hf-01 apr-1.6.3]#
- 安装apr-util-1.6.1,首先切换到/usr/local/src/apr-util-1.6.1目录下
[root@hf-01 apr-1.6.3]# cd /usr/local/src/apr-util-1.6.1
[root@hf-01 apr-util-1.6.1]#
- 安装apr-util
[root@hf-01 apr-util-1.6.1]# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
- 然后使用echo $?检查是否安装成功
[root@hf-01 apr-util-1.6.1]# echo $?
0
[root@hf-01 apr-util-1.6.1]#
- 然后make && make install
[root@hf-01 apr-util-1.6.1]# make && make install
- 问题
- 但是在执行make && make install,出现以下错误
xml/apr_xml.c:35:19: 致命错误:expat.h:没有那个文件或目录
#include <expat.h>
^
编译中断。
make[1]: *** [xml/apr_xml.lo] 错误 1
make[1]: 离开目录“/usr/local/src/apr-util-1.6.1”
make: *** [all-recursive] 错误 1
- 解决方法
- 是因为缺少了xml解释器,只需要yum安装 expat-devel 包即可
[root@hf-01 apr-util-1.6.1]# yum -y install expat-devel
- 这时再去执行make && make install 会正常执行
- --enable-so \ 表示支持动态扩展模块,Apache和PHP它们都会支持以一个模块的形式存在
- PHP是Apache的一个模块,这个模块实际上就是一个文件,就是 .so 后缀名的文件,那他以Apache的一个模块形式存在,所以Apache本身是一个进程,是一个服务,在这个进程里面,通过一些配置文件指定一个模块的路径,那就可以去调用模块。
- PHP模块是用来解析PHP的,执行PHP脚本的,所以就可以通过PHP脚本将PHP模块加载到进程里面去,加载到主进程里面去,
- 当它遇到PHP解析的需求时,它就会去调用这个模块,去执行一些操作
- 这些模块是一些独立的文件
- 而且还可以指定有哪些动态扩展的模块,需要加载哪些,这里指定是most
- --enable-mods-shared=most
- most,就是绝大多数,大多数会用到的模块,都会加载进来
- 查看/usr/local/apr-util/目录下,会看到生成三个子目录
[root@hf-01 apr-util-1.6.1]# ls /usr/local/apr-util/
bin include lib
[root@hf-01 apr-util-1.6.1]#
- 安装httpd-2.4.29,首先切换到httpd-2.4.29/
[root@hf-01 apr-util-1.6.1]# cd /usr/local/src/httpd-2.4.29/
[root@hf-01 httpd-2.4.29]#
- 安装httpd-2.4.29
[root@hf-01 httpd-2.4.29]# ./configure --prefix=/usr/local/apache2.4 --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util --enable-so --enable-mods-shared=most
configure: error: pcre-config for libpcre not found. PCRE is required and available from http://pcre.org/
- 解决方法:
- pcre是正则表达式的驱动库,支持正则表达式
- 可以先yum list |grep pcre 查看,因为是缺少库,所以只需要找带有 devel 或 lib 字符的,所以只需要安装pcre-devel包即可——>yum install -y pcre-devel
[root@hf-01 httpd-2.4.29]# yum list |grep pcre
pcre.x86_64 8.32-17.el7 @base
pcre-devel.x86_64 8.32-17.el7 @base
pcre.i686 8.32-17.el7 base
pcre-devel.i686 8.32-17.el7 base
pcre-static.i686 8.32-17.el7 base
pcre-static.x86_64 8.32-17.el7 base
pcre-tools.x86_64 8.32-17.el7 base
pcre2.i686 10.23-2.el7 base
pcre2.x86_64 10.23-2.el7 base
pcre2-devel.i686 10.23-2.el7 base
pcre2-devel.x86_64 10.23-2.el7 base
pcre2-static.i686 10.23-2.el7 base
pcre2-static.x86_64 10.23-2.el7 base
pcre2-tools.x86_64 10.23-2.el7 base
pcre2-utf16.i686 10.23-2.el7 base
pcre2-utf16.x86_64 10.23-2.el7 base
pcre2-utf32.i686 10.23-2.el7 base
pcre2-utf32.x86_64 10.23-2.el7 base
[root@hf-01 httpd-2.4.29]#
[root@hf-01 httpd-2.4.29]# yum install -y pcre-devel
- 使用echo $?检查是否安装成功,这里会看到成功安装
[root@hf-01 httpd-2.4.29]# echo $?
0
[root@hf-01 httpd-2.4.29]#
- 执行make && make install
[root@hf-01 httpd-2.4.29]# make && make install
collect2: error: ld returned 1 exit status
make[2]: *** [htpasswd] 错误 1
make[2]: 离开目录“/usr/local/src/httpd-2.4.29/support”
make[1]: *** [all-recursive] 错误 1
make[1]: 离开目录“/usr/local/src/httpd-2.4.29/support”
make: *** [all-recursive] 错误 1
- 解决方法:
- 删除源码包,重新解压,重新编译安装,重新做一遍即可
- 进入到/usr/local/src 目录下,删除源码包
- 接下来便会完成安装Apache
Apache安装完成后
- 在安装完成后,进入到/usr/local/apache2.4/目录下,并 ls 查看有哪些目录
[root@hf-01 httpd-2.4.29]# cd /usr/local/apache2.4/
[root@hf-01 apache2.4]# ls
bin build cgi-bin conf error htdocs icons include logs man manual modules
[root@hf-01 apache2.4]#
- 在以后会接触到的目录就是 bin目录,conf目录,htdocs目录
- /bin目录下,是可执行文件。也就是说,要启动一个服务,它就是在bin/httpd下面有一个文件或者命令,bin/httpd也是核心的二进制文件
[root@hf-01 apache2.4]# ls bin/httpd
bin/httpd
[root@hf-01 apache2.4]# ls -l bin/httpd
-rwxr-xr-x 1 root root 2334331 12月 15 00:01 bin/httpd
[root@hf-01 apache2.4]# du -sh bin/httpd
2.3M bin/httpd
[root@hf-01 apache2.4]#
[root@hf-01 apache2.4]# ls conf/
extra httpd.conf magic mime.types original
[root@hf-01 apache2.4]#
- htdocs目录,是存放了一个访问页。启动完httpd服务后,去访问网站,默认的网站会放到 htdocs/目录下
[root@hf-01 apache2.4]# ls htdocs/
index.html
[root@hf-01 apache2.4]#
- logs目录,就是日志相关的目录
- man目录,就是一些帮助文档
- modules扩展模块目录,所有模块都放到了modules目录下
[root@hf-01 apache2.4]# ls modules
httpd.exp mod_dbd.so mod_proxy_http.so
mod_access_compat.so mod_dir.so mod_proxy_scgi.so
mod_actions.so mod_dumpio.so mod_proxy.so
mod_alias.so mod_env.so mod_proxy_wstunnel.so
mod_allowmethods.so mod_expires.so mod_ratelimit.so
mod_auth_basic.so mod_ext_filter.so mod_remoteip.so
mod_auth_digest.so mod_file_cache.so mod_reqtimeout.so
mod_auth_form.so mod_filter.so mod_request.so
mod_authn_anon.so mod_headers.so mod_rewrite.so
mod_authn_core.so mod_include.so mod_sed.so
mod_authn_dbd.so mod_info.so mod_session_cookie.so
mod_authn_dbm.so mod_lbmethod_bybusyness.so mod_session_dbd.so
mod_authn_file.so mod_lbmethod_byrequests.so mod_session.so
mod_authn_socache.so mod_lbmethod_bytraffic.so mod_setenvif.so
mod_authz_core.so mod_lbmethod_heartbeat.so mod_slotmem_shm.so
mod_authz_dbd.so mod_log_config.so mod_socache_dbm.so
mod_authz_dbm.so mod_log_debug.so mod_socache_memcache.so
mod_authz_groupfile.so mod_logio.so mod_socache_shmcb.so
mod_authz_host.so mod_macro.so mod_speling.so
mod_authz_owner.so mod_mime.so mod_status.so
mod_authz_user.so mod_negotiation.so mod_substitute.so
mod_autoindex.so mod_proxy_ajp.so mod_unique_id.so
mod_buffer.so mod_proxy_balancer.so mod_unixd.so
mod_cache_disk.so mod_proxy_connect.so mod_userdir.so
mod_cache.so mod_proxy_express.so mod_version.so
mod_cache_socache.so mod_proxy_fcgi.so mod_vhost_alias.so
mod_cgid.so mod_proxy_fdpass.so mod_watchdog.so
mod_dav_fs.so mod_proxy_ftp.so
mod_dav.so mod_proxy_hcheck.so
[root@hf-01 apache2.4]# du -sh modules/
6.3M modules/
[root@hf-01 apache2.4]#
- 查看Apache加载了哪些模块
- /usr/local/apache2.4/bin/httpd -M //查看加载的模块
- /usr/local/apache2.4/bin/apachectl -M //查看加载的模块——>这是一个shell文件,它调用了二进制的httpd
[root@hf-01 apache2.4]# /usr/local/apache2.4/bin/httpd -M
AH00557: httpd: apr_sockaddr_info_get() failed for hf-01
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1. Set the 'ServerName' directive globally to suppress this message //这里仅仅是一个提示,提示你要去定义ServerName——>不用去管
Loaded Modules:
core_module (static)
so_module (static)
http_module (static)
mpm_event_module (static)
authn_file_module (shared)
authn_core_module (shared)
authz_host_module (shared)
authz_groupfile_module (shared)
authz_user_module (shared)
authz_core_module (shared)
access_compat_module (shared)
auth_basic_module (shared)
reqtimeout_module (shared)
filter_module (shared)
mime_module (shared)
log_config_module (shared)
env_module (shared)
headers_module (shared)
setenvif_module (shared)
version_module (shared)
unixd_module (shared)
status_module (shared)
autoindex_module (shared)
dir_module (shared)
alias_module (shared)
[root@hf-01 apache2.4]#
[root@hf-01 apache2.4]# /usr/local/apache2.4/bin/apachectl -M
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 192.168.0.104. Set the 'ServerName' directive globally to suppress this message
Loaded Modules:
core_module (static)
so_module (static)
http_module (static)
mpm_event_module (static)
authn_file_module (shared)
authn_core_module (shared)
authz_host_module (shared)
authz_groupfile_module (shared)
authz_user_module (shared)
authz_core_module (shared)
access_compat_module (shared)
auth_basic_module (shared)
reqtimeout_module (shared)
filter_module (shared)
mime_module (shared)
log_config_module (shared)
env_module (shared)
headers_module (shared)
setenvif_module (shared)
version_module (shared)
unixd_module (shared)
status_module (shared)
autoindex_module (shared)
dir_module (shared)
alias_module (shared)
[root@hf-01 apache2.4]#
- 在模块的右侧有小括号,里面是 static 或 shared ,static是静态
- 静态和动态的区别
- http是一个核心文件,这个文件加载了哪些模块
- 如果是static,那也就意味这个模块在httpd里面,和它绑定在了一起,它们是一个整体
- 如果是shared,说明它是一个扩展的模块,这个模块是一个文件,我们可以看到的 .so 文件,文件的目录是在/usr/local/apache2.4/module目录下
启动Apache2.4
- 在安装完成Apache2.4后,Apache启动不需要定义启动脚本,也不需要放到/etc/init.d下去,直接使用命令行启动就行
- /usr/local/apache2.4/bin/apachectl start //命令行启动Apache脚本
[root@hf-01 apache2.4]# /usr/local/apache2.4/bin/apachectl start
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 192.168.0.104. Set the 'ServerName' directive globally to suppress this message
[root@hf-01 apache2.4]#
- 查看Apache是否启动,看httpd进程是否存在
[root@hf-01 apache2.4]# ps aux |grep httpd
root 47977 0.0 0.2 71400 2188 ? Ss 05:09 0:00 /usr/local/apache2.4/bin/httpd -k start
daemon 47978 0.0 0.4 360364 4248 ? Sl 05:09 0:00 /usr/local/apache2.4/bin/httpd -k start
daemon 47979 0.0 0.4 360364 4248 ? Sl 05:09 0:00 /usr/local/apache2.4/bin/httpd -k start
daemon 47980 0.0 0.4 360364 4248 ? Sl 05:09 0:00 /usr/local/apache2.4/bin/httpd -k start
root 48071 0.0 0.0 112676 984 pts/0 R+ 05:12 0:00 grep --color=auto httpd
[root@hf-01 apache2.4]#
- 查看端口号——>httpd默认监听端口为80,mysqld默认监听端口为3306,25端口是发邮件的,22端口是远程登录的
[root@hf-01 apache2.4]# netstat -lntp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1498/master
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1190/sshd
tcp6 0 0 ::1:25 :::* LISTEN 1498/master
tcp6 0 0 :::3306 :::* LISTEN 1503/mysqld
tcp6 0 0 :::80 :::* LISTEN 47977/httpd
tcp6 0 0 :::22 :::* LISTEN 1190/sshd
[root@hf-01 apache2.4]#