首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 網頁程式設計 >> PHP 程式設計 >> PHP 相關知識

Linux + Apache + MongoDB + PHP 開發環境完整搭建指南:從源碼編譯到驅動安裝

     編輯:PHP 相關知識

Linux + Apache + MongoDB + PHP 開發環境完整搭建指南:從源碼編譯到驅動安裝

本文記錄在 CentOS 64 位系統上,從零構建 LAMP 則為 LAMM(Linux + Apache + MongoDB + PHP)環境的完整步驟,涵蓋系統初始化、Apache 安裝、MongoDB 部署、PHP 5.4 源碼編譯、MongoDB PHP 驅動編譯安裝、雙邊配置檔修改。

一、系統初始化

# 關閉 SELinux、iptables(或開放 80/27017)、配置固定 IP、主機名
setenforce 0
sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
service iptables stop
chkconfig iptables off

二、軟體版本與下載

元件版本/來源下載連結
Apacheyum 安裝 httpd*-
MongoDB2.6.4 (Linux x86_64)https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-2.6.4.tgz
PHP5.4.32https://cn2.php.net/distributions/php-5.4.32.tar.gz
MongoDB PHP Driver1.5.5https://pecl.php.net/get/mongo-1.5.5.tgz

三、安裝順序:Apache → MongoDB → PHP → Driver

原則:PHP 編譯需連結 Apache (apxs) 與 MongoDB 驅動,故必須在兩者之後。

1. Apache

yum install httpd* -y
# 修改 /etc/httpd/conf/httpd.conf
ServerName 192.168.1.100:80
# 啟動
service httpd start
chkconfig httpd on

2. MongoDB


# 建立資料/日誌目錄
mkdir -p /data/mongodb/db /data/mongodb/logs

# 解壓到 /usr/local
tar -xvf mongodb-linux-x86_64-2.6.4.tgz -C /usr/local/

# 環境變數
echo 'export PATH=$PATH:/usr/local/mongodb-linux-x86_64-2.6.4/bin' >> ~/.bash_profile
source ~/.bash_profile

# 啟動(背景執行)
mongod --dbpath /data/mongodb/db --logpath /data/mongodb/logs/mongo.log --fork

3. PHP 5.4 編譯(關鍵:--with-apxs2)


tar -xvf php-5.4.32.tar.gz
cd php-5.4.32
./configure \
  --prefix=/usr/local/php \
  --with-config-file-path=/etc \
  --with-apxs2=/usr/sbin/apxs \
  # 可加入:--with-mysql --with-pdo-mysql --with-curl --with-gd --enable-mbstring 等
make && make install

# 複製生產環境 php.ini
cp php.ini-production /etc/php.ini

# 環境變數
echo 'export PATH=$PATH:/usr/local/php/bin' >> ~/.bash_profile
source ~/.bash_profile
關鍵產物:編譯後會在 Apache 模組目錄產生 libphp5.so(如 /etc/httpd/modules/libphp5.so),供 Apache 載入。

4. MongoDB PHP Driver 編譯


tar -xvf mongo-1.5.5.tgz
cd mongo-1.5.5
phpize                    # 生成 configure
./configure --enable-mongo=shared --with-php-config=/usr/local/php/bin/php-config
make && make install
# 安裝路徑類似 /usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/mongo.so

四、雙邊配置檔修改

/etc/php.ini

extension=mongo.so
register_globals=On          # 不推薦開啟,僅相容舊代碼
date.timezone=Asia/Shanghai

/etc/httpd/conf/httpd.conf

LoadModule php5_module modules/libphp5.so
PHPIniDir "/etc/php.ini"
AddType application/x-httpd-php .php
AddType application/x-httpd-php .htm
AddType application/x-httpd-php .html
DirectoryIndex index.html index.php index.phtml

五、驗證


# 重啟 Apache
service httpd restart

# 建立測試頁
echo '<?php phpinfo(); ?>' > /var/www/html/info.php
# 瀏覽器訪問 https://IP/info.php 搜尋 "mongo" 區塊

# CLI 測試
php -r 'new MongoClient(); echo "Driver OK\n";'

現代化建議

  • MongoDB 4.0+:舊 mongo 擴展已棄用,請改用 mongodb 擴展(pecl install mongodb)並配合 MongoDB\Client 類。
  • PHP 7.4+:性能提升 2-3 倍,強型別支援,長期支援版本。
  • 容器化:生產環境請用 docker-compose.yml 定義 mongo、php:fpm、nginx 服務,一鍵啟動、版本鎖定、遷移無痛。

手工編譯雖繁瑣,但能深入理解元件間依賴關係(apxs、phpize、php-config 三劍客)。現代專案請優先採用官方 Docker Image 或 Ansible Playbook,將環境定義為代碼,實現 Immutable Infrastructure。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved