At Tower, we use Apache to host websites like our learning platform, our weblog, and our independent intersection locate. While this doesn’t mean we have to use Apache to run the sites locally, using exchangeable push-down storage for growth and product is broadly a good idea. This means setting up a development environment with Apache. On macOS, which is what we use for the most separate, there are quite a few options. We could set up Apache in a virtual machine, possibly controlled using Vagrant. We could use Docker to run Apache in a container. There are besides solutions with graphic drug user interfaces like MAMP. however, a commodious and dim-witted solution is to just set up Apache running natively in macOS — no wrappers, no virtual machines, no containerization. In this post, we’ll go through how to set up Apache and PHP, using versions installed using the Homebrew software director for macOS. macOS comes with built-in versions of Apache and PHP, and we could easily use those. however, there are a few drawbacks to this approach. We don’t have control over the claim versions used, and the version available might not be improving to date. I’ve had problems where OS updates have overwritten my configuration for the built-in Apache server. ultimately, running php -v to check the interpretation of the built-in PHP gives a warning message stating that PHP will be removed from future versions of macOS — in fact, in the approaching macOS Monterey, PHP seems to be gone. alternatively, of using the built-in versions, we’ll install Apache and PHP using Homebrew. We’ll look at how to set up a local development environment using these and, as common, we’ll try on to cover the “ why ” American Samoa well as the “ what ”; in addition to merely presenting the configuration, we’ll go over the aim of each directing and command.

here are the steps we’ll take:


Reading: How To Set Up Apache and PHP from Homebrew on macOS

1. Installation

Instructions for how to install Homebrew itself can be found on the official Homebrew website. Assuming Homebrew is installed, all you need to do in order to install Apache and PHP is to run the succeed command: brew install httpd php ( httpd refers to the Apache webserver ).

A word regarding paths: on a Mac with Apple Silicon, Homebrew will use /opt/homebrew as its prefix. The prefix is a screen of a foundation directory; a directory under which Homebrew will put diverse files belonging to the packages it installs. binary files will go in /opt/homebrew/bin, shapefiles in /opt/homebrew/etc/, and then on. On an Intel-based Mac, this base directory will probably be /usr/local alternatively. In this article, I’ll refer to paths using a Homebrew prefix of /opt/homebrew. If your prefix is different, please change the paths accordingly. To find out which base directory Homebrew is using on your machine, run brew –prefix.

2. Development URLs

Before we get started on Apache and PHP configuration, let’s touch on the subject of development URLs concisely. I think a dainty setup for local development is to use a specific TLD like.test, accessing my respective projects through URLs like my-first-project.test, my-second-project.test, and therefore on. In this guide, we’ll simply use the /etc/hosts charge to point our “ fake ” domains at our local webserver. If there’s interest, a future post might cover how to set up a servicing like dnsmasq to mechanically grab all requests to hosts ending with.test and send them to our local development server.

.test is a reserved top-level domain and therefore, using this, you shouldn’t run into problems such as the people for development environments did when Google acquired TLD. The /etc/hosts file provides a commodious way to associate hostnames with IP addresses. normally, when we visit a URL like, the actual IP address of the waiter has to be determined through something called the Domain Name System, or DNS for abruptly. The /etc/hosts file gives us an easy way to override this. For the purposes of this article, let’s say we’re working with a project called my-project, which we want to be served at the URL my-project.test. We’ll add this line at the buttocks of /etc/hosts: my-project.test

After saving the file, visiting my-project.test in a browser will result in the request being sent to the IP cover for our own car.

3. Apache Configuration

future, let’s catch up to work on the actual Apache shape. In my case, the main Apache shapefile is located at /opt/homebrew/etc/httpd/httpd.conf ( again, on an Intel-based Mac, this is likely to be /usr/local/etc/httpd/httpd.conf ). In this file, there are a few changes to make:

Listen 8080

This line tells Apache to listen for traffic on port 8080. Accessing ports with numbers lower than 1024 command ancestor privileges and thus, listening on port 8080 lets users run Apache without being root. however, as HTTP traffic goes to port 80 by default, we want to listen on that interface rather:

Listen 80

Chances are, you want to run multiple websites on your calculator, with several hostnames in /etc/hosts. however, all of these will hit the like Apache server. To serve multiple sites from one Apache waiter, Apache can look at the hostname of the incoming request and authorize the request to one of the multiple virtual hosts. virtual host support has to be enabled by removing the # in front of the agate line below, turning it from a remark into an Apache directing that loads the virtual host shape from the file in doubt:

#Include /opt/homebrew/etc/httpd/extra/httpd-vhosts.conf

We’ll uncomment another channel in ordain to load the mod_rewrite module. This module is used to rewrite entrance URLs. For example, many web frameworks use it to enable “ pretty URLs ”, letting website visitors use URLs like /posts/2021/some-post-title/ while translating them into URLs like /index.php?p=697 for the back-end. This module will likely be useful, so let’s second enable it by removing the # below:

#LoadModule rewrite_module lib/httpd/modules/

