Ir para o conteúdo


Banner

Instalando o Apache no MacOS via Brew

Catalina Required Libraries

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

brew install openldap libiconv

Apache Installation

The latest macOS 10.15 Catalina comes with Apache 2.4 pre-installed, however, it is no longer a simple task to use this version with Homebrew because Apple has removed some required scripts in this release. However, the solution is to install Apache 2.4 via Homebrew and then configure it to run on the standard ports (80/443).

If you already have the built-in Apache running, it will need to be shutdown first, and any auto-loading scripts removed. It really doesn't hurt to just run all these commands in order - even if it's a fresh installation:

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

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

Instalando o HTTPD

brew install httpd

Now we just need to configure things so that our new Apache server is auto-started

Iniciando o HTTPD

sudo brew services start httpd

Info

Updating Homebrew...
==> Auto-updated Homebrew!
Updated 3 taps (caskroom/cask, homebrew/cask and homebrew/core).
==> Updated Formulae
fdupes                     kcptun                     ngt                        re2                        sqlmap
==> Updated Casks
lehreroffice                     polymail                         sipgate-softphone                vimr
lehreroffice                     polymail                         sipgate-softphone                vimr

==> Tapping homebrew/services
Cloning into '/usr/local/Homebrew/Library/Taps/homebrew/homebrew-services'...
remote: Enumerating objects: 44, done.
remote: Counting objects: 100% (44/44), done.
remote: Compressing objects: 100% (40/40), done.
remote: Total 821 (delta 16), reused 16 (delta 3), pack-reused 777
Receiving objects: 100% (821/821), 229.77 KiB | 794.00 KiB/s, done.
Resolving deltas: 100% (334/334), done.
Tapped 1 command (39 files, 307.6KB).
Warning: Taking root:admin ownership of some httpd paths:
/usr/local/Cellar/httpd/2.4.43/bin
/usr/local/Cellar/httpd/2.4.43/bin/httpd
/usr/local/opt/httpd
/usr/local/opt/httpd/bin
/usr/local/var/homebrew/linked/httpd
This will require manual removal of these paths using `sudo rm` on
brew upgrade/reinstall/uninstall.
Warning: httpd must be run as non-root to start at user login!
==> Successfully started `httpd` (label: homebrew.mxcl.httpd)

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

Troubleshooting Tips

If you get a message that the browser can't connect to the server, first check to ensure the server is up.

ps -aef | grep httpd
    0 31374     1   0 12:35   ??         0:00.06 /usr/local/opt/httpd/bin/httpd -D FOREGROUND
   70 31377 31374   0 12:35   ??         0:00.00 /usr/local/opt/httpd/bin/httpd -D FOREGROUND
   70 31378 31374   0 12:35   ??         0:00.00 /usr/local/opt/httpd/bin/httpd -D FOREGROUND
   70 31379 31374   0 12:35   ??         0:00.00 /usr/local/opt/httpd/bin/httpd -D FOREGROUND
   70 31380 31374   0 12:35   ??         0:00.01 /usr/local/opt/httpd/bin/httpd -D FOREGROUND
   70 31381 31374   0 12:35   ??         0:00.00 /usr/local/opt/httpd/bin/httpd -D FOREGROUND
   70 31483 31374   0 12:42   ??         0:00.00 /usr/local/opt/httpd/bin/httpd -D FOREGROUND
  501 31487  3143   0 12:43   ttys002    0:00.00 grep httpd

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

Try to restart Apache with:

sudo apachectl -k restart

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

tail -f /usr/local/var/log/httpd/error_log

Apache is controlled via the apachectl command so some useful commands to use are:

Atenção
$ sudo apachectl start
$ sudo apachectl stop
$ sudo apachectl -k restart
Atenção

The -k will force a restart immediately rather than asking politely to restart when apache is good and ready

Apache Configuration

Now that we have a working web server, we will want to do is make some configuration changes so it works better as a local development server.

In the latest version of Brew, you have to manually set the listen port from the default of 8080 to 80, so we will need to edit Apache's configuration file: /usr/local/etc/httpd/httpd.conf

If you followed the instructions above you should be able to use Visual Studio Code to edit your files using the code Terminal command. However, if you want to use the default TextEditor application to perform edits, you can use the open -e command followed by the path to the file.

