一、下載源碼包
(1)下載Apache源碼包
Apache官網下載:http://httpd.apache.org/download.cgi
(2)下載Apache依賴包apr和apr-util
(3)安裝剩下的其他依賴包
[root@localhost ~]# yum install -y gcc gcc-c++ make cmake
二、開始安裝
(1)首先安裝Apache的依賴包 apr包
我為了方便管理,我把軟件都安裝在/usr/local/mysoftware的目錄下
[root@localhost src]# tar -zxvf apr-1.5.2.tar.gz #解壓apr包
[root@localhost src]# cd apr-1.5.2 #进入目录 编译解压的原文件
[root@localhost apr-1.5.2]# ./configure --prefix=/usr/local/mysoftware/apr/ #配置自己要安装目录
[root@localhost apr-1.5.2]# make && make install
(2)安裝Apache的依賴包 apr-util包
[root@localhost src]# tar -zxvf apr-util-1.5.4.tar.gz #解壓apr-util
[root@localhost src]# cd apr-util-1.5.4 #进入目录 编译解压的原文件
[root@localhost apr-util-1.5.4]# ./configure --prefix=/usr/local/mysoftware/apr-util/ --with-apr=/usr/local/mysoftware/apr/
[root@localhost apr-util-1.5.4]# make && make install
(3)安裝Apache
[root@localhost src]# tar -zxvf httpd-2.4.25.tar.gz
[root@localhost src]# cd httpd-2.4.25
[root@localhost httpd-2.4.25]# ./configure --prefix=/usr/local/mysoftware/apache/ --with-apr=/usr/local/mysoftware/apr/ --with-apr-util=/usr/local/mysoftware/apr-util/
#記住原先安裝的apr目錄和apr-util目錄
#看看有沒有報pcre錯誤,如果報錯了要安裝一個PCRE,我自己安裝過了,沒有報錯。
[root@localhost src]# wget http://jaist.dl.sourceforge.net/project/pcre/pcre/8.40/pcre-8.40.tar.gz
[root@localhost src]# tar -zxvf pcre-8.40.tar.gz
[root@localhost src]# cd pcre-8.40
[root@localhost pcre-8.40]# ./configure --prefix=/usr/local/mysoftware/pcre/
[root@localhost pcre-8.40]# make && make install
從新再編譯Apache
[root@localhost httpd-2.4.25]# ./configure --prefix=/usr/local/mysoftware/apache/ \
--with-apr=/usr/local/mysoftware/apr/ \
--with-apr-util=/usr/local/mysoftware/apr-util/ \
--with-pcre=/usr/local/mysoftware/pcre/
[root@localhost httpd-2.4.25]# make && make install
三、安裝成功測試Apache
(1)开启 Apache 服务
[root@localhost~]# cd /usr/local/mysoftware/apache/bin/
[root@localhost bin]# ./apachectl start #停止用stop
#如果出現以下這個問題
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using localhost.localdomain. Set the 'ServerName' directive globally to suppress this message
#解決上面的問題
[root@localhost bin]# cd /usr/local/mysoftware/apache/conf/
[root@localhost conf]# vim httpd.conf
找到ServerName,把裡面的#去掉 或者改為 ServerName localhost:80 都可以的
再從新啟啟Apache
[root@localhost~]# cd /usr/local/mysoftware/apache/bin/
[root@localhost bin]# ./apachectl start #沒有出現問題證明啟動成功了
[root@localhost conf]# ps -aux|grep httpd
root 4274 0.0 0.6 407512 12400 ? Ss 10:47 0:00 /usr/local/mysoftware/apache//bin/httpd -k start
daemon 4275 0.0 0.2 297164 4712 ? S 10:47 0:00 /usr/local/mysoftware/apache//bin/httpd -k start
daemon 4276 0.0 0.7 903428 13864 ? Sl 10:47 0:00 /usr/local/mysoftware/apache//bin/httpd -k start
daemon 4277 0.0 0.6 706756 11572 ? Sl 10:47 0:00 /usr/local/mysoftware/apache//bin/httpd -k start
daemon 4281 0.0 0.6 706756 11572 ? Sl 10:47 0:00 /usr/local/mysoftware/apache//bin/httpd -k start
root 4415 0.0 0.3 152012 5700 pts/1 T 10:50 0:00 vim httpd.conf
root 4538 0.0 0.0 112664 968 pts/1 S+ 11:02 0:00 grep --color=auto httpd
####啟動成功了
(2)關閉防火墻
centos7後面的版本使用systemctl替代了chkconfig管理服務,防火墙iptables變成了firewalld
首先用systemctl关闭防火墙:
systemctl stop firewalld.service
systemctl disable firewalld.service
(3)也可以安裝一個iptables來管理
- 3.1 安装iptable和iptable-service
#先检查是否安装了iptables
[root@localhost ~]# service iptables status
#如果安裝的,可以更新為最新版本的iptables
[root@localhost ~]# yum update iptables
#安装iptables
[root@localhost ~]# yum install -y iptables
#安装iptables-services
[root@localhost ~]# yum install iptables-services
- 3.1 禁用/停止自带的firewalld服务
#停止firewalld服务
[root@localhost ~]# systemctl stop firewalld.service
#禁用firewalld系统重启firewalld服务不会加载
[root@localhost ~]# systemctl disable firewalld.service
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
Removed symlink /etc/systemd/system/basic.target.wants/firewalld.service.
- 3.2 添加規則 iptables
[root@localhost ~]# vim /etc/sysconfig/iptables
#以後要用的端口就添加在這裡
-A INPUT -p tcp -m state --state NEW -m tcp --dport 80 -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 3306 -j ACCEPT
#保存上述规则
[root@localhost ~]# service iptables save
- 3.3开启iptables服务
###注册iptables的服务
[root@localhost ~]# systemctl enable iptables.service
###开启服务
[root@localhost ~]# systemctl start iptables.service
###查看iptables的状态
[root@localhost ~]# systemctl status iptables.service
● iptables.service - IPv4 firewall with iptables
Loaded: loaded (/usr/lib/systemd/system/iptables.service; enabled; vendor preset: disabled)
Active: active (exited) since 二 2017-06-06 11:27:45 CST; 6s ago
Process: 6894 ExecStart=/usr/libexec/iptables/iptables.init start (code=exited, status=0/SUCCESS)
Main PID: 6894 (code=exited, status=0/SUCCESS)
6月 06 11:27:45 localhost.localdomain systemd[1]: Starting IPv4 firewall with iptables...
6月 06 11:27:45 localhost.localdomain iptables.init[6894]: iptables: Applying firewall rules: [ 确定 ]
6月 06 11:27:45 localhost.localdomain systemd[1]: Started IPv4 firewall with iptables.
三、用瀏覽器通過ip訪問Apache
出現
It Works!代表 服务器已成功启动,成功安裝了Apache
如果想查看運行目錄或者指定運行目錄,到Apache配置文件那裡修
/usr/local/mysoftware/apache/htdocs 這個是他默認的目錄,也可以放在這裡
[root@localhost ~]# vim /usr/local/mysoftware/apache/conf/httpd.conf
我這裡指定了、home/www,一定要有這個目錄,否則運行失敗,還有要給權限
四、设置开启自启动
源码编译安装apache,是不能使用service这个命令来启动的,通常我们启动的命令是:
[root@localhost httpd-2.2.16]# /usr/local/mysoftware/apache24/bin/apachectl start
/usr/local/apache2/bin/中的apachectl其实就是一个启动脚本,我们把他copy到/etc/init.d/去,并且重命名为apache(这个名字随便取,你自己方便就行)
[root@localhost ~]# cp /usr/local/mysoftware/apache24/bin/apachectl /etc/init.d/apache(这个是你启动的名字)
然后修改2个地方,让他支持service和chkconfig命令
[root@localhost ~]# vim /etc/init.d/apache
在前面一段注释中任意地方加入这2行:
# chkconfig: 35 20 80
# description: apache
第一行后面的3个数字的意思分别是:在哪些运行级别启动apache(3,5);启动序号(S20);关闭序号(K80)。
3和5也就是说在第三启动级别和第五启动级别的时候会默认启动apache
20就是指系统起来的时候有很多的服务需要启动,而这个程序排在第二十位启动,以此类推
80就是指系统关闭的时候,这个服务顺序排在第80位关闭注意:这2行缺一不可,#号不能省略,一定要有,否则出问题。
然后保存退出,这样我们就可以用service来启动和关闭apache
如下:
[root@localhost ~]# service apache2 start
[root@localhost bin]# netstat -antp|grep 80
tcp6 0 0 :::80 :::* LISTEN 37331/httpd
如果想让apache跟随系统一起启动,也就是开机自启动,那么加入到chkconfig即可
[root@localhost ~]# chkconfig --add apache
[root@localhost ~]# chkconfig apache on