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.
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. Open your Safari (or any browser) and go to localhost. If you see It Works! then the Apache is up and ok.
Turn on the PHP
PHP also goes with MacOs and in the 7.1 version if you are on the High Sierra. Here is how to turn on the PHP on your MacOS:
step 1: Opet the Terminal and type in:
sudo nano /etc/apache2/httpd.conf
step 2: That command will in nano editor open the file httpd.conf so press Ctrl+W to start the search in nano editor and then type in the “PHP” and press Enter. Search will place you into the part where are the PHP settings.
step 3: In front of the row that looks like this:
LoadModule php7_module libexec/apache2/libphp7.so
remove the # .
step 4: Press Ctrl+O to WriteOut and then press Enter to save the settings. Use Ctrl+X to exit from nano editor.
step 5: In the Terminal type in sudo apachectl restart and press Enter, then type in your password in order to restart the Apache. PHP is now active so you can go further.
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. In that file place this text:
<?php
echo "hello people!";
phpinfo();
?>
Save that and open the Terminal. In Terminal type in the following:
sudo nano /etc/apache2/httpd.conf
Use shortcut Ctrl+W 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/name-of-your-user/Sites
Save the changes with Ctrl+O, and exit from nano editor with Ctrl+X. Do not close the Terminal 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:
Install the MySQL
This is how to install MySQL on MacOS:
step 1: go to https://dev.mysql.com/downloads/mysql/
step 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.
step 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.
step 4: Afer the installation, go to System Preferences and search for MySQL. You need to see the following:
step 5: Go to the following page Sequel Pro https://www.sequelpro.com/ Sequel Pro is the application like the PHPmyAdmin, but with desktop interface with which you can manage your databases. Run the installation.
step 6: Connect now Sequel Pro with MySQL. For the host enter 127.0.0.1, for the username enter root, and for the password enter password for the MySQL.
step 7: After you connect, click on the Choose Database, and then on the Add Database.
In the field that opens, enter a name for the new database and click on the Add button:
step 8: In the left corner you can find the “plus” (+) icon, click on + in order to add a new table in the database. Name it ad you wish, for example mydbtable1 and click on Add:
step 9: In the panel, above INDEXES, click on + icon. For the id enter a message, for the Type choose VARCHAR and for the Lenght write in 200.
step 10: Click on the Content icon, and then on the + icon on the bottom of that panel. In the entry write 1 for id and you can write “mysql ok” for the message.
step 11: 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", "password-goes-here", "database-name");
$message = $con->query("SELECT message FROM table-name-from-database")->fetch_object()->message;
$con->close();
echo "$message <br/>";
echo "hi people!";
phpinfo();
Open the Safari and go to localhost:
Here is what we do here and why you need this
We first activated the Apache web server – necessary for the site to work, whether it was a site on a local computer or on a web server. Then we turned on PHP – need for the PHP site to work on your computer (eg WordPress). We made a Sites folder that serves for the files of your site that you will develop on your computer – for example, on hosting it is a folder called /public_html.
It is important that you know that you put the files of the future site in the Sites folder, and you create a MySQL database through the SequelPro application, allowing a site to run on your computer.
Please note that the index.php file we set up was useful for testing, but before setting up a new site, you need to delete this file. For example, when you unpack the WordPress site (in the Sites folder), the index.php will come with WordPress files.
Thank you for this walkthrough/tutorial. You are a good teacher!
This was SUPER helpful. Much appreciated!
Dear Brittany, thank you 🙂
I just upgraded to Ventura — everytime the OS is upgraded it destroys the apache configs, this site helped me fix my “It works” issue when navigating localhost and not seeing the /Sites directory.
Thanks for that…