Apache Mysql Php For Mac Os



Step by step instructions to setup a local development environment running multiple versions of PHP simultaneously.

Disini, saya sudah mencoba mengaktifkan Apache dan PHP serta install MySQL dan PHPMyAdmin di Mac OS X dan macOS Sierra 10.12.3 Jika anda menggunakan Mac OS X versi yang lain, caranya hampir sama. Ikuti langkah-langkah dibawah ini: Bagaimana Cara Menginstall Apache, PHP, MySQL dan PHPMyAdmin di Mac OS X dan macOS Sierra 10.12.3. MAMP stands for: Mac, Apache, MySQL and PHP. With just a few mouse-clicks, you can install Apache, PHP and MySQL for OS X! It installs a local server environment in a matter of seconds on your OS X computer, be it PowerBook or iMac. Like similar packages from the Windows.

Sure, you could use MAMP like many other developers out there, and there’s nothing wrong with that, but setting things up this way will give you a better understanding of how all of these things come together and nearly everything you learn here will help you in the future if you need to setup Linux servers; Which in all likelihood you will do as understanding DevOps is a useful skill to have.

I took the steps below using a clean install of macOS Sierra, so hopefully I have covered everything that you need to do. macOS Sierra / Xcode is bundled with Apache and PHP, but we won’t be using these as it is an old version of PHP and messing around trying to update it could potentially break something. A disclaimer is necessary here, if you already have a functional development environment, and you decide to follow these steps, you can’t hold me responsible if it all goes wrong and you break your current setup.

Install Xcode and Xcode Command Line Tools

Open up the App Store and search for Xcode to install it.

Once it has installed, you’ll need to open Xcode at least once as there is a Terms & Conditions prompt to accept before you can use the command line tools. Once you have got past the prompt, if any appears you may have already cleared it previously, open the Terminal and enter the command below to install Xcode Command Line Tools.

The $ is just to indicate this is a command prompt, do not enter it with the command.

Ensure the system installation of Apache is not running.

Enter this command to stop Apache if it is running, the second line below (not prefixed with $) is the error I received from the command, this is to be expected as it shouldn’t be running.

This command will stop Apache from starting on boot.

Install Homebrew

To install Homebrew, check for the latest installation instructions at http://brew.sh/, but when I installed it I ran the command below.

Homebrew has a utility that will check if everything is setup correctly, run this after installation.

Add Homebrew taps

taps are extra repositories containing formulae to install software, we will need to add these by entering the commands below.

Enable auto-completion for brew commands (optional)

You can skip this step if you want, but to help you use brew, you can enable auto-completion for commands; So when you type brew and mash the tab button you’ll see what commands you can run.

After installation you should see a message telling you how to enable auto-completion, mine told me to add this to ~/.bash_profile

Once you have done this, open a new terminal (or tab), and now you should have auto-completion hints when typing brew and then hitting tab several times.

Install Apache

Now we are ready to install Apache 2.4

The next step involves using apachectl again, you may remember we used this right at the start to disable the system version of Apache. But now that we have installed Apache using Homebrew, apachectl will be controlling our version of Apache, not the system version. So let’s check this has worked by typing:

Homebrew installs everything within /usr/local/ so there is no danger of it conflicting with any system-level software. If you get anything else returned as the file path for apachectl, then something hasn’t been setup properly, likely your PATH isn’t setup correctly, run brew doctor again to check.

Assuming everything is fine, we are now ready to start Apache. You’ll need to use sudo with this command because it opens port 80. Don’t use sudo when you don’t need to, it could mess up file ownership and/or permissions.

Now open your web browser and go to http://127.0.0.1, you should see a message saying “It works!”

You should now set Apache to launch on startup.

Install PHP

I’ll be installing PHP 5.6 and 7.0, as previous projects I have worked on are deployed to servers running PHP 5.x, so it is sensible to try to match your development environment to your production environment.

Install PHP 5.6

Because we will be installing multiple versions of PHP, we need to change the default PHP-FPM (FastCGI Process Manager) port from 9000 to something else, so I suggest setting it to 9056.

Edit /usr/local/etc/php/5.6/php-fpm.conf and replace this line:

With this line:

Now we are ready to start PHP-FPM (this will also set it to launch on startup).

Setup Apache to work with PHP-FPM

Edit /usr/local/etc/apache2/2.4/httpd.conf

We need to enable some Apache modules, they have been commented out, so find these lines and remove the # to uncomment them.

