Apache简介
主要特点
- 开放源代码、跨平台应用
- 支持多种页面编程语言
- 模块化设计、运行稳定、良好的安全性
软件版本
1.X
- 目前最高版本时1.3、运行稳定
- 向下兼容性较好,但缺乏一些较新的功能
2.X
- 目前最高版本是2.4
- 具有更多的功能特性
编译安装httpd服务器
编译安装的优点
- 具有较大的自由度,功能可定制
- 可及时获得最新的软件版本
- 普遍适用于大多数Linux版本,便于移植使用
获得Apache服务器的源码包
Welcome to The Apache Software Foundation!
编译安装Apache的步骤
[root@bogon ~]# rpm -qa | grep httpd
[root@bogon ~]# yum -y install gcc gcc-* make
[root@bogon ~]# curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo yum不能使用的情况下 执行这条命令
[root@bogon ~]# yum -y install apr-util-devel pcre-devel zlib-devel
上传httpd-2.4.25.tar.gz 包
[root@bogon ~]# md5sum httpd-2.4.25.tar.gz 校验码
24fb8b9e36cf131d78caae864fea0f6a httpd-2.4.25.tar.gz
[root@bogon ~]# tar zxf httpd-2.4.25.tar.gz -C /usr/src/ 解压
[root@bogon ~]# cd /usr/src/httpd-2.4.25/ 切换目录
[root@bogon httpd-2.4.25]# ./configure --prefix=/usr/local/httpd --enable-so --enable-rewrite --enable-charset-lite --enable-cgi
--prefix:指定将httpd 服务程序安装到哪个目录下,如/usr/local/httpd。
--enable-so:启用动态加载模块支持,使 httpd 具备进一步扩展功能的能力。
--enable-rewrite:启用网页地址重写功能,用于网站优化及目录迁移维护。
--enable-charset-lite:启用字符集支持,以便支持使用各种字符集编码的网页。--enable-cgi:启用CGI 脚本程序支持,便于扩展网站的应用访问能力。
[root@bogon httpd-2.4.25]# make && make install
[root@bogon httpd-2.4.25]# echo $?
0
[root@bogon httpd-2.4.25]# ln -s /usr/local/httpd/bin/* /usr/local/bin 软连接
[root@bogon ~]# apachectl 启动
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1. Set the 'ServerName' directive globally to suppress this message
[root@bogon ~]# netstat -nlpt | grep 80 查看端口号
tcp6 0 0 :::80 :::* LISTEN 18137/httpd
[root@bogon ~]# curl http://192.168.73.129:80 访问端口
<html><body><h1>It works!</h1></body></html>
[root@bogon ~]# apachectl stop 关闭端口
root@bogon ~]#
[root@bogon ~]# cat > /lib/systemd/system/httpd.service << 'EOF'
[Unit]
Description=The Apache HTTP Server
After=network.target
[Service]
Type=forking
PIDFile=/usr/local/httpd/logs/httpd.pid
ExecStart= /usr/local/bin/apachectl $OPTIONS
ExecrReload= /bin/kill -HUP $MAINPID
[Install]
WantedBy=multi-user.target
EOF
[root@bogon ~]# systemctl start httpd 启动服务
[root@bogon ~]# systemctl status httpd 查看服务的状态
[root@bogon ~]# systemctl enable httpd 设置开机自启动
Created symlink from /etc/systemd/system/multi-user.target.wants/httpd.service to /usr/lib/systemd/system/httpd.service.
[root@bogon ~]# systemctl is-enabled httpd 验证是否为开机自启动
enabled
[root@bogon ~]#
[root@bogon ~]# cd /usr/local/httpd/htdocs/ 默认首页
[root@bogon htdocs]# ls
index.html
[root@bogon htdocs]#
构建虚拟主机
虚拟Web主机
在通一台服务器中运行多个Web站点,其中每一个站点并不独立占用一台真正的计算机
httpd支持的虚拟主机类型
- 基于域名的虚拟主机
- 基于ip地址的虚拟主机
- 基于端口的虚拟主机
虚拟主机提供域名解析
[root@bogon ~]# mkdir -p /var/www/html/kgccom
[root@bogon ~]# mkdir -p /var/www/html/kccecom
[root@bogon ~]# echo "<h1>www.kgc.com</h1>"> /var/www/html/kgccom/index.html
[root@bogon ~]# echo "<h1>www.kcce.com</h1>"> /var/www/html/kccecom/index.html
[root@bogon ~]# vi /usr/local/httpd/conf/extra/httpd-vhosts.conf
<VirtualHost *:80>
DocumentRoot "/var/www/html/kgccom"
ServerName www.kgc.com
ErrorLog "logs/www.kgc.com.error_log"
CustomLog "logs/www.kgc.com.access_log" common
<Directory "/var/www/html">
Require all granted
</Directory>
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "/var/www/html/kccecom"
ServerName www.kcce.com
ErrorLog "logs/www.kcce.com.error_log"
CustomLog "logs/www.kcce.com.access_log" common
<Directory "/var/www/html">
Require all granted
</Directory>
</VirtualHost>
[root@bogon ~]# vi /usr/local/httpd/conf/httpd.conf
(484行)
Include conf/extra/httpd-vhosts.conf
[root@bogon ~]# apachectl -t
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1. Set the 'ServerName' directive globally to suppress this message
Syntax OK
[root@bogon ~]# vi /usr/local/httpd/conf/httpd.conf
(201行)
ServerName www.example.com:80
[root@bogon ~]# apachectl -t
Syntax OK
[root@bogon ~]# systemctl restart httpd 重启
[root@bogon ~]#
新开一台桌面版centos
[root@bogon ~]# vi /etc/hosts
[root@bogon ~]# cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.73.129 www.kgc.com
192.168.73.129 www.kcce.com
[root@bogon ~]#
然后在桌面版centos上的浏览器上 www.kcce.com www.kgc.com
基于端口的虚拟主机
[root@bogon ~]# vi /usr/local/httpd/conf/extra/httpd-vhosts.conf
[root@bogon ~]# cat /usr/local/httpd/conf/extra/httpd-vhosts.conf
<VirtualHost 192.168.73.129:81>
DocumentRoot "/var/www/html/kgccom"
ServerName www.kgc.com
ErrorLog "logs/www.kgc.com.error_log"
CustomLog "logs/www.kgc.com.access_log" common
<Directory "/var/www/html">
Require all granted
</Directory>
</VirtualHost>
<VirtualHost 192.168.73.129:82>
DocumentRoot "/var/www/html/kccecom"
ServerName www.kcce.com
ErrorLog "logs/www.kcce.com.error_log"
CustomLog "logs/www.kcce.com.access_log" common
<Directory "/var/www/html">
Require all granted
</Directory>
</VirtualHost>
[root@bogon ~]# vi /usr/local/httpd/conf/httpd.conf
50行 添加监听的端口号
Listen 81
Listen 82
[root@bogon ~]# apachectl -t
Syntax OK
[root@bogon ~]# systemctl restart httpd
[root@bogon ~]#
验证效果
[root@bogon ~]# curl 192.168.73.129:81
<h1>www.kgc.com</h1>
[root@bogon ~]# curl 192.168.73.129:82
<h1>www.kcce.com</h1>
[root@bogon ~]#
也可以在浏览器上执行
LAMP平台概述
什么是LAMP
目前最为成熟的一种企业网站应用的模式,可提供动态Web站点应用及开发环境
构成组件
Liunx,Apache,MySQL,PHP/Perl/Python
LAMP的优势
- 成本低廉
- 可定制,易与开发
- 方便易用,安全和稳定
配置LAMP的步骤
先编译安装Apache 到 make && make install
编译完成后在执行以下命令
# 优化执行路径
[root@bogon httpd-2.4.25]# ln -s /usr/local/httpd/bin/* /usr/local/bin
[root@bogon httpd-2.4.25]# ls -l /usr/local/bin/httpd /usr/local/bin/apachectl
# 添加为系统服务
[root@bogon httpd-2.4.25]# cat > /lib/systemd/system/httpd.service << 'EOF'
[Unit]
Description=The Apache HTTP Server
After=network.target
[Service]
Type=forking
PIDFile=/usr/local/httpd/logs/httpd.pid
ExecStart= /usr/local/bin/apachectl $OPTIONS
ExecrReload= /bin/kill -HUP $MAINPID
[Install]
WantedBy=multi-user.target
EOF
[root@bogon httpd-2.4.25]# systemctl start httpd
[root@bogon httpd-2.4.25]# systemctl enable httpd
Created symlink from /etc/systemd/system/multi-user.target.wants/httpd.service to /usr/lib/systemd/system/httpd.service.
[root@bogon httpd-2.4.25]#
准备数据库
[root@bogon ~]# tar zxf auto_install_mysql.tar.gz
[root@bogon ~]# cd auto_install_mysql
[root@bogon auto_install_mysql]# ./auto_install_mysql
.....
[root@bogon auto_install_mysql]# source /etc/profile
[root@bogon auto_install_mysql]# mysql -uroot -p123.com
....
安装相关的软件包
# 卸载相关包,避免冲突
rpm -e php php-cli php-ldap php-common php-mysql --nodeps
# 安装依赖
yum -y install zlib-devel libxml2-devel xz-devel
上传libmcrypt-2.5.8.tar.gz包
tar zxf libmcrypt-2.5.8.tar.gz -C /usr/src/
cd /usr/src/libmcrypt-2.5.8/
./configure
make && make install
ln -s /usr/local/lib/libmcrypt.* /usr/lib/
cd
上传mhash-0.9.9.9.tar.gz包
# 安装 mhash
tar zxf mhash-0.9.9.9.tar.gz -C /usr/src/
cd /usr/src/mhash-0.9.9.9/
./configure
make && make install
ln -s /usr/local/lib/libmhash* /usr/lib/
上传mcrypt-2.6.8.tar.gz包
# 安装 mcrypt
export LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH
tar zxf mcrypt-2.6.8.tar.gz -C /usr/src/
cd /usr/src/mcrypt-2.6.8/
./configure
make && make install
安装php
安装php
上传 php-5.5.38.tar.gz 安装包
tar zxf php-5.5.38.tar.gz -C /usr/src
cd /usr/src/php-5.5.38
./configure --prefix=/usr/local/php5 \
--with-mcrypt \
--with-apxs2=/usr/local/httpd/bin/apxs \
--with-mysql=/usr/local/mysql \
--with-mysqli=/usr/local/mysql/bin/mysql_config \
--with-config-file-path=/usr/local/php5 \
--enable-mbstring
# --prefix:指定将 PHP 程序安装到哪个目录下,如/usr/local/php5
# --with-mcrypt:加载数据加密等扩展工具支持
# --with-apxs2:设置 Apache HTTP Server 提供的 apxs 模块支持程序的文件位置
# --with-mysql:设置 MySQL 数据库服务程序的安装位置
# --with-mysqli:添加 MySQL 扩展支持
# --with-config-file-path:设置 PHP 的配置文件 php.ini 将要存放的位置
# --enable-mbstring:启用多字节字符串功能,以便支持中文等代码
如果出现错误
如果出现以上图片的情况
vi /usr/local/httpd/bin/apxs 打开修改配置文件如下
然后在重新执行 在 编译安装
./configure --prefix=/usr/local/php5 \
--with-mcrypt \
--with-apxs2=/usr/local/httpd/bin/apxs \
--with-mysql=/usr/local/mysql \
--with-mysqli=/usr/local/mysql/bin/mysql_config \
--with-config-file-path=/usr/local/php5 \
--enable-mbstring
# 编译及安装
make && make install
[root@bogon php-5.5.38]# echo $?
0
修改配置文件
[root@bogon ~]# cp /usr/src/php-5.5.38/php.ini-development /usr/local/php5/php.ini
[root@bogon ~]# vi /usr/local/php5/php.ini
680行 default_charset = "UTF-8"
202行 short_open_tag = On
873行 extension=php_mysqli.dll
上传zend-loader-php5.5-linux-x86_64_update1.tar.gz包
tar zxf zend-loader-php5.5-linux-x86_64_update1.tar.gz -C /usr/src
cd /usr/src/zend-loader-php5.5-linux-x86_64/
cp ZendGuardLoader.so /usr/local/php5/lib/php
[root@bogon zend-loader-php5.5-linux-x86_64]# vi /usr/local/php5/php.ini
第898行 加
zend_extension=/usr/local/php5/lib/php/ZendGuardLoader.so
zend_loader.enable=1
[root@bogon zend-loader-php5.5-linux-x86_64]# vi /usr/local/httpd/conf/httpd.conf
第398行 加
AddType application/x-httpd-php .php
第260行
加 index.php
[root@bogon zend-loader-php5.5-linux-x86_64]# cd
[root@bogon ~]# apachectl -t
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1. Set the 'ServerName' directive globally to suppress this message
Syntax OK
[root@bogon ~]# systemctl restart httpd
[root@bogon ~]# netstat -nlpt | grep 80
tcp6 0 0 :::80 :::* LISTEN 48699/httpd
[root@bogon ~]# vi /usr/local/httpd/htdocs/test1.php
<?php
phpinfo( );
?>
浏览器上访问
192.168.73.129/test1.php
上传phpMyAdmin-4.7.2-all-languages.tar.gz包
tar zxvf phpMyAdmin-4.7.2-all-languages.tar.gz
mv phpMyAdmin-4.7.2-all-languages/ /usr/local/httpd/htdocs/phpMyAdmin
cd /usr/local/httpd/htdocs/phpMyAdmin
cp config.sample.inc.php config.inc.php
cd ..
[root@bogon htdocs]# ls
index.html phpMyAdmin test1.php test2.php
浏览器上访问
192.168.73.129/phpMyAdmin