Apache comes with a configuration for a nonpayment site, with a document route of /opt/homebrew/var/www. We won ’ deoxythymidine monophosphate be using this site, as we’ll manipulate virtual hosts alternatively. As it stands, everything would work, but Apache would emit a warning every time it started, stating that it can’t thymine determine the hostname to use for this nonpayment site. We’ll set the hostname on a per-virtual-host basis later. however, to silence the admonitory message, we’ll uncomment the default ServerName directive in httpd.conf ( or merely enter any hostname you want ):


Read more: How to host a website on IBM Cloud – GeeksforGeeks

4. PHP Configuration

We want PHP to be available on our server. For this, we’ll add another LoadModule directing after the other ones, loading a faculty provided by PHP as installed through Homebrew earlier ( on an Intel-based Mac, variety /opt/homebrew to /usr/local ):

LoadModule php_module /opt/homebrew/opt/php/lib/httpd/modules/

This loads the PHP module. There is some extra shape required. Let’s add far PHP configuration in a disjoined file in the extra directory, precisely like with the virtual host’s shape. We need to include it explicitly from the main configuration file, therefore find the section with Include directives and add this after the last one:

# PHP settings

Include /opt/homebrew/etc/httpd/extra/httpd-php.conf

The rest of our PHP configuration goes in the file /opt/homebrew/etc/httpd/extra/httpd-php.conf:


    SetHandler application/x-httpd-php



    DirectoryIndex index.html index.php


We start by checking if the PHP module is available, which might seem pleonastic as we good added it in the httpd.conf file. however, these files may get edited independently of each other, so get’s follow the convention of the other files in the extra directory and check that the modules we use are available. We’ll expect files ending in.php, and set their handler to application/x-httpd-php — an animal trainer provided by the PHP module. A coach in Apache represents a natural process to take for a file. While most files are just served using a built-in coach, the PHP files have to be interpreted by PHP before being served.

Some guides use the AddType or AddHandler directives hera, which take parameters for the file extensions they apply to. These can introduce security issues as they check their configured extensions against every annex of a file. For example, a web application may allow users to upload.jpeg files. however, if a drug user uploads a file with a name ending in.php.jpeg, this file might be executed as PHP if the above directives are used. consequently, we use the FilesMatch directive along with SetHandler. not that we’re probably to encounter these issues when we’re running Apache locally for ourselves, but we might as well set things up properly. The DirectoryIndex directing makes sure that if a URL for a directory is requested, and the directory contains an index.php file ( or an index.html file ), that file will be served.

5. Virtual Host Setup

We ’ ve now set up Apache to support PHP and virtual hosts. We still need to add shape for each virtual host individually. Earlier, we uncommented a line in order to include the /opt/homebrew/etc/httpd/extra/httpd-vhosts.conf file ( /usr/local/etc/httpd/extra/httpd-vhosts.conf on an Intel-based Mac ). now, let’s edit it to actually configure a virtual host.

    ServerName my-project.test

    DocumentRoot /path/to/my-project


        Require all granted

        AllowOverride All


This configures a virtual horde on port 80. ServerName sets the hostname for the virtual site. When we visit the my-project.test in the browser, the change we made to /etc/hosts will make certain the request is sent to the local anesthetic Apache server. Apache will find the virtual master of ceremonies with the matching hostname and serve that location. The DocumentRoot directing specifies the location on the file system where the files to be served exist. Make sure to change this way, along with the early example of it in the Directory directive, to the actual path of your website on your machine!

The Directory directive has to do with permissions. In the httpd.conf file, there’s a part that denies entree to any resource by default option. Access to anything that should be public has to be specifically allowed. so, for the files in our site root, we give everyone access to all resources through the Require directive. The AllowOverride directing controls which directives can be overridden in a.htaccess file. A.htaccess file can be used for per-directory configuration — many CMSes consume this file together with the mod_rewrite module mentioned earlier to set up their URLs, for exercise. here, we allow all directives to be overridden.

While we’re at it, we can go ahead and remove or comment out the existing dummy virtual hosts existing in this file, otherwise, Apache will emit some warnings on the inauguration, as the document roots for these sites likely don’t exist.

6. Up and Running

That’s all the configuration done! All that remains is to start the waiter. If you want Apache to start automatically with your computer, you can use the Homebrews services command to start the server and enable it for autostart at the same time:

sudo brew services start httpd

sudo brew services stop httpd will then both turn off the waiter and disable autostart for it.

Read more: How to Launch an Instance and Host a Website With AWS EC2 – DZone Cloud

personally, I tend to fair use Apache’s built-in “ server control interface ” to start the server as I need it: sudo apachectl start will start the server and sudo apachectl stop will stop it. Hopefully, after starting the server, visiting your chosen hostname in your browser will result in your site being served correctly!

That’s about all for today! I hope you found this template useful. Of course, we’ve only covered Apache and PHP — there are many additions you may want to make to this push-down list, such as the MySQL database server. If you’re interested in a future article covering this or any other view of an Apache-based host stack, please let us know!


Category: Website hosting

Leave a Reply

Your email address will not be published.