Unlike in Windows, it is perfectly posible to easily configure customized local web server in mac os with the exact versions of softwares you need. I will discuss how to install nginx, MySQL and Php 7.3. But you can select any version you prefer to install.
Homebrew
Homebrew is a widely used package manager for mac-os which helps you to install various unix packages. Bellow command is used to install homebrew as stated in their official page.
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"
Nginx
Nginx is a lightweight powerful web server which can be configured easily. And it can be used to do lots of things like revers proxis, load balancing etc. You can simply install and start nginx on mac using bellow commands respectively.
brew install nginx brew services start nginx
Now, the web server is ready and you can access it using http://localhost:8080 as the default port for nginx is 8080. The default nginx page is something similar to bellow.
Well, lets modify the configuration so that we can access the website using http://dev.test.com/ URL. To do that, open the nginx configuration file
vim /usr/local/etc/nginx/nginx.conf
In order to avoid getting the 403 error, we should grant proper permission to Nginx. For that, bellow line should be as the first line of your nginx configuratioin file (replace [your_user_name] accordingly).
user [your_user_name] staff;
Inside http directive you will see the default site is configured starting from server { . Similarly we will add a new server section to add our new website.
server { listen 80; server_name dev.test.com; root /Users/[your_user_name]/path/to/your/website; index index.html index.htm; }
Now we should restart the nginx server in order to apply the changes we did.
brew services restart nginx
Configuration is all set for your website. Now, navigate to the project path you mentioned in the configuration file and create an index.html file with some content ( I’m not going to explain HTML content here )
/Users/[your_user_name]/path/to/your/website
Once you create and save the html file, you can view it through browser with http://dev.test.com/ now.
Php
I will install and start php 7.3 fpm using bellow commands respectively.
brew instal php@7.3 //brew install php command will install latest version of php. Here //we need 7.3 specific version brew services start php@7.3
Then we re-modify the server section we inserted in nginx configuration file above to read php files. add index.php in the default file type line
index index.php index.html index.htm;
Moreover add bellow lines in the server section to instruct to the nginx server, how to read files with .php extention
location ~ \.php { fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; fastcgi_pass 127.0.0.1:9000; fastcgi_split_path_info ^(.+\.php)(/.+)$; fastcgi_buffers 16 16k; fastcgi_buffer_size 32k; }
Php also need the proper permision to read the files. Therefor we will grant it in php configuration file
vim /usr/local/etc/php/7.3/php-fpm.d/www.conf
Do the changes mentioned bellow in this file.
// You will find two parameters user and group like this user = www group = www // modify those lines to have user = [user_user_name] group = staff
Now create a php file in your project directory named index.php and add some php code there. Finally we need to restart both nginx and php-fpm in order the changes to take effect.
brew services restart nginx brew services restart php@7.3
All good and now you can load the php file you created above, through the browser using http://dev.test.com/
MySQL
At this point of writing the article, MySQL already has released it’s version 8.0 and homebrew also set default version to 8. Since we are aiming to install 5.7 version, we do it by appending the version at the end.
brew instal mysql@7.3 // and start it using bellow command brew services start mysql@5.7
After that we should add MySQL path to zsh shell PATH
echo 'export PATH="/usr/local/opt/mysql@5.7/bin:$PATH"' >> ~/.zshrc
This change will apply only after you restart the current terminal. Since we have not yet set a password for root, you can connect to MySQL using bellow command.
mysql -u root
Securing your MySQL Installation
Once everything is done, our next step is to securing the MySQL by setting up a password for root user. You will do it by executing bellow command
mysql_secure_installation
- You will have to set a password to root user. Make sure to keep this password as you will not be able to access the database without it.
- Remove anonymous users? – Yes, since this is a local installation, there is no need for anonymous users to access.
- Disallow root login remotely? – Yes, Similarly, there is no need to access this local database remotely.
- Remove test database and access to it? – Yes, We don’t need test database or test data as we are not going to access them anymore.
- Reload privilege tables now? – Yes, This will refresg the user privileges properly.
After this step, you can connect to the MySQL using the newly created root password.
mysql -u root -p // it will prompt for the password
Conclusion
During this article we discussed how to set up your development environment properly on Mac-Os by installing Php 7.3, Nginx and MySQL. Also we discussed about the configurations of each software and the security concerns you should be aware of when you use MySQL in your development environment.