banner



How To Install Apache Web Server On Mac

Function 1: macOS xi.0 Big Sur Web Development Environment

Developing web applications on macOS is a real joy. There are plenty of options for setting upwards your development environments, including the e'er-popular MAMP Pro that provides a nice UI on meridian of Apache, PHP and MySQL. However, there are times when MAMP Pro has slow downs, or out of date versions, or is just behaving badly due to its restrictive system of configuration templates and non-standard builds.

It is times like these that people frequently await for an alternative arroyo, and luckily there is 1, and it is relatively direct-forward to setup.

In this blog post, we volition walk you through setting up and configuring Apache 2.4 and multiple PHP versions. In the 2d blog post in this 2-post series, we will cover MySQL, Apache virtual hosts, APC caching, and Xdebug installation.

10/31/2021 Added dynamic back up for Apple Silicon and Intel homebrew paths
ten/29/2021 Updated to reverberate macOS 12.0 Monterey and removed PHP 5.6
11/27/2020 Updated to add together some information on PHP 8.0
xi/13/2020 Updated to reflect the release of macOS 11.0 Large Sur
12/02/2019 Updated to reflect the latest release of PHP 7.4 and the removal of PHP seven.1 from Official tap
12/02/2019 Updated to reverberate the latest release of PHP vii.iv and the removal of PHP seven.1 from Official tap
10/08/2019 Updated to reflect the release of macOS 10.v Catalina
01/x/2019 Updated to add back PHP v.6 and PHP 7.0 from and external deprecated keg
12/12/2018 Updated to reflect the latest release of PHP 7.3 and the removal of PHP 7.0 from Brew.

If you accept followed this guide in the past with the Homebrew/php tap, and are looking to upgrade to the new Homebrew/cadre approach, then you should showtime make clean-up your current installation past following our new Upgrading Homebrew.

This guide is intended for experienced web developers. If you are a beginner developer, you volition be better served using MAMP or MAMP Pro.

XCode Command Line Tools

If you don't already accept XCode installed, it's best to first install the command line tools as these volition be used by homebrew:

          xcode-select --install        

Homebrew Installation

This process relies heavily on the macOS package manager called Homebrew. Using the brew command yous tin easily add powerful functionality to your mac, just outset we take to install information technology. This is a unproblematic process, but you lot need to launch your Last (/Applications/Utilities/Concluding) application and and then enter:

          /bin/fustigate -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"        

Just follow the terminal prompts and enter your password where required. This may take a few minutes.

If this is a fresh install and you don't take your path setup properly, yous tin can follow the installation "side by side steps" which are already customized for you, or y'all tin can manually add together the following paths to your .bashrc or .zshrc:

          eval "$(/opt/homebrew/bin/brew shellenv)"        

Now you can exam your installation to ensure you lot take installed brew correctly, only blazon:

          brew --version        
          Homebrew iii.3.1  Homebrew/homebrew-core (git revision 1362c572e16; last commit 2022-x-29)        

You lot should probably also run the following command to ensure everything is configured correctly:

          brew doctor        

It will instruct y'all if you need to correct anything.

Monterey Required Libraries

When installing fresh on Monterey, I ran into a few libraries that were missing when completing all the steps below. To make things easier, please simply run this now:

          brew install openssl        

Apache Installation

The latest macOS 12.0 Monterey comes with Apache ii.4 pre-installed, however, it is no longer a uncomplicated job to use this version with Homebrew because Apple has removed some required scripts in this release. Even so, the solution is to install Apache ii.four via Homebrew and then configure information technology to run on the standard ports (80/443).

If you already have the built-in Apache running, information technology will demand to be shutdown first, and whatsoever auto-loading scripts removed. It really doesn't hurt to only run all these commands in lodge - even if it'southward a fresh installation:

          sudo apachectl stop sudo launchctl unload -w /System/Library/LaunchDaemons/org.apache.httpd.plist two>/dev/zippo        

Now we need to install the new version provided by Brew:

          brew install httpd        

Without options, httpd won't demand to exist congenital from source, so it installs pretty quickly. Upon completion you should see a message similar:

          🍺  /opt/homebrew/Cellar/httpd/2.4.51: 1,660 files, 32.0MB        

Now we only need to configure things so that our new Apache server is automobile-started

          brew services commencement httpd        

