search
HomeBackend DevelopmentPHP TutorialTutorial Setup Localhost Mac (ApacheMySQL , and PHP )

Haloo gaes, pada tutorial kali ini aku akan share tutorial cara setup local development tanpa menginstall aplikasi web server seperti XAMPP ataupun MAMP. Kita akan menggunakan beberapa fitur bawaan Mac OS.

Mac OS yang aku gunakan saat ini adalah Mac OS Big Sur version 11.6.2

Apache2

Secara default Mac OS sudah terinstall apache2. Untuk mengeceknya buka terminal run apachectl -v . Hasilnya seperti ini.

dhimaskirana:~$ apachectl -v
Server version: Apache/2.4.51 (Unix)
Server built:   Nov  1 2021 21:26:04

Secara default lokasi file-file yang akan ditampilkan oleh apache berlokasi di /Library/WebServer/Documents.

PHP

Secara default Mac OS sudah terinstall php versi 7.3. Untuk mengecek kalian bisa menjalankan php -v

dhimaskirana:~$ php -v
WARNING: PHP is not recommended
PHP is included in macOS for compatibility with legacy software.
Future versions of macOS will not include PHP.
PHP 7.3.29-to-be-removed-in-future-macOS (cli) (built: Sep  6 2021 05:14:39) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.3.29, Copyright (c) 1998-2018 Zend Technologies

Tetapi akan ada warning bahwa di versi mac berikutnya php akan dihapus dari Mac OS. Jadi sebaiknya kalian menginstall php sendiri menggunakan Homebrew.

Menginstall Homebrew

Nah, kita akan menginstall php menggunakan homebrew. Homebrew merupakan sebuah package manager untuk iOS, yang dapat diinstall dengan mudah melalui terminal.

Sebelum menginstall php, install homebrew dulu. Kunjungi website homebrew di brew.sh. Kemudian kalian copy script Install Homebrew dan jalankan pada terminal. Tunggu sampai proses instalasi selesai. Kalau kalian baru pertama kali menginstall homebrew, proses instalasi akan memakan waktu yang cukup lama.

Menginstall PHP

Oke homebrew sudah terinstall saatnya menginstall php. Untuk menginstall php jalankan perintah brew install php. Homebrew akan menginstall PHP versi terbaru. Tunggu sampai proses install selesai.

Setelah instalasi php selesai, jalankan perintah export PATH="/usr/local/opt/php/bin:$PATH" untuk menambahkan path php ke environment variable pada Mac OS, sehingga perintah php bisa dieksekusi secara global.

Jika berhasil, silahkan lakukan perintah php -v, maka versi yang muncul adalah versi terbaru.

dhimaskirana:~$ php -v
PHP 8.1.3 (cli) (built: Mar  1 2022 10:23:25) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.1.3, Copyright (c) Zend Technologies
with Zend OPcache v8.1.3, Copyright (c), by Zend Technologies

Menginstall MySQL

Langkah selanjutnya adalah menginstall MySQL untuk keperluan database. Untuk menginstall mysql jalankan perintah brew install mysql@5.7. Tunggu hingga proses instalasi selesai. Jika sudah selesai, untuk mengecek versi mysql yang terinstall gunakan mysql –version

dhimaskirana:~$ mysql --version
mysql  Ver 14.14 Distrib 5.7.37, for osx10.16 (x86\_64) using  EditLine wrapper

Set Modul PHP di Apache

Secara default apache di Mac OS tidak memuat modul php, oleh karena itu jalankan perintah brew info php. Maka akan keluar informasi terkait modul php yang sudah kita install melalui homebrew tadi seperti berikut ini.

\==> Caveats
To enable PHP in Apache add the following to httpd.conf and restart Apache:
    LoadModule php\_module /usr/local/opt/php/lib/httpd/modules/libphp.so

    <filesmatch>
        SetHandler application/x-httpd-php
    </filesmatch>

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

Informasi ini yang akan kita gunakan untuk menkonfigurasikan apache supaya memuat modul php yang telah kita install.

Jalankan perintah berikut ini untuk membuat teks editor.

