
Trên macOS 12 Monterey, Apache được tích hợp vào hệ thống. Tuy nhiên, khi bạn kiểm tra httpd.conf tích hợp sẵn của Apache, bạn sẽ thấy một dòng cho biết:
#PHP was deprecated in macOS 11 and removed from macOS 12
Từ bản macOS 12, PHP không được tích hợp sẵn, nếu bạn muốn thiết lập một local web server để kiểm thử (Apache, MySQL, PHP) một giải pháp được đưa ra là sử dụng HomeBrew để cài đặt. Các dòng lệnh thực hiện trên Terminal.
BƯỚC 1: CHUẨN BỊ
Vô hiệu hóa Apache tích hợp sẵn trên macOS:
sudo apachectl stop
Cài đặt HomeBrew
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
BƯỚC 2: CÀI ĐẶT
Cài đặt Apache, PHP, mySQL và phpMyAdmin qua Homebrew:
brew install httpd php mysql phpmyadmin



BƯỚC 3: CẤU HÌNH
Chuyển tới thu mục cấu hình:
cd /usr/local/etc/httpd
Backup file cấu hình:
sudo cp httpd.conf httpd.conf.bak
Sửa file cấu hình:
sudo nano httpd.conf
a) Kích hoạt các Mô-đun
Dùng phím tắt control + w để tìm kiếm và đảm bảo hủy comment (bỏ dấu #):
LoadModule authn_core_module lib/httpd/modules/mod_authn_core.so
LoadModule authz_host_module lib/httpd/modules/mod_authz_host.so
LoadModule userdir_module lib/httpd/modules/mod_userdir.so
LoadModule include_module lib/httpd/modules/mod_include.so
LoadModule rewrite_module lib/httpd/modules/mod_rewrite.so
Include /usr/local/etc/httpd/extra/httpd-userdir.conf
b) Cấu hình DocumentRoot
Sử dụng control + w và tìm kiếm DocumentRoot
Mặc định thư mục root là /usr/local/var/www, Ngọc sửa thành /Users/lucngoc/Sites/ (folder Sites trong thư mục người dùng, thay lucngoc bằng username của bạn).
Thêm dấu # để biến thành comment:
#DocumentRoot "/usr/local/var/www"
#<Directory "/usr/local/var/www">
Và thêm các dòng vào bên dưới:
DocumentRoot "/Users/lucngoc/Sites/"
<Directory "/Users/lucngoc/Sites/">
Sau đó thêm các tùy chọn:
Options Indexes FollowSymLinks Multiviews
MultiviewsMatch Any
DirectoryIndex index.php
AllowOverride All
Cấu hình DocumentRoot trông giống như thế này:
#DocumentRoot "/usr/local/var/www"
#<Directory "/usr/local/var/www">
DocumentRoot "/Users/lucngoc/Sites/"
<Directory "/Users/lucngoc/Sites/">
#
# Possible values for the Options directive are "None", "All",
# or any combination of:
# Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
#
# Note that "MultiViews" must be named *explicitly* --- "Options All"
# doesn't give it to you.
#
# The Options directive is both complicated and important. Please see
# http://httpd.apache.org/docs/2.4/mod/core.html#options
# for more information.
#
Options Indexes FollowSymLinks Multiviews
MultiviewsMatch Any
DirectoryIndex index.php
#
# AllowOverride controls what directives may be placed in .htaccess files.
# It can be "All", "None", or any combination of the keywords:
# AllowOverride FileInfo AuthConfig Limit
#
AllowOverride All
#
# Controls who can get stuff from this server.
#
Require all granted
</Directory>
c) Sửa Port
Mặc định sẽ sử dụng Port 8080, địa chỉ web bạn có dạng localhost:8080
Bạn có sửa thành Port 80 bằng cách dùng control + w tìm kiếm Listen 8080 và sửa thành Listen 80, tuy nhiên khuyến nghị bạn nên giữ nguyên 8080.
Kết thúc sửa file cấu hình, bấm control + x, chọn y để lưu lại file.
d) Cấu hình PHP và phpMyAdmin trên Apache
Chạy lệnh:
tee -a httpd.conf <<EOF
LoadModule php_module /usr/local/opt/php/lib/httpd/modules/libphp.so
<FilesMatch \.php$>
SetHandler application/x-httpd-php
</FilesMatch>
Alias /phpmyadmin /usr/local/share/phpmyadmin
<Directory /usr/local/share/phpmyadmin/>
Options Indexes FollowSymLinks MultiViews
DirectoryIndex index.php
AllowOverride All
<IfModule mod_authz_core.c>
Require all granted
</IfModule>
<IfModule !mod_authz_core.c>
Order allow,deny
Allow from all
</IfModule>
</Directory>
EOF
e) Cấu hình mySQL
Chạy lệnh vào làm theo hướng dẫn để thêm mật khẩu cho root, các tùy chọn còn lại chọn yes:
mysql_secure_installation
BƯỚC 4: KHỞI ĐỘNG
Bây giờ khởi động Apache và PHP
brew services start httpd
brew services start php
Dùng lệnh resert để khởi động lại dịch vụ nếu cần thiết:
brew services restart httpd
Bạn có thể kiểm tra các dịch vụ đang được brew chạy:
brew services list
Test thử, chạy lệnh:
cd ~/Sites && echo '<?php phpinfo(); ?>' > phpinfo.php
Sau đó mở trình duyệt vào địa chỉ localhost:8080/phpinfo.php
BƯỚC 5: CHẠY THỬ
Quản lý phpMyAdmin tại địa chỉ localhost:8080/phpmyadmin, tại đây có thể tạo mới Database.
Để tải WordPress ta chạy lệnh
cd ~/Sites && wget https://wordpress.org/latest.tar.gz && tar -xzvf latest.tar.gz && mv -v -f wordpress/* . && rm -f latest.tar.gz && rm -r wordpress

Cài đặt WordPress hay các nền tảng yêu thích của bạn!










Đúng cái mình cần, cảm ơn Ngọc!