Apache2.4.25编译安装之虚拟主机For Centos7

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Apache2.4.25编译安装之虚拟主机For Centos7相关的知识,希望对你有一定的参考价值。

    安装之前准备:

  1. 配置好防火墙,selinux,时区.

  2. 检查系统是否安装了httpd服务

  3. yum install gcc gcc-c++ zlib-devel 

  4. 下载软件,pcre,apr,apr-util,apache

wget https://ftp.pcre.org/pub/pcre/pcre-8.40.tar.gz

wget http://mirror.bit.edu.cn/apache//apr/apr-1.5.2.tar.gz

wget http://mirror.bit.edu.cn/apache//apr/apr-util-1.5.4.tar.gz

wget http://apache.fayea.com/httpd/httpd-2.4.25.tar.gz


创建软件安装目录及下载目录

1.下载目录: /opt/dowanload

2.安装目录:

[[email protected] ~]# tree -L 1 /opt/app/

/opt/app/                      #主目录

├── apache -> /opt/app/apache2.4.25  #软连接,方便以后升级及管理

├── apache2.4.25               #apache安装目录,所有文件存放地

├── apr                     #apr 目录

├── apr-util                 #apr-util 目录

└── pcre                    #pcre 目录


编译安装依赖包

tar zxf apr apr-util pcre

先安装apr,apr-util依赖apr


#cd /opt/download

#cd  apr-1.5.2

#./configure --prefix=/opt/app/apr

#make && make install


#cd apr-util-1.5.4

#./configure --prefix=/opt/app/apr-util --with-apr=/opt/app/apr

#make && make install 


#cd pcre-8.40

#./configure --prefix=opt/app/pcre

#make && make install


开始编译安装apache

# tar zxf  httpd-2.4.25.tar.gz

# cd http-2.4.25

# ./configure --prefix=/opt/app/apache2.4.25 --with-apr=/opt/app/apr --with-apr-util=/opt/app/apr-util --with-pcre=/opt/app/pcre --with-mpm=worker --enable-rewrite --enable-so --enable-headers --enable-expires --enable-deflate

# make && make install


虚拟主机篇(先备份好需要改动的配置文件)

#软连接 ln -s /opt/app/apache2.4.25 /opt/app/apache

默认安装完成后有如下文件:

[[email protected] extra]# tree -L 1 /opt/app/apache

/opt/app/apache

├── bin    #启动目录

├── build                                  

├── cgi-bin

├── conf

├── error

├── htdocs

├── icons

├── include

├── logs

├── man

├── manual

└── modules


[[email protected] ~]# tree -L 1 /opt/app/apache/conf/

/opt/app/apache/conf/

├── extra    #此文件存放虚拟主机配置文件等           

├── httpd.conf #主配置文件

├── magic                             

├── mime.types                       

└── original


配置虚拟主机

[[email protected] extra]# tree -L 1 /opt/app/apache/conf/extra

/opt/app/apache/conf/extra

├── httpd-autoindex.conf

├── httpd-dav.conf

├── httpd-default.conf

├── httpd-info.conf

├── httpd-languages.conf

├── httpd-manual.conf

├── httpd-mpm.conf

├── httpd-multilang-errordoc.conf

├── httpd-ssl.conf

├── httpd-userdir.conf

├── httpd-vhosts.conf         #虚拟主机文件,

├── httpd-vhosts.conf.2017-04-05

└── proxy-html.conf


编辑虚拟主机文档,跟2.4版本前的稍微有些变动

[[email protected] extra]# egrep -v "^$|#"  /opt/app/apache/conf/extra/httpd-vhosts.conf

<VirtualHost *:80>                  

    ServerAdmin sadoc.blog.51cto.com      

    DocumentRoot "/var/www"             #web网站存放目录,请mkdir好目录

    ServerName bbs.52cto.com            #域名

    ServerAlias 52ctt.com              #域名别名

    ErrorLog "logs/bbs-error_log"         #错误日志

    CustomLog "logs/bbs-access_log" common   #客户端访问日志

</VirtualHost>

修改主配置文件

DocumentRoot "/opt/app/apache2.4.25/htdocs"

<Directory "/opt/app/apache2.4.25/htdocs">

    Options Indexes FollowSymLinks

    AllowOverride None

    Require all granted

</Directory>

#新增如下,不增加会报错403错误.

<Directory "/var/www">

   Options Indexes FollowSymLinks

   AllowOverride None

   Require all granted

</Directory>


#取消了vhost的加载注释

