源码编译安装lamp_第1篇(apache)

来源:互联网 发布:linux关闭端口 编辑:程序博客网 时间:2024/06/07 03:51

准备:
Linux操作系统(本次使用的为CentOS 6.5)
Apache源码包和较新版本的apr和apr-util (本次使用的为httpd-2.4.20 , apr-1.5.2 , apr-util-1.5.4)
安装好编译环境:
yum groupinstall "Development Tools" "Additional Development"
依赖pcre-devel 可以直接yum -y install pcre-devel (安装中遇到若有依赖的程序包未安装可用此方法安装”yum -y install [程序包]-devel”

方法/步骤:
1
首先下载最新版的Apache源码包,因为2.4版的httpd需要较新版本的apr支持,所以需要先安装新版本的apr以及apr-util。

2
编译安装apr:

# tar jxvf apr-1.5.2.tar.bz2# cd apr-1.5.1# ./configure --prefix=/usr/local/apr# make && make install

3
编译安装apr-util:

# tar jxvf apr-util-1.5.4.tar.bz2# cd apr-util-1.5.4# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr# make && make install

4
编译安装httpd-2.4.20:

#tar jvxf httpd-2.4.20.tar.bz2# cd httpd-2.4.20# ./configure --prefix=/usr/local/apache \--sysconfdir=/etc/httpd --enable-so --enable-ssl \--enable-cgi --enable-rewrite --with-zlib --with-pcre \--with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util \--enable-modules=most# make && make install

./configure之后如果不确定配置成功没,可以输入echo $?来查看返回值,
如果返回值为0 则继续执行make && make install

5
Apache编译安装成功后安装路径为:/usr/local/apache
配置文件路径为:/etc/httpd/httpd.conf
然后为Apache添加服务脚本:

# cp /usr/local/apache/bin/apachectl  /etc/init.d/httpd添加进环境变量:# echo 'export PATH=$PATH:/usr/local/apache/bin' > \/etc/profile.d/httpd.sh# chmod +x /etc/profile.d/httpd.sh# source /etc/profile.d/httpd.sh

6
编辑配置文件:

# vim /etc/httpd/httpd.conf

添加 ServerName localhost
然后保存退出

7
然后执行服务脚本启动httpd :

# service httpd start

查看是否正常监听80端口:

# netstat -anpt | grep "80"

如果想确保正常访问网页请关闭SElinux和iptables防火墙
或者自行添加iptables规则,临时关闭关闭则如下。

#setenforce 0# service iptables stop

a.防火墙的问题:

/etc/init.d/iptables status会得到一系列信息,说明防火墙开着。/etc/init.d/iptables stop(暂时关闭)永久关闭防火墙:chkconfig --level 35 iptables off

b.防火墙的长久解决方法(允许80端口)如下:

/sbin/iptables -I INPUT -p tcp --dport 80 -j ACCEPT然后保存:/etc/rc.d/init.d/iptables save重启防火墙/etc/init.d/iptables restart

c.SELinux问题:
永久关闭SELinux

查看当前SELinux状态:/usr/sbin/sestatus -vgetenforce ##也可以用这个命令检查编辑/etc/selinux/config,找到SELINUX 行修改成为:SELINUX=disabled:# This file controls the state of SELinux on the system.# SELINUX= can take one of these three values:# enforcing - SELinux security policy is enforced.# permissive - SELinux prints warnings instead of enforcing.# disabled - No SELinux policy is loaded.**SELINUX=disabled** # SELINUXTYPE= can take one of these two values:# targeted - Only targeted network daemons are protected.# strict - Full SELinux protection.SELINUXTYPE=targeted重启系统。

Centos还提供了实时改变SELinux工作模式的工具,前提是SELinux没有被disabled掉的时候

/usr/sbin/setenforce 0 #使SELinux工作模式变成permissive模式/usr/sbin/setenforce 1 #使SELinux工作模式变成enforcing模式添加到系统默认启动脚本里面echo "/usr/sbin/setenforce 0" >> /etc/rc.local这样就可以实时控制SELinux的启用和不启用了。

三个参数介绍介绍
enforcing— The SELinux security policy is enforced.
permissive— The SELinux system prints warnings but does not enforce policy.
disabled— SELinux is fully disabled. SELinux hooks are disengaged from the kernel and the pseudo-file system is unregistered.
简单翻译:
enforcing模式:强制组织。
permissive模式:SELinux系统输出警告,但是不强制阻止程序运行。
disabled:

8
然后浏览器输入你的主机名或IP地址来访问你的web服务
如何浏览器出现了大大的 It works! 那么恭喜你!你的apache服务器搭建成功了,你的网页文档目录在 /usr/local/apache/htdocs 中

9
如果想把apache服务加入到开机自启,可以修改服务启动脚本:

# vim /etc/init.d/httpd在第二行下添加:# chkconfig:235 85 15     #description: This is apache server!加入开机自启:# chkconfig --add httpd

其中235代表在第2,3,5运行级别下会开机启动
85和15代表开机和关机的启动顺序,数值越大代表启动的优先级越低
后启动的一般要先关闭,参数可以随你的喜好调整
description后的内容可以随意写

10
对apache服务器的配置文件httpd.conf的简单介绍与设置:
配置文件路径为:/etc/httpd/httpd.conf
用vim编辑器打开这个文件,以下是各选项意义:

ServerRoot "/usr/local/apache"// Apache的安装目录ServerName localhost                // 服务名称,可以填主机名Listen 80                           // 服务监听端口,默认80LoadModule                          // 服务启动需要加载的模块User daemon                         // apache进程执行者Group deamon                        // 执行者的属组ServerAdmin you@example.com         // 管理员的邮箱地址DocumentRoot "/usr/local/apache/htdocs"     // 网站根目录ErrorLog "logs/error_log"            // Apache运行产生的错误日志LogLevel warn                       // 指定ErrorLog会记录的错误的级别DirectoryIndex index.html           // 网站目录索引文件

11
当设置完配置文件后,如果想知道配置是否正确可以使用:

# httpd -t 或者:# service httpd configtest

如果输出为:Syntax OK
则证明配置文件没有问题,然后就可以重启服务了:

# httpd -k restart 或者:# service httpd restart或者只提醒服务重新读取配置文件:# httpd -k graceful 或者:# service httpd graceful如果想关闭服务:# httpd -k stop 或者:# service httpd stop
1 0
原创粉丝点击