In this tutorial, we will see how to install Nginx and PHP-FPM and Mysql on Mac and setup these environment to develop application using these technologies. We will make configuration related to sites enabled and sites available for virtual host using Nginx and make use of it to serve html and php page.
Nginx is a high performance web server which is also used as a reverse proxy and load balancer.Similarly, Mysql provides persistent storage and PHP is a server side scripting language that can be used to create dynamic and interactive web pages.Combining these 3 technology stacks makes LEMP stack and let's setup this stack today.
Mac provides a beautiful package manager called Homebrew that provides easy installation of software on MacOS. It is a tool similar to apt in linux system. It is always recommended to install any softwares with brew command to avoid permission related issues and it also makes installation and un-installation fairly simple.Check if it already installed first using following command.
In case it is not installed use following command to install it first.
You can use following command to update it if it is already installed.
To install PHP execute following commands in the terminal. The first two command register PHP repository and the last command installs it.
If you will look into the terminal, you can find all the necessary instructions to launch php-fpm on startup, PHP control script location and how to start php70 with brew.Following is the screenshot:
It will take couple of minutes to complete the installation process.In the terminal you can find all the configuration location and instructions. All the files and folder and related to this installation will be available in the location .To start the server you can use sudo brew services start nginx. The default port on which it is running is 8080. We will change these configuration to some other port as our tomcat might be running at 8080.
All the nginx configurations are available in /usr/local/etc/nginx/nginx.conf file and now we will start making changes to it's configuration file to serve html page. For this change the existing configuration under key server and location. The server key holds the port number to which nginx will be listening. By default, it is 8080. We will change it to 8090
Also, while installing nginx from brew, the default location of static files such as .html file is configured under /usr/local/Cellar/nginx/1.12.2_1/html and this can be changed to point to your custom location. In my case it is /Users/b0202781/Documents/work/workspace/www.To make these configuration, we need to modify the existing nginx.conf file as below:Original Configuration
Create a custom index.html file inside /Users/b0202781/Documents/work/workspace/www and reload the nginx using brew services reload nginx.Now invoking curl command curl -IL http://127.0.0.1:8080 should load your custom html page.
Till now all the above configurations are very simple but with Nginx you can do much more.We can configure Nginx to handle multiple virtual hosts.There are 2 things here - sites available and sites-enabled. All the available sites configuration resides in sites-available but those configuration which are present in sites-enabled are only served by Nginx.Actually, sites-enabled contains symlinks to the sites-available directory and you should always modify sites-available configuration and these changes will automatically reflect in the sites-available. To accommodate these changes we have following directory structure inside /usr/local/etc/nginx
The folders of interest here are conf.d, sites-available, sites-enabled and we have our configurations in nginx.conf. We have corresponding configuration files present inside sites-available and sites-enabled and we have include these configuration inside nginx.conf file.nginx.conf
In the sites enabled folder we have a configuration file - default that has configuration PHP. But before that we will tell Nginx about our PHP-FPM configuration. While installing PHP-FPM through brew, the default port where PHP-FPM runs is 9000 and the same need to be configured as follow.php-fpm file is inside conf.d directory.php-fpm
Once this is done, now it is time to configure our sites-available. Following is the configuration present inside sites-available directory. Check the location we have configured here.It is the location of the same file while we configured earlier for PHP. Also, we have our PHP workspace inside /Users/b0202781/Documents/work/php_workspace/default
Once these configurations are done reload the Nginx configuration using - brew services reload nginx and hit the url - localhost:8090/info to check if php is configured correctly.
Also, we have index.php in the same folder php_workspace which can be accessed at localhost:8090/index.php
I hope this article served you that you were looking for. If you have anything that you want to add or share then please share it below in the comment section.
Contact us: firstname.lastname@example.org