Apache服务器搭建教程
Apache(全称:Apache HTTP Server)是一款广泛使用的开源Web服务器软件,它以其稳定性、安全性和性能而著称。本文将详细介绍如何在Linux系统上搭建一个简单的Apache服务器。
一、准备工作
1. 安装Linux操作系统(推荐使用Ubuntu或Debian)。
2. 下载并安装Apache服务器。访问Apache官方网站(http://httpd.apache.org/)下载对应版本的Apache源码包。
3. 准备Apache配置文件(通常位于`/etc/httpd/conf.d/`目录下)。根据需要修改文件内容,例如添加虚拟主机、设置监听端口等。
二、配置环境变量
1. 编辑`/etc/profile`文件,添加以下内容:
```bash
export httpD_HOME=/usr/local/apache2
export httpD_MAKE_PREFIX=/usr/local/apache2
export httpD_BINARY_URL=https://mirrors.tuna.tsinghua.edu.cn/apache24/binaries/apache-2.4.55-1_linux-x86_64
export httpD_BUILD_DATE=$(date +%s)
export httpD_BUILD_NUMBER=$(echo $httpD_BUILD_DATE | cut -f1 -d' ' | tr '[:upper:]' '[:lower:]')
export httpD_VERSION=$(grep -o 'version' /usr/local/apache2/bin/httpd.version | cut -d'=' -f1)
export httpD_ARCH=$(grep -o 'buildarch' /usr/local/apache2/bin/httpd.version | cut -d'=' -f1)
export httpD_MODULES=$(grep -o 'modules' /usr/local/apache2/bin/httpd.version | cut -d'=' -f1)
export httpD_PROJECT_URL=$(grep -o 'projectURL' /usr/local/apache2/bin/httpd.version | cut -d'=' -f1)
export httpD_SITE_NAME=$(grep -o 'sitename' /usr/local/apache2/bin/httpd.version | cut -d'=' -f1)
export httpD_SITE_TAGLINE=$(grep -o 'sitetagline' /usr/local/apache2/bin/httpd.version | cut -d'=' -f1)
export httpD_SITE_DESC=$(grep -o 'sitedesc' /usr/local/apache2/bin/httpd.version | cut -d'=' -f1)
export httpD_SITE_LINK=$(grep -o 'sitelink' /usr/local/apache2/bin/httpd.version | cut -d'=' -f1)
export httpD_SITE_DIR=$(grep -o 'dir' /usr/local/apache2/bin/httpd.version | cut -d'=' -f1)
export httpD_SITE_ROOT=$(grep -o 'root' /usr/local/apache2/bin/httpd.version | cut -d'=' -f1)
export httpD_SITE_PATH=$(grep -o 'path' /usr/local/apache2/bin/httpd.version | cut -d'=' -f1)
export httpD_SITE_URL=$(grep -o 'url' /usr/local/apache2/bin/httpd.version | cut -d'=' -f1)
export httpD_SITE_PORT=$(grep -o 'port' /usr/local/apache2/bin/httpd.version | cut -d'=' -f1)
export httpD_SITE_SSL_PORT=$(grep -o 'sslPort' /usr/local/apache2/bin/httpd.version | cut -d'=' -f1)
export httpD_SITE_SSL_KEY='$(grep 'key' /usr/local/apache2/bin/httpd.conf | cut -d'=' -f2)'
export httpD_SITE_SSL_CERT='$(grep 'cert' /usr/local/apache2/bin/httpd.conf | cut -d'=' -f2)'
export httpD_SITE_SSL_CA='$(grep 'ca' /usr/local/apache2/bin/httpd.conf | cut -d'=' -f2)'
export httpD_SITE_SSL_CA_BUNDLE='$(grep 'caBundle' /usr/local/apache2/bin/httpd.conf | cut -d'=' -f2)'
```
2. 在`/etc/environment`文件中添加以下内容:
```bash
HTTPD_HOME="/usr/local/apache2"
HTTPD_MAKE_PREFIX="/usr/local/apache2"
HTTPD_BINARY_URL="https://mirrors.tuna.tsinghua.edu.cn/apache24/binaries/apache-2.4.55-1_linux-x86_64"
HTTPD_BUILD_DATE=$(date +%s)
HTTPD_BUILD_NUMBER=$(echo $HTTPD_BUILD_DATE | cut -f1)
HTTPD_VERSION=$(grep -o 'version' /usr/local/apache2/bin/httpd.version | cut -d'=' -f1)
HTTPD_ARCH=$(grep -o 'buildarch' /usr/local/apache2/bin/httpd.version | cut -d'=' -f1)
HTTPD_MODULES=$(grep -o 'modules' /usr/local/apache2/bin/httpd.version | cut -d'=' -f1)
HTTPD_PROJECT_URL=$(grep -o 'projectURL' /usr/local/apache2/bin/httpd.version | cut -d'=' -f1)
HTTPD_SITE_NAME=$(grep -o 'sitename' /usr/local/apache2/bin/httpd.version | cut -d'=' -f1)
HTTPD_SITE_TAGLINE=$(grep -o 'sitetagline' /usr/local/apache2/bin/httpd.version | cut -d'=' -f1)
HTTPD_SITE_DESC=$(grep -o 'sitedesc' /usr/local/apache2/bin/httpd.version | cut -d'=' -f1)
HTTPD_SITE_LINK=$(grep -o 'sitelink' /usr/local/apache2/bin/httpd.version | cut -d'=' -f1)
HTTPD_SITE_DIR=$(grep -o 'dir' /usr/local/apache2/bin/httpd.version | cut -d'=' -f1)
HTTPD_SITE_ROOT=$(grep -o 'root' /usr/local/apache2/bin/httpd.version | cut -d'=' -f1)
HTTPD_SITE_PATH=$(grep -o 'path' /usr/local/apache2/bin/httpd.version | cut -d'=' -f1)
HTTPD_SITE_URL=$(grep -o 'url' /usr/local/apache2/bin/httpd.version | cut -d'=' -f1)
HTTPD_SITE_PORT=$(grep -o 'port' /usr/local/apache2/bin/httpd.version | cut -d'=' -f1)
HTTPD_SITE_SSL_PORT=$(grep -o 'sslPort' /usr/local/apache2/bin/httpd.version | cut -d'=' -f1)
HTTPD_SITE_SSL_KEY='$(grep 'key' /usr/local/apache2/bin/httpd.conf | cut -d'=' -f2)'
HTTPD_SITE_SSL_CERT='$(grep 'cert' /usr/local/apache2/bin/httpd.conf | cut -d'=' -f2)'
HTTPD_SITE_SSL_CA='$(grep 'ca' /usr/local/apache2/bin/httpd.conf | cut -d'=' -f2)'
HTTPD_SITE_SSL_CA_BUNDLE='$(grep 'caBundle' /usr/local/apache2/bin/httpd.conf | cut -d'=' -f2)'
```
3. 重启Apache服务:
```bash
sudo systemctl restart httpd
```
4. 检查Apache服务状态:
```bash
sudo systemctl status httpd
```
如果看到“Active: active (running)”字样,说明Apache服务已经启动成功。
三、配置虚拟主机
1. 编辑`/etc/apache2/mods-available`目录中的`000-default.conf`文件,添加以下内容:
```ini
ServerAdmin webmaster@localhost
DocumentRoot "/var/www/html"
ErrorLog "/var/log/apache2/error.log"
CustomLog "/var/log/apache2/access.log" combined
```
2. 编辑`/etc/apache2/sites-available`目录中的`example.com`文件,添加以下内容:
```ini
ServerAdmin webmaster@localhost
ServerName example.com
DocumentRoot "/var/www"
ErrorLog "/var/log/apache2/error.log"
CustomLog "/var/log/apache2/access.log" combined
```
3. 重启Apache服务:
```bash
sudo systemctl restart httpd
```
4. 访问`http://localhost:80`,查看是否出现“example.com”页面。
至此,Apache服务器搭建完成。你可以根据需要继续配置其他虚拟主机和模块。