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

CentOS 6/7 搭建 LAMP + FTP + SSL 完整安裝配置指南

     編輯:PHP 相關知識

CentOS 6/7 搭建 LAMP + FTP + SSL 完整安裝配置指南

本文記錄在 CentOS 環境下安裝配置 Apache、MySQL/MariaDB、PHP(LAMP)、vsftpd、SSL 憑證的完整步驟,含防火牆、服務管理、phpMyAdmin 部署,適合生產環境部署參考。

環境準備

# 1. 系統更新
yum update -y

# 2. 關閉 SELinux(或設定為 Permissive)
sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
setenforce 0

# 3. 設定時區
timedatectl set-timezone Asia/Taipei
# 或
ln -sf /usr/share/zoneinfo/Asia/Taipei /etc/localtime

1. 安裝 MySQL/MariaDB

# CentOS 7 預設 MariaDB
yum install -y mariadb-server mariadb

# 啟動並設定開機自啟
systemctl start mariadb
systemctl enable mariadb

# 安全性初始化
mysql_secure_installation
# 依序設定:root 密碼、移除匿名用戶、禁止 root 遠程登入、移除 test 資料庫、重新載入權限表

# 驗證連線
mysql -u root -p -e "SELECT VERSION();"

2. 安裝 Apache

yum install -y httpd

systemctl start httpd
systemctl enable httpd

# 防火牆開放 HTTP/HTTPS
firewall-cmd --permanent --add-service=http
firewall-cmd --permanent --add-service=https
firewall-cmd --reload

# 驗證:瀏覽器存取 https://伺服器IP 出現測試頁面

3. 安裝 PHP

# CentOS 7 基礎源 PHP 5.4(舊),建議啟用 EPEL/Remi 安裝 PHP 7.4/8.x
yum install -y epel-release
yum install -y https://rpms.remirepo.net/enterprise/remi-release-7.rpm

# 啟用 PHP 7.4 模組
yum install -y yum-utils
yum-config-manager --enable remi-php74

# 安裝 PHP 核心與常用擴充
yum install -y php php-cli php-fpm php-mysqlnd php-gd php-mbstring php-xml php-json php-opcache php-pdo php-pecl-zip php-pecl-redis

systemctl start php-fpm
systemctl enable php-fpm

# 驗證
php -v
php -m | grep -E 'mysql|gd|mbstring|json|opcache'

4. 整合 Apache + PHP-FPM(效能優於 mod_php)

# 安裝代理模組
yum install -y httpd proxy_fcgi

# 配置 Apache 使用 PHP-FPM
cat > /etc/httpd/conf.d/php-fpm.conf << 'EOF'

    ProxySet connectiontimeout=30 timeout=300



    SetHandler "proxy:fcgi://127.0.0.1:9000"


DirectoryIndex index.php index.html
EOF

systemctl restart httpd

5. 安裝 phpMyAdmin

yum install -y phpmyadmin

# 允許遠程存取(修改預設只允許本機)
sed -i 's/Require ip 127.0.0.1/Require all granted/' /etc/httpd/conf.d/phpMyAdmin.conf
# 或新增允許特定 IP
# Require ip 192.168.1.0/24

systemctl restart httpd

# 存取 https://伺服器IP/phpMyAdmin

6. 配置 SSL/HTTPS

# 安裝 mod_ssl
yum install -y mod_ssl

# 產生自簽憑證(測試用,生產請用 Let's Encrypt)
mkdir -p /etc/pki/tls/private /etc/pki/tls/certs
openssl req -x509 -nodes -days 365 -newkey rsa:2048 \
    -keyout /etc/pki/tls/private/localhost.key \
    -out /etc/pki/tls/certs/localhost.crt \
    -subj "/C=TW/ST=Taipei/L=Taipei/O=MyOrg/CN=localhost"

# 配置 SSL 虛擬主機
cat > /etc/httpd/conf.d/ssl-custom.conf << 'EOF'

    ServerName localhost
    DocumentRoot /var/www/html
    
    SSLEngine on
    SSLCertificateFile /etc/pki/tls/certs/localhost.crt
    SSLCertificateKeyFile /etc/pki/tls/private/localhost.key
    
    
        AllowOverride All
        Require all granted
    

EOF

systemctl restart httpd

7. 安裝配置 vsftpd

yum install -y vsftpd

# 備份原配置
cp /etc/vsftpd/vsftpd.conf /etc/vsftpd/vsftpd.conf.bak

# 核心配置
cat > /etc/vsftpd/vsftpd.conf << 'EOF'
anonymous_enable=NO
local_enable=YES
write_enable=YES
local_umask=022
dirmessage_enable=YES
xferlog_enable=YES
connect_from_port_20=YES
xferlog_std_format=YES
listen=NO
listen_ipv6=YES
pam_service_name=vsftpd
userlist_enable=YES
tcp_wrappers=YES

# 被動模式配置(需防火牆開放對應埠)
pasv_enable=YES
pasv_min_port=40000
pasv_max_port=40100
pasv_address=你的公網IP
EOF

# 允許 root 登入(不建議生產環境)
sed -i 's/^root/#root/' /etc/vsftpd/ftpusers
sed -i 's/^root/#root/' /etc/vsftpd/user_list

# 防火牆開放 FTP 埠
firewall-cmd --permanent --add-port=21/tcp
firewall-cmd --permanent --add-port=40000-40100/tcp
firewall-cmd --reload

systemctl start vsftpd
systemctl enable vsftpd

8. 常用服務管理指令

服務 啟動 停止 重啟 狀態 開機自啟
httpd systemctl start httpd systemctl stop httpd systemctl restart httpd systemctl status httpd systemctl enable httpd
mariadb systemctl start mariadb systemctl stop mariadb systemctl restart mariadb systemctl status mariadb systemctl enable mariadb
php-fpm systemctl start php-fpm systemctl stop php-fpm systemctl restart php-fpm systemctl status php-fpm systemctl enable php-fpm
vsftpd systemctl start vsftpd systemctl stop vsftpd systemctl restart vsftpd systemctl status vsftpd systemctl enable vsftpd

9. 進階優化建議

  • MySQL 調優:innodb_buffer_pool_size 設為實體記憶體 70%
  • PHP-FPM 池配置:pm.max_children、pm.start_servers 依記憶體調整
  • Apache MPM Event:httpd -V 確認,配合 PHP-FPM 效能最佳
  • Opcache 啟用:opcache.enable=1, opcache.memory_consumption=128
  • Let's Encrypt 免費憑證:certbot --apache 自動申請續期
  • 日誌切割:logrotate 管理 access_log、error_log、slow_log
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved