Php Apache And Mysql For Mac



I recently purchased brand new MacBook Pro and I had to install NGINX, PHP and MySQL in my MacBook Pro. In this article, I am going to share my experience for how to install NGINX, PHP and MySQL on your Mac. This article is for someone interesting to set-up web development environment, especially for the first time.

Reson I choose Nginx over Apache web server is its light-weight resource utilization and its ability to scale easily. Also, Niginx has grown its popularity during last few years. Furthermore, Nginx is often selected by administrators for its resource efficiency and responsiveness under the heavy load.

  • Mac LAMP development with MAMP (Mac OS X, Apache, MySQL, and PHP) First of all, MAMP is a terrific, free all-in-one Mac LAMP/PHP solution. Just download MAMP and install the MAMP folder in your Mac Applications folder, and you're almost ready to start your Mac/Apache/MySQL/PHP project. In fact, if you don't work from the Mac OS X command line.
  • The free web development solution with Apache, Nginx, PHP & MySQL What is MAMP? MAMP installs a local server environment in a matter of seconds on your Windows or macOS computer.
  • The MAMP package includes the Apache web server, PHP engine, and MySQL database. Mac OS X 10.5 and later include the MySQL database and Apache server with PHP support. While it certainly is possible to configure the built-in database and server with the IDE, MAMP’s all-in-one bundle provides a convenient and easily configurable solution.
  • For Mac OS The package has been set up to be very easy to install and to use like Windows OS. To install and run it on Mac OS download offline installer setup. It is an easy to install Apache distribution for Mac OS X. The package for Mac is similar to windows. It includes the Apache web server, MySQL, PHP, Perl, an FTP server and phpMyAdmin.

My MacBook Pro installed Mac OS High Sierra and it was shipped with pre-installed PHP package. So, depending on your web development environment setup, you may need to upgrade default PHP version to the latest version.

Normally, Mac OS X doesn’t ship with its own copy of MySQL. So, you will have to install MySQL on your local machine.

This is a list of notable AMP (Apache, MySQL/MariaDB, Perl/PHP/Python) software stacks for all computer platforms; these software bundles are used to run dynamic Web sites or servers.There are LAMPs (for Linux); WAMPs (for Windows); MAMPs (for macOS) and DAMPs (for Darwin); SAMPs (for Solaris); and FAMPs (for FreeBSD).

Also, remember this article only explain to install these required packages on your local machine. Alternatively, you can set up similar development environment by using Docker or Vagrant. I will write another post in this regarding.

Now let’s have a look how to install NGINX, PHP and MySQL on Mac OS local environment.

Install NGINX

I prefer to use Homebrew package manager to install required additional packages on my Mac OS X.

If you haven’t install Homebrew on your Mac, you just need to copy paste following command on your terminal and Homebrew will install on your Mac automatically. Remember you need an Internet connection to download these packages.

Once you installed Homebrew, you can install NGINX by using the following command.

Nginx will set following document root by default. But you can change to any path using nginx.conf file.

NGINX default document root

/usr/local/var/www

Configure NGINX virtual hosts

I have included my pre-configured virtual hosts as a zip file.

So you can download my pre-configured NGINX virtual host files here.

Once you download the zip file, unzip the contents and then you just need to copy into the following path. Furthermore, my NGINX virtual hosts specially configured for developing Magento 1, Magento 2 and WordPress projects in my local.

Step 1 – Rename existing “nginx” folder /usr/local/etc/

Step 2 – Copy downloaded “nginx” folder in to /usr/local/etc/

Apache Php Mysql Mac High Sierra

Step 3 – Change your system username

All the pre-configured NGINX virtual hosts are located under the following path.

/usr/local/etc/nginx/servers

You will find four configuration files under the above path.

  1. 00_upstream.conf – For Fast CGI upstream
  2. 10_localhost.com.conf – For Magento 1 hosts
  3. 20_localhost.com.conf – For Magento 2 hosts
  4. magestyle.conf – For WordPress and general hosts

Also, you will notice, each virtual host’s document root is defined under the above configuration files.

Update your host file

Also, you will need to update your host file for the custom virtual host names. So you can have a look my host file as an example.

NGINX server state management

Start NGINX Server

Reload NGINX Server

Stop NGINX Server

Mikrotik winbox port. Test and verify NGINX services

My local configuration examples:

For Magento 1 projects

Local Document Root – /Users/chatura/htdocs/m1-local

Host name – http://m1-local.localhost.com/

Configuration file path – /usr/local/etc/nginx/servers/10_localhost.com.conf

For Magento2 projects

Local Document Root – /Users/chatura/htdocs/m2-local/web

Host name – http://m2-local.m2.localhost.com/

Configuration file path – /usr/local/etc/nginx/servers/20_localhost.com.conf

For WordPress and other projects

