CentOS7 安装并配置 LAMP

LAMP 为 Linux、Apache、MySQL、PHP 的简称,这是一个常规的 Web 服务器环境解决方案,使用其首字母缩写 “LAMP” 来引用。

关闭防火墙和 selinux

1
2
systemctl stop firewalld
systemctl disable firewalld

禁用 Selinux

1
vim /etc/selinux/config

修改为 disabled

1
SELINUX=disabled

image

注意:上面配置是重启后才生效,所以需要临时关闭 selinux 防火墙

1
setenforce  0

安装 Apache

1
yum -y install httpd

启动 Apache

1
2
3
4
5
systemctl start httpd //启动apache

systemctl enable httpd //设置apache开机启动

systemctl status httpd //查看服务状态

启动后在外部浏览器访问主机 IP 就能看到 Apache 页面
image

目录详解

  • 程序目录:/usr/sbin/httpd
  • 默认网站主页存放目录: /var/www/html/
  • 日志文件存放目录:/var/log/httpd/
  • 主配置文件:/etc/httpd/conf/httpd.conf
  • 从配置文件:/etc/httpd/conf.d/

检查配置文件是否正确

1
httpd -t

image

如果有以下提示可忽略

1
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

配置站点的三种方式

基于 IP 的方式

  1. 新建站点文件
    1
    2
    3
    cd /var/www/html
    mkdir web1 && cd web1
    vim index.html
    添加网页内容
    1
    <h1 style="color:#D81B60">Hello Multisite! </h1>
  2. 多站点配置文件
1
2
3
4
5
6
7
8
9
10
vim /etc/httpd/conf.d/http-vhost.conf


<VirtualHost *:80>
ServerAdmin feng@gmail.com
DocumentRoot /var/www/html/web1
ServerName web1.frg.com
ErrorLog logs/web1-frg-com-error_log
CustomLog logs/web1-frg-com-access_log common
</VirtualHost>
  1. 重启 Apache
    1
    2
    3
    4
    # 检查配置文件
    httpd -t
    # 重启服务
    systemctl restart httpd

使用 IP 访问
image

使用端口访问

配置多站点配置文件

  1. 新建站点文件
    1
    2
    3
    cd /var/www/html
    mkdir web2
    vim index.html
    添加网页内容
    1
    <h1 style="color:#D81B60">Hello Multisite! </h1>
  2. 多站点配置文件
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    vim /etc/httpd/conf.d/http-vhost.conf


    <VirtualHost *:8899>
    ServerAdmin feng@gmail.com
    DocumentRoot /var/www/html/web2
    ServerName web2.frg.com
    ErrorLog logs/web2-frg-com-error_log
    CustomLog logs/web2-frg-io-access_log common
    </VirtualHost>
  3. 增加监听端口
    1
    2
    3
    4
    vim /etc/httpd/conf/httpd.conf

    # 在Listen 80 之后增加一行
    Listen 8899
  4. 重启 Apache
    1
    2
    3
    4
    # 检查配置文件
    httpd -t
    # 重启服务
    systemctl restart httpd
    浏览器通过域名:8899 访问
    image

本地 DNS 解析访问

《CentOS7 安装并配置本地 DNS 服务器》 https://www.cnblogs.com/Likfees/p/15713510.html

在 DNS 服务器添加正反向解析

1
2
3
4
5
6
7
8
vim /etc/named.rfc1912.zones


zone "frg.com" IN {
type master;
file "feng.io.zone";
allow-update { none; };
};

正向数据区域文件

1
2
3
4
5
cd /var/named

cp -p named.localhost frg.com.zone

vim named.localhost frg.com.zone

image

重启 DNS 服务器

1
systemctl restart named

客户机中增加 DNS 服务器解析

Linux:

1
sudo vim /etc/resolv.conf 

增加自己本地的 DNS 服务器地址到顶部
重启网络即可使用域名访问
image

image

image

window:

  1. 修改网卡首选 DNS 为本地服务器
  2. 修改 host 文件
    1
    192.168.139.100 frg.com

Mysql8 安装

《CentOS7 安装 Mysql8 并配置远程登录》 https://www.cnblogs.com/Likfees/p/15366225.html

PHP 安装与配置

编译安装

  1. 下载 PHP7 源码包

    1
    wget -P /opt/software https://www.php.net/distributions/php-7.4.27.tar.gz
  2. 解压

    1
    tar -zxvf php-7.4.27.tar.gz
  3. 安装依赖包

    1
    yum -y install libxml2-devel sqlite-devel  httpd-devel
  4. 预编译

    –enable-fpm –with-apxs2=/usr/bin/apxs 调用 Apache 的 apxs 生成 PHP 模块,依赖包 httpd-devel

    1
    2
    cd /opt/software/php-7.4.27
    ./configure --prefix=/usr/local/php7 --enable-fpm --with-apxs2=/usr/bin/apxs

    image

  5. 编译并安装

    1
    make && make install
  6. PHP 配置文件

    1
    2
    cd /opt/software/php-7.4.27
    cp php.ini-development php.ini
  7. 链接可执行文件

    1
    ln -s /usr/local/php7/bin/php /usr/local/bin
1
php -v

image

配置 Apache 支持 PHP7

  1. 编辑 httpd.conf
    1
    vim /etc/httpd/conf/httpd.conf
    定位到 <IfModule dir_module> 添加上 index.php
    1
    2
    3
    <IfModule dir_module>
    DirectoryIndex index.php index.html
    </IfModule>
    image

在文件末尾处添加
如果已存在则不需要添加

1
2
3
4
LoadModule php7_module modules/libphp7.so
<FilesMatch \.php$>
SetHandler application/x-httpd-php
</FilesMatch>

image

  1. 检查配置文件

    1
    httpd -t
  2. 进入站点新建一个 index.php
    vim index.php

    1
    2
    3
    <?php

    phpinfo();

    访问站点
    image