If you want to do a website development on your MacOS you also need to set localhost environment on your Mac.
In this way, you can create a website or application and then upload it to web hosting.
Here you can read how to set your Mac in order to have a development environment for your needs.
MacOS Version: 10.15.3 (Catalina)
Step 1. Turn on the Apache
Apache is already on your Mac, you just need to turn it on. Open the Terminal and type the following:
sudo apachectl start
User password will be requested, so you need to type in the password and then press Enter.
Then. Open your Safari (or any browser) and go to localhost.
If you see It Works! then the Apache is up and ok.
Step 2. Turn on the PHP
PHP also goes with MacOs and in the 7.3.11 (Maybe) version if you are on the Catalina.
php -version
Here is how to turn on the PHP on your MacOS:
1: Open the Terminal and type in:
sudo vim /etc/apache2/httpd.conf
That command will in Vim editor open the file httpd.conf so press / to start the search in vim editor and then type in the “PHP” and press Enter. Search will place you into the part where are the PHP settings.
In front of the row that looks like this:
LoadModule php7_module libexec/apache2/libphp7.so
remove the # .
Press :wq and then press Enter to save the settings and Quit.
2. In the Terminal type in sudoapachectl restart and press Enter, then type in your password in order to restart the Apache. PHP is now active so you can go further.
Step 3. Make Sites folder in Home
Open Finder, click on Go and go to Home. Create a new folder and name it as Sites. A folder will automatically get the Safari icon:
When you finish that, go to folder Sites, and create a new file with the index.php name.
sudo vim ~/Sites/index.php
In that file place this text:
phpinfo();
Save that and open the Terminal. In Terminal type in the following:
sudo vim /etc/apache2/httpd.conf
Type / to search this file, and type in Library in order to search for Library. You need to see the following:
Now, in both rows, edit the existing:
/Library/WebServer/Documents
into:
/Users/Matthew/Sites
Save the changes. And type:
sudo apachectl restart
Open the Safari and go to localhost address. You need to see the following from the image below. This page says that your index.php file serves the information that you enter to this file:
2: Click on the download button to download the DMG package for MacOS. This will open the page, and you can click on No thanks, just start my download.
3: Run the installation of this file. Pay attention to enter the Root password for MySQL when installation wizard requests that. Root password can be any by your choice. Check in the Use Legacy Password Encryption. Keep in mind that this is not your Mac password, but a new one for MySQL.
4: Afer the installation, go to System Preferences and search for MySQL. You need to see the following:
5: Connect now some tools with MySQL. For the host enter 127.0.0.1, for the username enter root, and for the password enter password for the MySQL.
6: After you connect, Create new database test:
7: Open the ‘test’ database, Create test_table. For the id enter a message, for the Type choose Varchar(255).
8: Write “Hi MySQL” for the message.
9: Go back to your index.php file in the Sites folder and change the content of that file with the following content (or just add), but pay attention to password-goes-here, database-name, table-name-from-database and replace that with your data:
<?php
$con = new mysqli("127.0.0.1", "root", "xxxxxxxxx", "test");
$message = $con->query("SELECT message FROM test_table")->fetch_object()->message;
$con->close();
echo "$message <br/>";
phpinfo();
?>