Local Document Root -/Users/chatura/htdocs/wp-test

Host name – http://wp-test.localhost.com/

Local Document Root – /usr/local/etc/nginx/servers/magestyle.conf

Install PHP

If you follow the following steps, you can replace default PHP with Homebrew PHP version. Also, you can easily upgrade or switch your PHP version according your project requirement.

Especially I am going use my development setup for Magento 2 development. So I followed the Magento technology stack documentation before decide which PHP version I need to install. So I am going to install PHP 7.1 version in my local environment as per the Magento 2 latest technology stack.

Use following “brew tap” command to allow Homebrew to tap into Homebrew PHP to formulae. Once you’ve done this, you’ve expanded your options of installable PHP versions. So these additional Git repos will be saved inside the (usr/local/Library/Taps) directory,

To check list of available configuration options

Install PHP without Apache and with PHP FPM

Install additonal PHP extensions

Setup PHP CLI binary

In addition, if you want to use the PHP command line tools, you need to update the $PATH environment variable of your shell profile.

If you use the default Bash shell:

If you use ZSH:

Free office programs for mac. If you are not sure which one you use, run following code in your terminal. I am using ZSH and it returns “/bin/zsh”

Configure PHP using PHP-FPM

PHP-FPM is a modern way of running PHP with a process manager and the FastCGI protocol. Also, this enables high performance as well as easy switching between various versions of PHP.

Php Apache And Mysql For Mac

Note: Also you can install many PHP version with Homebrew package manager. You just need to link required PHP version and unlink other PHP versions.

Check Installed PHP version and php.ini file

PHP service management

Install MySQL

Let’s install MySQL server.

Also, you can use the following command to MySQL server gets automatically started and stopped when the Mac is shutdown/powered on.

So, you can start MySQL start service manually using the following command for now.

MySQL service management

Test MySQL connection

I hope this article will help you to install Nginx, PHP, MySQL on your Mac OS. Let me know in case you’re stuck at some point or have general feedback. Please feel free to comment below.

I have been always playing with the Apache, PHP, and MySQL every time but since when I switched to Mac and wanted to make a web development environment on my Mac. So the first step is to install Apache, PHP, and MySQL.

Install Apache for Mac OS

Since Mac OS is ultimately built on Unix so it is very to install any Unix supporting software. The most interesting part is that Mac OS comes with the Apache web server and we just need to start it and make some changes to setup our development environment.

Now, We need to start the apache server with root privileges to avoid permission issues.

Therefore, just launch the terminal type the command as follows:-

After starting Apache – test to see if the webserver is working in the browser – http://localhost – you should see the “It Works!” text.

Creating Apache VirtualHosts

The default location for Apache webserver project files is /Library/WebServer/Documents/. In this location, you all can create your application in order to host your web applications.

To enable VirtualHosts, we need to make some changes in the Apache Configuration file (httpd.conf).

Make sure that you change your DocumentRoot, ServerName and Directory location according to your needs.

Now your VirtualHosts setup is done now, just restart the apache with command

Mysql

Install PHP on Mac OS X

In order to install PHP, in Mac OS you will just need to enable PHP support for Apache, i.e, to need to make changes in its (httpd) configuration file.

Open httpd.conf file with the command as follows:-

Uncomment below the line and save it.

LoadModule php7_module libexec/apache2/libphp7.so

Now restart the server and it should be able to process PHP files. To test it we can create a simple PHP file as below. Video editing for mac os x.

Create test.php file in the /Library/WebServer/Documents/ directory with the following lines of code as shown below and save it.

Load the PHP file in browser with URL http://localhost/test.php and you should see a huge page with all the PHP configurations as shown below:-

Congrats now you have successfully installed PHP on your Mac OS.

Installing MySql on Mac OS

Installing MySQL in Mac OS is very simple you will just have to download MySQL dmg file from the MySQL website and just press next, next in order to install.

Download the MySql DMG file from MySql Download Website.

Open the DMG file and install MySql server

Start the MySql server if it’s not running and you can find MySQL in system preferences.

Click on MySQL and you will see the screen as below:-

If you want to control MySQL through your terminal then you will have to add MySQL directory to your shell path, this is done in your “.bash_profile” file in your home directory if you don’t have that file just create it using vi or nano:

open that “.bash_profile” and add the following line and save it.

export PATH=${PATH}:/usr/local/mysql/bin/

Windows Apache Php Mysql

Now, you can access MySQL through your terminal, in order to check just type the command as follows:-

Now, you have successfully connected your MySQL server through your terminal.

If you want to change the MySQL root password then just login to MySQL and the query as follows:-

Change the lowercase ‘MyNewPass’ to what you want – and keep the single quotes.

Now you have successfully installed all your web development environment. If you want to GUI to manage MySQL then you can install MySQL workbench.

If you have any queries do post your questions in the comment section below: 🙂