This guide will help you set up your own web server within minutes. Of course, you could use the likes of MAMP or XAMP to have this entire process automated in a form of an app, but in this case we will manually install everything one by one.
PART 1 - APACHE
Once you have both Xcode and Homebrew installed you can proceed with Apache.
First we need to stop the system installation of Apache from starting on boot:
We then have to stop the system installation of Apache, in case it is running in the background. We are going to install Apache using Homebrew which will allow us to control the version of Apache unlike the system version:
It is very unlikely that Apache is running on your server unless you have activated it previously therefore expect a notification that it ‘could not find specified service’.
Now we can proceed with the installation of Apache 2.4:
Once the process finishes you can check if your installation was successful. Be aware of the PATH used during installation of Homebrew-Cask as you may have changed it:
You can also check with:
This command will display the version of your server and its build.
At this stage you should be ready to start Apache:
Your website will be located under the IP address of your machine, for example http://18.104.22.168. Hopefully you’ll get a satisfying message saying “It Works!”
No we have to make sure that Apache starts automatically on startup in case you decide to reboot your machine:
In the latest version of Brew, the default port used for Apache is 8080, you should change it to 80 using the Terminal App:
Simply find the line that says Listen 8080 and change it to Listen 80
Look for AllowOverride in your file, we will need to change it to:
We also have to enable mod_rewrite
Search for the following line:
And simply remove the # (hash) just before LoadModule
At last we need to edit our ServerName which would look like this in your file:
Replace it with your IP Address or domain:
You can now restart apache:
PART 2 – PHP
First of all you need to decide which version of PHP you require ranging from 5.6 to 7.2, in this tutorial we’ll use 7.2 but installation of any other version would be much alike.
Install PHP 7.2 using brew.
You can simply replace the 7.2 with another version according to your needs, it can be 5.6, 7.0, 7.1.
Once the installation finishes you can begin editing files, beginning with php.ini. It is the default configuration file for running applications.
You can decide to change the memory_limit, upload_max_filesize or max_execution_time. You can also activate safe mode.
Now you need to go back to your httpd.conf file which we edited during the Apache installation process and add a line which will connect PHP with Apache once again use:
Add libphp somewhere below the last edit we made by uncommenting LoadModule:
At your discretion depending on the project you’re working on, you can also edit an option which will allow you to load index.php or index.html
And replace it with:
Now you can stop and start apache once again to validate the changes.
PART 3 – MYSQL
At last you can install mysql and the process is very straightforward. You can even use the Native Packages to run the whole installation process.
For this example, we will go through the process of installing it with Homebrew:
Now you can load and start services:
It will tell you to secure your installation, run the following command:
Set a new root password, remove anonymous users and other options to limit access to localhost only.
You can now restart the MySQL server:
You’re all set.