Linux下安装apache httpd以及httpd常见用法
一、Linux安装apache httpd
1、官网下载httpd源码包:(目前最新版为2.4.10)
2、安装apache httpd安装依赖的包:apr和apr-util
如果不安装这些包,在编译httpd时会报错:
可以到官网下载:http://apr.apache.org/download.cgi
安装apr
1 tar -zxvf apr-1.5.1.tar.gz 2 cd apr-1.5.1 3 ./configure 4 make&&make install 5 安装apr-util 6 tar -zxvf apr-util-1.5.4.tar.gz 7 cd apr-util-1.5.4 8 ./configure --prefix/usr/local/apr-util --with-apr=/usr/local/apr
3、解压编译安装
1 tar -zxvf httpd-2.4.10.tar.gz 2 cd httpd-2.4.10 3 ./configure --prefix=/usr/local/httpd --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util 4 make&&make install
二、apache的简单操作
它是使用一个apachectl的脚本来实现对apache的操作的(这个文件在apache安装目录下的bin目录中,我的安装目录为/usr/local/httpd
)
个人理解:apachectl的英文:apache controll
用法:apachectl -[k|f|t] 操作名
1、开启apache
/usr/local/httpd/bin/apachectl -k start
2、关闭apache
/usr/local/httpd/bin/apachectl -k stop
3、重启apache
- 第一种是:直接重启
1 /usr/local/httpd/bin/apachectl -k restart
- 第二种是:优雅重启(重启时如果apache繁忙,则等它闲下的时候再重启)
1 /usr/local/httpd/bin/apachectl -k graceful
4、指定apache配置文件
1 /usr/local/httpd/bin/apachectl -f /usr/local/httpd/conf/httpd.conf
5、检测apache配置文件语法是否正确
1 /usr/local/httpd/bin/apachectl -t
6、其他操作:
1 /usr/local/httpd/bin/apachectl -v #显示当前apache的版本号 2 /usr/local/httpd/bin/apachectl -V #显示编译时的配置
三、apache的简单配置:
(1)将apache开机启动
- 方法一:
1 cp /usr/local/apache/bin/apachectl /etc/init.d/httpd vi /etc/init.d/httpd
在#!/bin/sh后面加入下面两行
1 #chkconfig:345 85 15 2 #description: Start and stops the Apache HTTP Server.
1 chmod +x /etc/rc.d/init.d/httpd chkconfig --add httpd
然后可以用setup命令进入服务设置,设置为开机启动
- 方法二: 将服务加到
/etc/rc.d/rc.local
中vi /etc/rc.d/rc.local
添加以下内容
1 /usr/local/apache/bin/apachectl start
(2)修改apache的默认端口(为什么要修改apache的端口,假如我们还安装了另一种web服务器nginx,它占用了80端口,那我们就不能再使用80端口)
修改apache的配置文件,查找listen修改80为8080
修改ServerName 为localhost:8080
1 listen 8080 #大约在httpd.conf中的52行
增加一行:约在httpd.conf中的189行
1 ServerName localhost:8080
四、apache httpd配置虚拟主机
1、去掉加载配置虚拟主机文件的注释:约在463行:
1 Include conf/extra/httpd-vhosts.conf
2、进入虚拟主机配置文件,每个virtualHost段之间
- ServerAdmin为管理员邮箱(不需要)
- DocumentRoot 网站根目录 (必填项)
- ServerName 为服务器名称(一般是域名或IP)(必填项)
- ErrorLog 为错误日志位置
- CustomLog 为访问日志位置
- 比如我们有两个域名:
www.shixinke.com
和www.withec.com
分别指向到我的服务器网站的根目录:/web/www/shixinke和/web/www/withec
1 <VirtualHost *:8080> 2 DocumentRoot "/web/www/shixinke" 3 ServerName shixinke.com 4 ServerAlias www.shixinke.com 5 ErrorLog "logs/shixinke.com-error_log" 6 CustomLog "logs/shixinke.com-access_log" common 7 </VirtualHost> 8 <VirtualHost *:8080> 9 DocumentRoot "/web/www/withec" 10 ServerName withec.com 11 ErrorLog "logs/withec.com-error_log" 12 CustomLog "logs/withec.com-access_log" common 13 </VirtualHost>
五,apache中两个指令补充(配置中的指令)
1、alias:指定虚拟目录
1 alias /uploads /web/www/uploads;
这个指令可以指定当我们访问/uploads
这个目录时,实际访问的是/web/www/uploads
这个目录。
2、ServerAlias:如果我们有多个域名指向同一个网站,可以使用这个指令
1 ServerName withec.com; 2 ServerAlias withec.net;
转载于:http://www.shixinke.com/linux/apache-httpd