So it should now look like…

We need to configure Apache to attempt to load index.php by default, so find these lines.

And change it to this.

We also need to enable virtual hosts, so find this line and uncomment it.

Now save the file.

We now need to edit the virtual hosts configuration file /usr/local/etc/apache2/2.4/extra/httpd-vhosts.conf

By default, all of your virtual hosts would go into this file, but it would soon become a mess once you have setup just a few sites. It’s better to use individual files, so either delete or comment out all the lines in this file, and add this to the end.

This will read all the files with the .conf extension in that folder, so to add/edit/remove a virtual host, we just add/edit/delete the individual file. Now we are ready to setup our first virtual host. Create a directory for the config files to live in.

Now create a file for our virtual host /usr/local/etc/apache2/2.4/vhosts/_localhost.conf and enter the configuration below.

Now we need to create the folder for the site files.

Now create an index.php file with the following code in that folder.

Because we have changed Apache’s settings, we need to restart it.

To get our new virtual host to work in the browser, we need to edit our /etc/hosts file. You’ll need to use sudo to edit this file, so assuming you are comfortable using Vim…

Add this line to the bottom of the file.

Now open your web browser, and go to http://php56.apache.localhost, you should see the output from phpinfo() showing PHP 5.6.29 is installed.

You’ll also see an error saying “It is not safe to rely on the system’s timezone settings. You are required to use the date.timezone setting or the date_default_timezone_set() function.”

To resolve this edit /usr/local/etc/php/5.6/php.ini, and find this line.

Remove the ; to uncomment the setting and set the timezone to UTC.

Now we have changed PHP’s settings, we need to restart PHP-FPM.

If you refresh the browser you’ll now see that the error has gone.

Now we have one version of PHP working with Apache, let’s install PHP 7.0.

Install PHP 7.0

First we need to run this, otherwise the installation will fail as there will conflicts.

Now we can install PHP 7.0 with the same extensions as above.

Again, because we are installing multiple versions of PHP we need to change the default FPM port from 9000 to something else, so I suggest setting it to 9070. The layout of the config files is slightly different in this version of PHP.

Edit /usr/local/etc/php/7.0/php-fpm.d/www.conf and replace this line:

With this line:

To avoid the same error we had before, edit /usr/local/etc/php/7.0/php.ini and find:

Replace the line with:

Now we are ready to start PHP-FPM (this will also set it to launch on startup).

Edit our existing Apache virtual host config file to test PHP 7.0 is working, edit /usr/local/etc/apache2/2.4/vhosts/_localhost.conf and add this block to the end of the file.

Because we have changed Apache’s settings, we need to restart it.

Again, we need to create an entry in our /etc/hosts file.

Add this line to the end of the file.

Now open your web browser, and go to http://php70.apache.localhost, you should see the output from phpinfo() showing PHP 7.0.14 is installed.

Install MySQL

Now lets start MySQL and get it to run on startup.

I would recommend you setup a password for root, and only allow access from localhost. There is an easy way to do this.

It’s an interactive prompt, here were my responses.

I’ve noticed that since working on older projects, that MySQL is returning errors to PHP when doing SELECT DISTINCT or GROUP BY queries. Turned out it the default sql_mode in MySQL 5.7 is more restrictive than previous versions. Now, by default it is:

Now this is fine to leave as the default, I would recommend you leave the settings alone unless you have problems.

Initially I tried fixing some of the custom queries, but then started noticing problems from queries generated by WordPress’ get_posts() function, and as I work on so many different WordPress sites I figured that I would end up wasting loads of time trying to fix these on other sites.

You can override this setting by creating a config file for mysql, create a file /usr/local/etc/my.cnf with these settings:

Now if you restart the MySQL server, you shouldn’t be getting these errors.

Summary

Now you should have everything you need to get started, each time you need to work on a new project, reference the previous steps to setup a new virtual host for it and restart Apache. In the future, if you need to install a different version of PHP, you can reference the steps here, and it should work.

Install apache mysql php mac os x

If you got stuck at any point, and want to reference my configuration files, you can browse them here:https://github.com/lukearmstrong/localhost

Further Reading

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.

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.

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/

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

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

Mysql Apache Php Server

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.

Apache Php Mysql Mac Os X Catalina

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:

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.

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.

Apache Mysql Php For Mac Os 10.13

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.

Apache Mysql Php For Mac Os 10.7

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.