sudo /System/Applications/TextEdit.app/Contents/MacOS/TextEdit /private/etc/apache2/httpd.conf

Perintah ini akan membuka file httpd.conf (file konfigurasi apache2) menggunakan aplikasi bawaan Mac OS yaitu TextEdit secara administrator atau root. Jangan lupa masukan password.

Tutorial Setup Localhost Mac (ApacheMySQL , and PHP )

Masukan semua konfigurasi ke dalam file httpd.conf seperti pada gambar di atas. Konfigurasi yang perlu di set adalah:

  1. LoadModule php
  2. SetHandler supaya bisa membaca file php
  3. DirectoryIndex

Jangan lupa klik simpan tersebut. Kemudian tutup aplikasi teks editornya.

Kemudian restart apache nya supaya apache nya mengenali php 8.1 yang telah di install tadi. Cara nya jalankan perintah untuk merestart apache.

sudo apachectl restart

Test PHP

Buat file index.php dalam sebuah folder phpinfo, kemudian masukan fungsi phpinfo(). Kemudian akses dengan alamat http://localhost/phpinfo/.

Tutorial Setup Localhost Mac (ApacheMySQL , and PHP )

Jika informasi PHP muncul, maka apache sukses memuat modul php. Nah kalian bisa mencoba menginstall WordPress.

Install phpMyAdmin

Setelah php berhasil, kita akan gunakan phpMyAdmin untuk mengakses database mysql kita. phpMyAdmin adalah database management berbasis web browser, sehingga akan mempermudah kita membuat database.

Download phpMyAdmin di https://www.phpmyadmin.net/downloads/ kemudian pilih saja yang bahasa English saja.

Unzip, kemudian rename folder menjadi phpmyadmin, copy ke /Library/WebServer/Documents.

Akses menggunakan alamat http://localhost/phpmyadmin/.

Secara default akses mysql menggunakan username root dan password kosong.

Jika kalian mendapati error Login without a password is forbidden by configuration (see AllowNoPassword) berarti phpMyAdmin tidak mengijinkan password kosong.

Solusinya gampang, kalian buka folder phpmyadmin kemudian ada file config.sample.inc.php rename jadi config.inc.php.

Kemudian buka file config.inc.php dengan text editor kalian, kemudian set AllowNoPassword menjadi true. Simpan konfigurasi file kalian.

$cfg\['Servers'\]\[$i\]\['AllowNoPassword'\] = true;

mod_rewrite

Jika permalink WordPress kalian error padahal sudah buat file .htaccess di instalasi WordPress kemungkinan penyebabnya adalah modul mod_rewrite belum dimuat di apache.

Tutorial Setup Localhost Mac (ApacheMySQL , and PHP )

Caranya adalah buka kembali file httpd.conf menggunakan perintah sudo supaya dibuka menggunakan akses root, kemudian uncomment rewrite_module. Kemudian simpan dan restart apache menggunakan perintah sudo apachectl restart.

Permission

Jika kalian mengcopy file ke dalam folder /Library/WebServer/Documents maka file kalian hanya bisa dibaca tetapi tidak bisa dieksekusi. Ini terjadi karena secara default apache berjalan diatas user _www dan group _www.

Solusinya adalah:

  1. Menambahkan username account kalian ke group _www
  2. Semua file yang kalian copy ke /Library/WebServer/Documents harus diubah owner dan groupnya ke _www
  3. Memberikan akses write ke group _www

Tambah Username ke Group _www

Langsung kita coba, tambahkan username kalian ke group _www dengan command berikut. whoami di command akan digantikan dengan username kalian.

sudo dseditgroup -o edit -a `whoami` -t user _www

Untuk mengecek apakah username kalian sudah masuk ke group _www bisa menggunakan command berikut

dscacheutil -q group -a name _www

Ubah Owner dan Group file ke _www

Setelah itu, setiap kita mengcopy file atau folder ke dalam /Library/WebServer/Documents, kita harus mengubah owner dan group semua file yg dicopy ke _www.