code /usr/local/etc/httpd/httpd.conf
ou 
open -e /usr/local/etc/httpd/httpd.conf
ou 
nano /usr/local/etc/httpd/httpd.conf

Find the line that says

Listen 8080

and change it to 80:

Listen 80

Next we'll configure it to use the to change the document root for Apache. This is the folder where Apache looks to serve file from. By default, the document root is configured as /usr/local/var/www. As this is a development machine, let's assume we want to change the document root to point to a folder in our own home directory.

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

DocumentRoot "/usr/local/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"
DocumentRoot "/Users/luisrodrigoog/Documents/Sites"

You also need to change the tag reference right below the DocumentRoot line. This should also be changed to point to your new document root also:

<Directory "/usr/local/var/www">

<Directory "/Users/your_user/Sites">
<Directory "/Users/luisrodrigoog/Documents/Sites">
We removed the optional quotes around the directory paths as TextEdit will probably try to convert those to smart-quotes and that will result in a Syntax error when you try to restart Apache. Even if you edit around the quotes and leave them where they are, saving the document may result in their conversion and cause an error.

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

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

Also we should now enable mod_rewrite which is commented out by default. Search for mod_rewrite.so and uncomment the line by removing the leading #:

##
# Descomente a linha abaixo
## 
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. One problem still exists, however. By default, apache runs as the user daemon and group daemon. This will cause permission problems when trying to access files in our home directory. About a third of the way down the httpd.conf file there are two settings to set the User and Group Apache will run under. Change these to match your user account (replace your_user with your real username), with a group of staff:

# User _www
# Group _www
User luisrodrigoog
Group staff

Servername

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

#ServerName www.example.com:8080
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 simple index.html and put some dummy content in it like:

My User Web Root

.

mkdir -p ~/Documents/Sites
echo "<h1>Ola, mundo</h1>" > ~/Documents/Sites/index.html

Restart apache to ensure your configuration changes have taken effect:

sudo apachectl -k restart

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

Pointing your browser to http://localhost should display your new message. If you have that working, we can move on!

PHP

brew install php

Apache PHP Setup

ls -l /usr/local/opt/php/lib/httpd/modules/libphp7.so

You have successfully installed your PHP versions, but we need to tell Apache to use them. You will again need to edit the /usr/local/etc/httpd/httpd.conf. Modify the paths as follows, comment out all but one entry:

sudo nano /usr/local/etc/httpd/httpd.conf

Localize a seção Dynamic Shared Object (DSO) Support e adicione as seguintes linhas

## Carregando o PHP
LoadModule php7_module /usr/local/opt/php/lib/httpd/modules/libphp7.so

## Outros Modulos
LoadModule rewrite_module lib/httpd/modules/mod_rewrite.so
LoadModule socache_shmcb_module lib/httpd/modules/mod_socache_shmcb.so
LoadModule ssl_module lib/httpd/modules/mod_ssl.so
LoadModule userdir_module lib/httpd/modules/mod_userdir.so
LoadModule vhost_alias_module lib/httpd/modules/mod_vhost_alias.so

Localize a seção Supplemental configuration e adicione as seguintes linhas

## Includes
Include /usr/local/etc/httpd/extra/httpd-userdir.conf
Include /usr/local/etc/httpd/extra/httpd-vhosts.conf
Include /usr/local/etc/httpd/extra/httpd-ssl.conf

Localize a seção DirectoryIndex e ajuste para:

<IfModule dir_module>
    #DirectoryIndex index.html
    DirectoryIndex index.html index.php
</IfModule>
Logo abaixo adicione as seguintes linhas:

<FilesMatch \.php$>
    SetHandler application/x-httpd-php
</FilesMatch>

Reiniciando o Apache

sudo /usr/local/bin/apachectl -k restart

SSL/Virtual Hosts

Change default 8443 ports to 443 in the SSL configuration file.

nano /usr/local/etc/httpd/extra/httpd-ssl.conf
Localize a linha 'Listen 8443' e substitua por 'Listen 443'.

#Listen 8443
Listen 443

Localize e a linha abaixo

<VirtualHost _default_:8443>

E ajuste para:

#<VirtualHost _default_:8443>
<VirtualHost _default_:443>

Localize ==ServerName www.example.com:8443== e ajuste para:

#ServerName www.example.com:8443
ServerName www.example.com:443

Save the file, open up /usr/local/etc/httpd/extra/httpd-vhosts.conf and add your own SSL based virtual hosts.

