Tag Archive พีเอชพี

Byphunsanit

Apache: run PHP

การติดตั้ง Apache โดยใช้ brew จะไม่สามารถที่จะ run PHP โดยอัตโนมัติ เราต้องมาแก้คอนฟิกเกอเรชั่นอีกเล็กน้อยให้สามารถทำงานได้

  1. ติดตั้ง apache หรือ php ให้ติดตั้งก่อนโดย คำสั่ง
    brew install httpd php
  2. หาตำแหน่งที่ติดตั้งอะแพชีก่อนโดยใช้คำสั่ง
    brew info httpd
    จะเห็น message กลับมา เช่น
    ==> httpd: stable 2.4.61 (bottled)
    Apache HTTP server
    https://httpd.apache.org/
    Installed
    /opt/homebrew/Cellar/httpd/2.4.61 (1,664 files, 32.2MB) *
    Poured from bottle using the formulae.brew.sh API on 2024-05-17 at 18:56:00
    From: https://github.com/Homebrew/homebrew-core/blob/HEAD/Formula/h/httpd.rb
    License: Apache-2.0
    ==> Dependencies
    Required: apr ✔, apr-util ✔, brotli ✔, libnghttp2 ✔, openssl@3 ✔, pcre2 ✔
    ==> Caveats
    DocumentRoot is /opt/homebrew/var/www.

    The default ports have been set in /opt/homebrew/etc/httpd/httpd.conf to 8080 and in
    /opt/homebrew/etc/httpd/extra/httpd-ssl.conf to 8443 so that httpd can run without sudo.

    To restart httpd after an upgrade:
    brew services restart httpd
    Or, if you don’t want/need a background service you can just run:
    /opt/homebrew/opt/httpd/bin/httpd -D FOREGROUND
    ==> Analytics
    install: 11,758 (30 days), 29,508 (90 days), 117,739 (365 days)
    install-on-request: 10,529 (30 days), 25,590 (90 days), 98,933 (365 days)
    build-error: 27 (30 days)
    จะเห็นข้อมูลสำคัญ 3 จุดคือ
    • จะสามารถแสดงเว็บเพจใน DocumentRoot คือ /opt/homebrew/var/www.
    • คอนฟิกเกอเรชั่นจะอยู่ที่ /opt/homebrew/etc/httpd/
    • ตอนนี้ใช้รอรับคำสั่งที่ port 8080
  3. ดูข้อมูลพีเอชพีโดยคำสั่ง
    brew info httpd
    จะเห็น message กลับมา เช่น
    ==> php: stable 8.3.9 (bottled), HEAD
    General-purpose scripting language
    https://www.php.net/
    Installed
    /opt/homebrew/Cellar/php/8.3.9 (524 files, 88.7MB) *
    Poured from bottle using the formulae.brew.sh API on 2024-05-17 at 19:00:23
    From: https://github.com/Homebrew/homebrew-core/blob/HEAD/Formula/p/php.rb
    License: PHP-3.01
    ==> Dependencies
    Build: httpd ✔, pkg-config ✘
    Required: apr ✔, apr-util ✔, argon2 ✔, aspell ✔, autoconf ✔, curl ✔, freetds ✔, gd ✔, gettext ✔, gmp ✔, icu4c ✔, krb5 ✔, libpq ✔, libsodium ✔, libzip ✔, oniguruma ✔, openldap ✔, openssl@3 ✔, pcre2 ✔, sqlite ✔, tidy-html5 ✔, unixodbc ✔
    ==> Options
    –HEAD
    Install HEAD version
    ==> Caveats
    To enable PHP in Apache add the following to httpd.conf and restart Apache:
    LoadModule php_module /opt/homebrew/opt/php/lib/httpd/modules/libphp.so

    <FilesMatch \.php$>
    SetHandler application/x-httpd-php
    </FilesMatch>


    Finally, check DirectoryIndex includes index.php
    DirectoryIndex index.php index.html

    The php.ini and php-fpm.ini file can be found in:
    /opt/homebrew/etc/php/8.3/

    To restart php after an upgrade:
    brew services restart php
    Or, if you don’t want/need a background service you can just run:
    /opt/homebrew/opt/php/sbin/php-fpm –nodaemonize
    ==> Analytics
    install: 53,003 (30 days), 156,531 (90 days), 690,907 (365 days)
    install-on-request: 49,358 (30 days), 145,335 (90 days), 645,350 (365 days)
    build-error: 14 (30 days)
    จะเห็นข้อมูลสำคัญ 3 จุดคือ
    • php version php: stable 8.3.9
    • ตำแหน่ง php mosule คือ
      LoadModule php_module /opt/homebrew/opt/php/lib/httpd/modules/libphp.so
    • php ini อยู่ที่ /opt/homebrew/etc/php/8.3/php-fpm.ini
  4. แก้ไฟล์ /opt/homebrew/etc/httpd/httpd.conf
    1. ถ้าต้องการให้ apache รอรับคำสั้งโดยใช้ port อื่น ๆ นอกจาก 8080 ให้ค้นหาบรรทัด
      Listen 8080
      แก้เป็น
      Listen 80
      ถ้าไม่ต้องการใช้ nginx หรือไม่ต้องการให้ user ต้องใส่ port เวลาเรียก URL
    2. เอา comment ออกจาก module ที่ต้องการ ( เอา # หน้าบรรทัดออก ) จาก
      LoadModule alias_module lib/httpd/modules/mod_alias.so
      LoadModule rewrite_module lib/httpd/modules/mod_rewrite.so
    3. เพิ่มโหลด PHP จากที่เห็นในด้านบนในบรรทัดสุดท้าย หรือจะต่อจากจุดเดิมก็ได้ เช่น
      LoadModule php_module /opt/homebrew/opt/php/lib/httpd/modules/libphp.so
    4. แก้ลำดับการแสดงไฟล์เมื่อไม่ระบุชื่อไฟล์ โดยค้นหา DirectoryIndex เช่น
      DirectoryIndex index.html
      แก้เป็น
      DirectoryIndex index.php index.html
    5. โหลด config เพิ่มจากไฟล์
      Include /opt/homebrew/etc/httpd/extra/httpd-php.conf
  5. สร้างไฟล์ configuration เริ่มต้นของ PHP โดยสร้างไฟล์ /opt/homebrew/etc/httpd/extra/httpd-php.conf มีเนื้อหา
    AddType application/x-httpd-php .php
    <IfModule php_module>
      <FilesMatch \.php$>
        SetHandler application/x-httpd-php
      </FilesMatch>
    
      <IfModule dir_module>
        DirectoryIndex index.html index.php
      </IfModule>
    </IfModule>
    
  6. สร้างไฟล์ทดสอบ /opt/homebrew/var/www/phpinfo.php โดยคำสั่ง
    echo "<?php phpinfo();" > /opt/homebrew/var/www/phpinfo.php
    จะมีเนื้อหา
    <?php phpinfo();
  7. ทดสอบโดยใช้คำสั่ง
    httpd -T
    แก้ไขตาม error หรือ info ที่แจ้งกลับมา
  8. เปลี่ยน permissions โดย
    sudo chown -R :www /opt/homebrew/var/www
    sudo chmod -R g+w /opt/homebrew/var/www
  9. restart โดยคำสั่ง
    brew services restart httpd
    และดูผลการติดตั้งโดยเปิดเว็บ
    http://localhost:8080/phpinfo.php
    หรือ
    http://localhost/phpinfo.php

อ่านเพิ่มเติม