You now have installed Homebrew's Apache, and configured it to auto-outset with a privileged account. Information technology should already be running, and so y'all can try to reach your server in a browser by pointing information technology at http://localhost:8080, you should meet a uncomplicated header that says "It works!".

Troubleshooting Tips

If you lot get a message that the browser tin't connect to the server, showtime cheque to ensure the server is up.

          ps -aef | grep httpd        

You lot should see a few httpd processes if Apache is up and running.

Attempt to restart Apache with:

          brew services restart httpd        

You can watch the Apache fault log in a new Terminal tab/window during a restart to encounter if anything is invalid or causing a problem:

          tail -f /opt/homebrew/var/log/httpd/error_log        

Apache is controlled via the brew services command so some useful commands to employ are:

          brew services cease httpd mash services start httpd mash services restart httpd        

Visual Studio Code

In past guides, I've always provided instructions to edit files using the default TextEdit application that comes pre-installed. However, this is not what I use myself as it's a terrible editor and when testing my guide for Large Sur, I kept running into problems with encoding, finding line numbers etc. The amend solution is to simply install a ameliorate editor. Then please install the amazingly versatile yet, 100% free, Visual Studio Lawmaking. It's bachelor on Mac, Windows, and Linux, but correct now nosotros just care about the mac version.

We tin can make use of the HomeBrew and install VSCode and the accompanying lawmaking CLI command in one shot with:

          mash install --cask visual-studio-code        

If you already have Visual Studio Lawmaking installed, you can easily create a lawmaking symlink with:

          ln -s /Applications/Visual\ Studio\ Code.app/Contents/Resources/app/bin/lawmaking /opt/homebrew/bin/lawmaking        

Apache Configuration

Now that nosotros have a working spider web server, we will want to do is make some configuration changes and then it works better every bit a local development server.

In the latest version of Brew, you lot have to manually fix the listen port from the default of 8080 to 80, so we volition need to edit Apache'southward configuration file /opt/homebrew/etc/httpd/httpd.conf.

If you followed the instructions in a higher place y'all should be able to utilize Visual Studio Code to edit your files using the code Final control. Even so, if you want to employ the default TextEditor awarding to perform edits, you tin utilise the open -e command followed by the path to the file.

          lawmaking /opt/homebrew/etc/httpd/httpd.conf        

VSC

Find the line that says

          Listen 8080        

and alter it to fourscore:

          Heed 80        

Next we'll configure information technology to use the to alter the document root for Apache. This is the folder where Apache looks to serve file from. By default, the document root is configured as /opt/homebrew/var/www. As this is a evolution auto, let'south assume we desire to change the document root to indicate to a binder in our own dwelling directory.

Search for the term DocumentRoot, and yous should see the following line:

          DocumentRoot "/opt/homebrew/var/www"        

Change this to point to your user directory where your_user is the name of your user account:

          DocumentRoot "/Users/your_user/Sites"        

You also demand to alter the <Directory> tag reference right below the DocumentRoot line. This should also be changed to point to your new certificate root also:

          <Directory "/Users/your_user/Sites">        

In that same <Directory> block you will find an AllowOverride setting, this should be changed as follows:

          # # AllowOverride controls what directives may exist placed in .htaccess files. # It can be "All", "None", or any combination of the keywords: #   AllowOverride FileInfo AuthConfig Limit # AllowOverride All        

Likewise we should at present enable mod_rewrite which is commented out by default. Search for mod_rewrite.then and uncomment the line past removing the leading # by pushing + / on the line (this is a quick way to uncomment and comment a single or multiple lines:

          LoadModule rewrite_module lib/httpd/modules/mod_rewrite.so        

User & Group

Now we have the Apache configuration pointing to a Sites folder in our home directory. I problem still exists, all the same. By default, apache runs every bit the user daemon and group daemon. This will cause permission problems when trying to access files in our home directory. About a 3rd of the way downwards the httpd.conf file there are two settings to fix the User and Group Apache will run under. Alter these to match your user account (replace your_user with your real username), with a group of staff:

          User your_user Group staff        

Servername

Apache likes to accept a server name in the configuration, simply this is disabled by default, so search for:

          #ServerName world wide web.instance.com:8080        

and replace it with:

          ServerName localhost        

Sites Folder