nano /usr/local/etc/httpd/extra/httpd-vhosts.conf

Create your virtual host entries that you want to use SSL with.

<VirtualHost *:443>
    DocumentRoot "/Users/luisrodrigoog/Documents/Sites"
    ServerName yourdomain.com
    SSLEngine on
    SSLCertificateFile "/usr/local/etc/httpd/server.crt"
    SSLCertificateKeyFile "/usr/local/etc/httpd/server.key"
</VirtualHost>

Certificates

Generate a key and certificate.

cd /usr/local/etc/httpd
openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout server.key -out server.crt

Reiniciando o Apache

sudo /usr/local/bin/apachectl -k restart
ps -aef | grep httpd
    0 31374     1   0 12:35   ??         0:00.97 /usr/local/opt/httpd/bin/httpd -D FOREGROUND
  501 41122 31374   0  7:59   ??         0:00.00 /usr/local/opt/httpd/bin/httpd -D FOREGROUND
  501 41123 31374   0  7:59   ??         0:00.00 /usr/local/opt/httpd/bin/httpd -D FOREGROUND
  501 41124 31374   0  7:59   ??         0:00.00 /usr/local/opt/httpd/bin/httpd -D FOREGROUND
  501 41125 31374   0  7:59   ??         0:00.00 /usr/local/opt/httpd/bin/httpd -D FOREGROUND
  501 41126 31374   0  7:59   ??         0:00.00 /usr/local/opt/httpd/bin/httpd -D FOREGROUND
  501 41161 34304   0  8:01   ttys000    0:00.01 grep httpd
lsof -i | grep LISTEN
rapportd    501 luisrodrigoog    3u  IPv4 0x50192de57c430245      0t0  TCP *:49190 (LISTEN)
rapportd    501 luisrodrigoog    4u  IPv6 0x50192de57ce50e45      0t0  TCP *:49190 (LISTEN)
Python     1306 luisrodrigoog    5u  IPv4 0x50192de5831dd245      0t0  TCP localhost:irdmi (LISTEN)
httpd     41122 luisrodrigoog    4u  IPv6 0x50192de57ce4fd05      0t0  TCP *:https (LISTEN)
httpd     41122 luisrodrigoog    6u  IPv6 0x50192de57ce519c5      0t0  TCP *:http (LISTEN)
httpd     41123 luisrodrigoog    4u  IPv6 0x50192de57ce4fd05      0t0  TCP *:https (LISTEN)
httpd     41123 luisrodrigoog    6u  IPv6 0x50192de57ce519c5      0t0  TCP *:http (LISTEN)
httpd     41124 luisrodrigoog    4u  IPv6 0x50192de57ce4fd05      0t0  TCP *:https (LISTEN)
httpd     41124 luisrodrigoog    6u  IPv6 0x50192de57ce519c5      0t0  TCP *:http (LISTEN)
httpd     41125 luisrodrigoog    4u  IPv6 0x50192de57ce4fd05      0t0  TCP *:https (LISTEN)
httpd     41125 luisrodrigoog    6u  IPv6 0x50192de57ce519c5      0t0  TCP *:http (LISTEN)
httpd     41126 luisrodrigoog    4u  IPv6 0x50192de57ce4fd05      0t0  TCP *:https (LISTEN)
httpd     41126 luisrodrigoog    6u  IPv6 0x50192de57ce519c5      0t0  TCP *:http (LISTEN)
netstat  -a | grep LISTEN
tcp46      0      0  *.https                *.*                    LISTEN
tcp46      0      0  *.http                 *.*                    LISTEN
tcp4       0      0  localhost.irdmi        *.*                    LISTEN
tcp6       0      0  *.49190                *.*                    LISTEN
tcp4       0      0  *.49190                *.*                    LISTEN
tcp4       0      0  localhost.ipp          *.*                    LISTEN
tcp6       0      0  localhost.ipp          *.*                    LISTEN

Validando a instalação do PHP

The best way to test if PHP is installed and running as expected is to make use of phpinfo(). This is not something you want to leave on a production machine, but it's invaluable in a development environment.

Simply create a file called info.php in your Sites/ folder you created earlier with this one-liner.

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

Copy

Point your browser to http://localhost/info.php and you should see a shiny PHP information page:

Referencias


Última atualização: 24 de setembro de 2020

Comentários