Sebagai contoh kalian copy folder wordpress ke /Library/WebServer/Documents. Maka buka terminal di /Library/WebServer/Documents, kemudian jalankan perintah:

sudo chown -R _www:_www wordpress

Memberikan akses write ke group _www

Setelah semua file menjadi owner dan group _www, maka sekarang kita akan memberikan akses supaya semua user di dalam group _www dapat write file kita.

g+w artinya group + (tambahkan) write.

sudo chmod -R g+w wordpress

Nah begitulah tutorial konfigurasi Apache, PHP, dan MySQL di Mac OS. Jika kalian menemui kesulitan bisa tulis di kolom komentar yaa, kita sama-sama belajar bareng.

Selamat mencoba.

The above is the detailed content of Tutorial Setup Localhost Mac (ApacheMySQL , and PHP ). For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
What data can be stored in a PHP session?What data can be stored in a PHP session?May 02, 2025 am 12:17 AM

PHPsessionscanstorestrings,numbers,arrays,andobjects.1.Strings:textdatalikeusernames.2.Numbers:integersorfloatsforcounters.3.Arrays:listslikeshoppingcarts.4.Objects:complexstructuresthatareserialized.

How do you start a PHP session?How do you start a PHP session?May 02, 2025 am 12:16 AM

TostartaPHPsession,usesession_start()atthescript'sbeginning.1)Placeitbeforeanyoutputtosetthesessioncookie.2)Usesessionsforuserdatalikeloginstatusorshoppingcarts.3)RegeneratesessionIDstopreventfixationattacks.4)Considerusingadatabaseforsessionstoragei

What is session regeneration, and how does it improve security?What is session regeneration, and how does it improve security?May 02, 2025 am 12:15 AM

Session regeneration refers to generating a new session ID and invalidating the old ID when the user performs sensitive operations in case of session fixed attacks. The implementation steps include: 1. Detect sensitive operations, 2. Generate new session ID, 3. Destroy old session ID, 4. Update user-side session information.

What are some performance considerations when using PHP sessions?What are some performance considerations when using PHP sessions?May 02, 2025 am 12:11 AM

PHP sessions have a significant impact on application performance. Optimization methods include: 1. Use a database to store session data to improve response speed; 2. Reduce the use of session data and only store necessary information; 3. Use a non-blocking session processor to improve concurrency capabilities; 4. Adjust the session expiration time to balance user experience and server burden; 5. Use persistent sessions to reduce the number of data read and write times.

How do PHP sessions differ from cookies?How do PHP sessions differ from cookies?May 02, 2025 am 12:03 AM

PHPsessionsareserver-side,whilecookiesareclient-side.1)Sessionsstoredataontheserver,aremoresecure,andhandlelargerdata.2)Cookiesstoredataontheclient,arelesssecure,andlimitedinsize.Usesessionsforsensitivedataandcookiesfornon-sensitive,client-sidedata.

How does PHP identify a user's session?How does PHP identify a user's session?May 01, 2025 am 12:23 AM

PHPidentifiesauser'ssessionusingsessioncookiesandsessionIDs.1)Whensession_start()iscalled,PHPgeneratesauniquesessionIDstoredinacookienamedPHPSESSIDontheuser'sbrowser.2)ThisIDallowsPHPtoretrievesessiondatafromtheserver.

What are some best practices for securing PHP sessions?What are some best practices for securing PHP sessions?May 01, 2025 am 12:22 AM

The security of PHP sessions can be achieved through the following measures: 1. Use session_regenerate_id() to regenerate the session ID when the user logs in or is an important operation. 2. Encrypt the transmission session ID through the HTTPS protocol. 3. Use session_save_path() to specify the secure directory to store session data and set permissions correctly.

Where are PHP session files stored by default?Where are PHP session files stored by default?May 01, 2025 am 12:15 AM

PHPsessionfilesarestoredinthedirectoryspecifiedbysession.save_path,typically/tmponUnix-likesystemsorC:\Windows\TemponWindows.Tocustomizethis:1)Usesession_save_path()tosetacustomdirectory,ensuringit'swritable;2)Verifythecustomdirectoryexistsandiswrita

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor