Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux基礎 >> Linux技術

CentOS 7 打造 LAMP 環境完整實作:從防火牆到 PHP 測試

LAMP(Linux、Apache、MariaDB/MySQL、PHP)是最經典的網站伺服器組合之一。本文以 CentOS 7 為例,示範如何一步步把整套環境架起來。

一、預先處理防火牆與 SELinux

CentOS 7 預設採用 firewalld 作為防火牆,為了避免後續測試受阻,可先關閉:

systemctl stop firewalld.service   # 停止 firewall
systemctl disable firewalld.service  # 禁止 firewall 開機啟動

接著關閉 SELinux:

vi /etc/selinux/config
# 將 SELINUX=enforcing 註解掉
# 新增 SELINUX=disabled
:wq!    # 保存退出
setenforce 0  # 使設定立即生效

二、安裝 Apache

yum install httpd       # 依提示輸入 Y 完成安裝
systemctl start httpd.service   # 啟動
systemctl stop httpd.service    # 停止
systemctl restart httpd.service  # 重啟
systemctl enable httpd.service   # 設為開機啟動

三、安裝 MariaDB(MySQL)

yum install mariadb mariadb-server  # 輸入 Y 自動安裝
systemctl start mariadb.service   # 啟動
systemctl enable mariadb.service   # 開機啟動

修改 root 密碼與設定遠端授權:

set password for 'root'@'localhost'=password('root');
grant all on *.* to root identified by 'root';

四、安裝 PHP 及相關元件

yum install php php-mysql php-gd libjpeg* php-ldap php-odbc php-pear php-xml php-xmlrpc php-mbstring php-bcmath php-mhash
systemctl restart mariadb.service
systemctl restart httpd.service

五、測試成果

建立測試頁面:

cd /var/www/html
vi index.php
<?php phpinfo(); ?>
:wq!

在用戶端瀏覽器輸入伺服器 IP,若能看到 PHP 設定資訊頁,代表整套 LAMP 環境已成功運作。

Copyright © Linux教程網 All Rights Reserved