Include conf/extra/httpd-vhosts.conf


apache服务启动方式:

[[email protected] ~]# /opt/app/apache/bin/apachectl -t  #检查配置文件是否正常.

Syntax OK   

[[email protected] ~]# /opt/app/apache/bin/apachectl -k start  #启动服务


自己手动写启动脚本,方便启动.

脚本写的很简单,也可以用进程数量判断

[[email protected] ~]# cat /etc/init.d/httpd 


#!/bin/bash

#author:http://sadoc.blog.51cto.com/

#2017-04-06


#加载函数库

. /etc/rc.d/init.d/functions


#定义变量本脚本以Pid文件是否存在判断启动.

PID="/opt/app/apache/logs/httpd.pid"

STARTING="/opt/app/apache/bin/apachectl"


start(){

if [ ! -s "$PID" ];then

  $STARTING -k start

  action "Apache Start" /bin/true

fi


}


stop(){

if [ -s "$PID" ];then

  $STARTING -k stop

  action "Apache Stop:" /bin/true

fi


}


case $1 in


start)

start

;;

stop)

  stop

  

  ;;


status)

  status

  

  ;;

  

reload)

  reload

  

  ;;

 

esac

# chmod +x /etc/init.d/httpd 

用了脚本后,直接yum方式启动,centos7可以用systemctl管理启动

[[email protected] ~]# systemctl status httpd.service  

● httpd.service - (null)

   Loaded: loaded (/etc/rc.d/init.d/httpd; bad; vendor preset: disabled)

   Active: active (running) since Wed 2017-04-05 17:20:03 CST; 22h ago

   Docs: man:systemd-sysv-generator(8)

  Process: 3873 ExecStart=/etc/rc.d/init.d/httpd start (code=exited, status=0/SUCCESS)

   CGroup: /system.slice/httpd.service

           ├─3877 /opt/app/apache2.4.25/bin/httpd -k start

           ├─3879 /opt/app/apache2.4.25/bin/httpd -k start

           ├─3880 /opt/app/apache2.4.25/bin/httpd -k start

           ├─3881 /opt/app/apache2.4.25/bin/httpd -k start

           └─3963 /opt/app/apache2.4.25/bin/httpd -k start

Apr 05 17:20:03 bbs.52cto.com systemd[1]: Starting (null)...

Apr 05 17:20:03 bbs.52cto.com httpd[3873]: Apache Start [  OK  ]

Apr 05 17:20:03 bbs.52cto.com systemd[1]: Started (null).


# 查看服务端口

[[email protected] ~]# lsof -i:80

COMMAND  PID   USER   FD   TYPE DEVICE SIZE/OFF NODE NAME

httpd   3877   root    4u  IPv6  29554      0t0  TCP *:http (LISTEN)

httpd   3879 daemon    4u  IPv6  29554      0t0  TCP *:http (LISTEN)

httpd   3880 daemon    4u  IPv6  29554      0t0  TCP *:http (LISTEN)

httpd   3881 daemon    4u  IPv6  29554      0t0  TCP *:http (LISTEN)

httpd   3963 daemon    4u  IPv6  29554      0t0  TCP *:http (LISTEN)

[[email protected] ~]# ps -ef | grep httpd

root           3877      1  0 Apr05 ?           00:00:03 /opt/app/apache2.4.25/bin/httpd -k start

daemon     3879   3877  0 Apr05 ?        00:00:00 /opt/app/apache2.4.25/bin/httpd -k start

daemon     3880   3877  0 Apr05 ?        00:00:00 /opt/app/apache2.4.25/bin/httpd -k start

daemon     3881   3877  0 Apr05 ?        00:00:00 /opt/app/apache2.4.25/bin/httpd -k start

daemon     3963   3877  0 Apr05 ?        00:00:00 /opt/app/apache2.4.25/bin/httpd -k start

root           6074   2380  0 15:45 pts/0   00:00:00 grep --color=auto httpd


查PID文件

[[email protected] ~]# cat /opt/app/apache/logs/httpd.pid 

3877




以上是关于Apache2.4.25编译安装之虚拟主机For Centos7的主要内容,如果未能解决你的问题,请参考以下文章

开发人员学Linux:CentOS7编译安装Subversion1.9.5及Apache2.4.25并集成

Linux二进制安装apache2.4.25

Mac配置apache2.4.25服务器

开发人员学Linux:CentOS7编译安装Git-2.13.1及使用

php-7.0.16 , apache2.4.25 配置

开发环境——基于RHEL 6.8开发环境的搭建