Now, you need to create a Sites folder in the root of your home directory. You can do this in your terminal, or in Finder. In this new Sites folder create a uncomplicated index.html and put some dummy content in it like: <h1>My User Web Root</h1>.

          mkdir ~/Sites echo "<h1>My User Web Root</h1>" > ~/Sites/index.html        

Restart apache to ensure your configuration changes accept taken result:

          brew services stop httpd brew services start httpd        

If you lot receive an error upon restarting Apache, try removing the quotes effectually the DocumentRoot and Directory designations we set up before.

Pointing your browser to http://localhost should brandish your new message. If y'all accept that working, nosotros can move on!

Makes sure you lot remove the :8080 port we used earlier. Likewise, you might need to Shift + Reload to clear the browser cache and selection up the new file.

Troubleshooting Non-Sudo httpd Services Offset

I will be updating this section with more tips and things to try as I find solutions to common problems. Please hop on our #macos Discord Chat to get some "alive" help.

This yr, with macOS Big Sur, I've switched from using sudo to launch httpd with root (even though it ran equally the user/group defined in httpd.conf), and for people who have upgraded from that version to this, there have been bug.

I ran into some problems myself but was able to get information technology working pretty easily, but others take reported more than wide-spread problems. Delight try these steps if your Apache is not starting when yous use brew services commencement httpd.

First, try to start apache direct with:

          /opt/homebrew/bin/httpd -m get-go        

This bypasses the brew services command and often prints out specific issues. If you have issues reported about not beingness able to write to log files, try removing all the current log httpd log files:

          rm -Rf /opt/homebrew/var/log/httpd/*        

Then try starting again.

If you accept see a message saying something like Address already in use: AH00072: make_sock: could not bind to address, try irresolute the Mind config in httpd.conf to:

          Listen 0.0.0.0:80        

PHP Installation

If you have existing PHP installations via Brew, you need to first cleanup your setup with our Upgrading Homebrew guide before continuing with this section.

Up until the end of March 2022, all PHP related brews were handled past Homebrew/php tab, but that has been deprecated, then at present we use what'southward available in the Homebrew/core bundle. This should be a better maintained, but is a much less complete, set of packages.

PHP 7.0, and PHP 7.1 accept been deprecated and removed from Brew because they are out of support, and while it's not recommended for production, there are legitimate reasons to test these unsupported versions in a development surroundings. These versions as well need to "built from source" in lodge to apply the latest versions of icu4c and openssl.

Remember only PHP 7.2 through 8.1 are officially supported by Brew, but these likewise have to exist built which is pretty slow. For the latest version of our guide nosotros will utilise the new tap from @shivammahtur as there are many versions (including the latest PHP eight.1) pre-built.

          brew tap shivammathur/php        

We will keep by installing various versions of PHP and using a unproblematic script to switch betwixt them every bit we need. Feel costless to exclude whatsoever versions you lot don't want to install.

          brew install shivammathur/php/[email protected] brew install shivammathur/php/[email protected] brew install shivammathur/php/[email protected] mash install shivammathur/php/[email protected] brew install shivammathur/php/[electronic mail protected] brew install shivammathur/php/[e-mail protected]        

As well, you may have the need to tweak configuration settings of PHP to your needs. A common matter to change is the retention settings or the appointment.timezone configuration. The php.ini files for each version of PHP are located in the following directories:

          /opt/homebrew/etc/php/7.0/php.ini /opt/homebrew/etc/php/7.1/php.ini /opt/homebrew/etc/php/7.2/php.ini /opt/homebrew/etc/php/7.3/php.ini /opt/homebrew/etc/php/7.4/php.ini /opt/homebrew/etc/php/viii.0/php.ini        

At this betoken, I strongly recommend closing ALL your terminal tabs and windows. This will mean opening a new terminal to continue with the side by side step. This is strongly recommended because some really strange path issues can arise with existing terminals (trust me, I have seen information technology!).

We have installed only not linked these PHP versions. To switch to PHP 7.3 for example we can type:

          brew unlink php && mash link --overwrite --strength [email protected]        

Quick test that we're in the correct version:

          php -v        
          PHP 7.3.32 (cli) (built: Oct 29 2022 00:43:12) ( NTS ) Copyright (c) 1997-2018 The PHP Group Zend Engine v3.3.32, Copyright (c) 1998-2018 Zend Technologies     with Zend OPcache v7.3.32, Copyright (c) 1999-2018, by Zend Technologies        

and to switch to to vii.4:

          mash unlink php && brew link --overwrite --forcefulness [email protected]        

And bank check that it'southward changed correctly:

          php -v        
          PHP 7.4.25 (cli) (built: October 21 2022 00:29:22) ( NTS ) Copyright (c) The PHP Group Zend Engine v3.iv.0, Copyright (c) Zend Technologies     with Zend OPcache v7.iv.25, Copyright (c), by Zend Technologies        

Apache PHP Setup - Role one

You have successfully installed your PHP versions, but we need to tell Apache to use them. You will once more demand to edit the /opt/homebrew/etc/httpd/httpd.conf file scroll to the bottom of the LoadModule entries.

If you accept been following this guide correctly, the last entry should be your mod_rewrite module:

          LoadModule rewrite_module lib/httpd/modules/mod_rewrite.so        

Below this add the following libphp modules:

          #LoadModule php7_module /opt/homebrew/opt/[email protected]/lib/httpd/modules/libphp7.so #LoadModule php7_module /opt/homebrew/opt/[email protected]/lib/httpd/modules/libphp7.so #LoadModule php7_module /opt/homebrew/opt/[email protected]/lib/httpd/modules/libphp7.so #LoadModule php7_module /opt/homebrew/opt/[e-mail protected]/lib/httpd/modules/libphp7.so LoadModule php7_module /opt/homebrew/opt/[email protected]/lib/httpd/modules/libphp7.so #LoadModule php_module /opt/homebrew/opt/[email protected]/lib/httpd/modules/libphp.and then        

We can but have one module processing PHP at a time, so for now, and then we have left our [email protected] entry uncommented while all the others are commented out. This volition tell Apache to utilise PHP 7.4 to handle PHP requests. (We will add together the ability to switch PHP versions later).

Also you lot must fix the Directory Indexes for PHP explicitly, so search for this block:

          <IfModule dir_module>     DirectoryIndex alphabetize.html </IfModule>        

and supervene upon information technology with this:

          <IfModule dir_module>     DirectoryIndex index.php index.html </IfModule>  <FilesMatch \.php$>     SetHandler awarding/ten-httpd-php </FilesMatch>        

Save the file and stop Apache then showtime over again, now that nosotros have installed PHP:

          brew services cease httpd brew services start httpd        

Validating PHP Installation

The all-time manner to test if PHP is installed and running as expected is to brand use of phpinfo(). This is not something you want to get out on a production machine, but information technology's invaluable in a development environment.

Simply create a file called info.php in your Sites/ binder you lot created before with this i-liner.

          echo "<?php phpinfo();" > ~/Sites/info.php        

Point your browser to http://localhost/info.php and you should run into a shiny PHP data page:

If you see a like phpinfo result, congratulations! You now take Apache and PHP running successfully. Y'all can exam the other PHP versions by commenting the LoadModule ... [electronic mail protected] ... entry and uncommenting one of the other ones. Then only restart apache and reload the same page.

PHP Switcher Script

We hard-coded Apache to use PHP 7.iv, but nosotros actually want to be able to switch betwixt versions. Luckily, some industrious individuals take already done the hard work for u.s. and written a very handy piddling PHP switcher script.

This sphp script has been updated to support PHP 8.0. If you desire to use that version, please repeat the process below to get the latest version.

Nosotros will install the sphp script into brew's standard /opt/homebrew/bin:

          curlicue -L https://gist.githubusercontent.com/rhukster/f4c04f1bf59e0b74e335ee5d186a98e2/raw/adc8c149876bff14a33e3ac351588fdbe8172c07/sphp.sh > /opt/homebrew/bin/sphp chmod +x /opt/homebrew/bin/sphp        

Testing the PHP Switching

After y'all have completed these steps, yous should be able to switch your PHP version by using the control sphp followed by a two digit value for the PHP version:

          sphp 8.0        

Y'all will probably accept to enter your ambassador password, and it should requite yous some feedback:

          Switching to [email protected] Switching your trounce Unlinking /opt/homebrew/Cellar/[electronic mail protected]/7.iii.32... 25 symlinks removed. Unlinking /opt/homebrew/Cellar/[email protected]/vii.4.25... 0 symlinks removed. Unlinking /opt/homebrew/Cellar/php/eight.0.12... 0 symlinks removed. Linking /opt/homebrew/Cellar/php/8.0.12... 24 symlinks created. Switching your apache conf Restarting apache Stopping `httpd`... (might accept a while) ==> Successfully stopped `httpd` (label: homebrew.mxcl.httpd) ==> Successfully started `httpd` (label: homebrew.mxcl.httpd)  PHP 8.0.12 (cli) (built: Oct 21 2022 00:21:xix) ( NTS ) Copyright (c) The PHP Grouping Zend Engine v4.0.12, Copyright (c) Zend Technologies     with Zend OPcache v8.0.12, Copyright (c), past Zend Technologies  All washed!                  

Test to see if your Apache is now running PHP vii.iv by over again pointing your browser to http://localhost/info.php. With a trivial luck, you should see something like this:

Troubleshooting PHP Switching

If yous take upgraded from a previous version of this guide and take installed PHP8, you may encounter message like: Unknown version of PHP. PHP Switcher tin can only handle arguments of: 7.0,seven.one,7.2,7.3,7.iv,8.0,8.one, then you need to reinstall the sphp script which has been updated.

If you get a bulletin almost conflicting PHP versions, then y'all probably have a conflict of taps. You will need to uninstall your previous PHP versions, then remove the old tap, then add the new tap, and and then reinstall PHP versions using the syntax above. For instance:

          mash untap exolnet/deprecated brew tap shivammathur/php brew install shivammathur/php/[e-mail protected]        

Test Your Setup with Grav CMS!

What better way to test your new powerful setup than to quickly install and exam Grav. The best apartment-file CMS in the world 😆! Nosotros can do this with simply a couple of quick final commands:

          cd ~/Sites curl -OJL https://getgrav.org/download/core/grav-admin/latest        
                      % Total    % Received % Xferd  Average Speed   Fourth dimension    Time     Fourth dimension  Current                                  Dload  Upload   Total   Spent    Left  Speed   0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0 100   628  100   628    0     0   1008      0 --:--:-- --:--:-- --:--:--  2803 100 17.6M  100 17.6M    0     0  12.3M      0  0:00:01  0:00:01 --:--:-- 77.3M        

And so just excerpt the zip file:

          unzip grav-admin-v1.7.24.zip        

This will extract the zip into a ~/Sites/grav-admin folder. Then simply point your browser at this directory http://localhost/grav-admin:

Fill up in the relevant information to create a new administator user, and simply click Create User to get into the admin. No other steps needed.

All done!!!!

Updating PHP and other Mash Packages

Brew makes it super easy to update PHP and the other packages yous install. The first step is to update Mash and so that information technology gets a list of available updates:

          brew update        

This volition spit out a list of available updates, and any deleted formulas. To upgrade the packages simply type:

          brew upgrade        

Y'all will demand to switch to each of your installed PHP versions and run update once more to get updates for each PHP version and ensure you are running the version of PHP you intend.

Activating Specific/Latest PHP Versions

Due to the way our PHP linking is set up up, only one version of PHP is linked at a time, only the current active version of PHP will be updated to the latest version. You can see the electric current active version by typing:

          php -v        

And you can run into the specific versions of a PHP package by typing:

          brew info [email protected]        
          [electronic mail protected]: stable 7.4.25 (bottled) [keg-merely] Full general-purpose scripting language https://www.php.net/ /opt/homebrew/Cellar/[email protected]/7.iv.25 (498 files, 73MB)   Poured from canteen on 2022-ten-29 at 17:13:47 From: https://github.com/Homebrew/homebrew-core/blob/Head/Formula/[email protected] License: PHP-3.01 ==> Dependencies ...        

OK, that wraps upwards Function 1 of this 3 part serial You now have a fully functional Apache 2.four installation with a quick-and-easy way to toggle between PHP seven.0, 7.1, 7.2, 7.3, seven.4, and 8.0. Check out Part 2 to discover out how to setup your environment with MySQL, Virtual Hosts, APC caching, YAML, and Xdebug. Also take a gander at Part 3 to detect out how to setup SSL for your Apache Virtual Hosts.

Source: https://getgrav.org/blog/macos-monterey-apache-multiple-php-versions

Posted by: wernerpras1965.blogspot.com

0 Response to "How To Install Apache Web Server On Mac"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel