# Apache



### Installer Apache (homebrew)

#### Désactiver le serveur Apache d'origine:

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

#### Installation:

```bash
$ brew install httpd
$ sudo brew services start httpd
```

Voir si Apache tourne:

```bash
$ ps -aef | grep httpd
  501 28356     1   0 10:56   ??         0:00.29 /opt/homebrew/opt/httpd/bin/httpd -D FOREGROUND
  501 12984   781   0  9:09   ttys001    0:00.01 tail -f /opt/homebrew/var/log/httpd/error_log
  501 29101   786   0  1:19   ttys002    0:00.00 grep httpd
```

Faire les m-à-j directement depuis Homebrew.



### Arrêter / redémarrer Apache

Apache est controllé par les *services brew*:

```bash
$ brew services stop httpd
$ brew services start httpd
$ brew services restart httpd
```

ou par *apachectl*:

```bash
$ sudo apachectl start
$ sudo apachectl stop
$ sudo apachectl -k restart
$ sudo apachectl -k restart -e Debug -E /dev/stdout
```



### Log:

```bash
# Mac intel
$ tail -f /usr/local/var/log/httpd/error_log
$ tail -f /usr/local/var/log/httpd/access_log

# Mac M1
$ tail -f /opt/homebrew/var/log/httpd/error_log
$ tail -f /opt/homebrew/var/log/httpd/access_log
```

```bash
$ multitail -s 2 /usr/local/var/log/httpd/error_log /usr/local/var/log/httpd/access_log

$ multitail -s 2 /opt/homebrew/var/log/httpd/error_log /opt/homebrew/var/log/httpd/access_log
```

```bash
$ tail -f /opt/homebrew/var/log/httpd/error_log -f /opt/homebrew/var/log/httpd/access_log

==> /opt/homebrew/var/log/httpd/error_log <==
[Fri Jul 30 06:58:08.250315 2021] [mpm_prefork:notice] [pid 59484] AH00169: caught SIGTERM, shutting down
PHP Warning:  PHP Startup: Unable to load dynamic library 'mcrypt.so' (tried: /opt/homebrew/lib/php/pecl/20200930/mcrypt.so (dlopen(/opt/homebrew/lib/php/pecl/20200930/mcrypt.so, 9): image not found), /opt/homebrew/lib/php/pecl/20200930/mcrypt.so.so (dlopen(/opt/homebrew/lib/php/pecl/20200930/mcrypt.so.so, 9): image not found)) in Unknown on line 0

==> /opt/homebrew/var/log/httpd/access_log <==
::1 - - [29/Jul/2021:17:39:55 +0200] "GET /info.php HTTP/1.1" 200 120227
::1 - - [29/Jul/2021:18:04:09 +0200] "GET /info.php HTTP/1.1" 200 120660

```



### Version:

```bash
$ httpd -v
Server version: Apache/2.4.48 (Unix)
Server built:   Jul  6 2021 20:11:03
```



### Modules activés:

```bash
$ httpd -M
Loaded Modules:
 core_module (static)
 so_module (static)
 http_module (static)
 mpm_prefork_module (shared)
 authn_file_module (shared)

$ apachectl -M
```



### Configuration:

Ouvrir le fichier *httpd.conf*:

```bash
# Mac intel
$ nano /usr/local/etc/httpd/httpd.conf

# Mac M1
$ nano /opt/homebrew/etc/httpd/httpd.conf
```

Remplacer les lignes et blocs:

| Original                                        | Modifié                                                      |
| ----------------------------------------------- | ------------------------------------------------------------ |
| `Listen 8080`                                   | `Listen 80`                                                  |
| `DocumentRoot "/usr/local/var/www"`             | `DocumentRoot "/Users/bruno/Sites"`                          |
| `<Directory "/usr/local/var/www">`              | `<Directory "/Users/bruno/Sites">`<br /><br />*Dans le même bloc Directory:*<br/>`AllowOverride All` |
| #User & Group:<br/>`User http`<br/>`Group http` | #User & Group:<br/>`User bruno`<br/>`Group staff`            |
| `#ServerName www.example.com:8080`<br/>         | `ServerName airbook.local`<br />#ServerName localhost        |

Activer certains modules:

```http
LoadModule rewrite_module lib/httpd/modules/mod_rewrite.so
```

Créer une page d'accueil:

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



#### Tester la configuration:

```bash
$ httpd -t
Syntax OK

$ apachectl configtest
```



#### Redémarrer Apache:

```bash
$ brew services restart httpd
```

Créer un alias dans .zshrc

```bash
alias bra='brew services restart httpd'
```



### [=> Installer PHP](php.md)



### Virtual Hosts:

Les activer dans *httpd.conf* (décocher les lignes):

```http
LoadModule vhost_alias_module lib/httpd/modules/mod_vhost_alias.so
```
Mac M1
```http
# Virtual hosts
Include /opt/homebrew/etc/httpd/extra/httpd-vhosts.conf
```

Mac intel

```http
# Virtual hosts
Include /usr/local/etc/httpd/extra/httpd-vhosts.conf
```



#### Redirection:

Editer le fichier *hosts*:

```bash
$ sudo nano /etc/hosts

127.0.0.1       airbook.local
127.0.0.1       wordpress.airbook.local
127.0.0.1       dev.airbook.local
127.0.0.1       zenphoto.airbook.local
127.0.0.1       phpmyadmin.airbook.local
```

Editer le fichier *httpd-vhosts.conf*:

```bash
# Mac intel
$ nano /usr/local/etc/httpd/extra/httpd-vhosts.conf

# Mac M1
$ nano /opt/homebrew/etc/httpd/extra/httpd-vhosts.conf
```

```http
<VirtualHost *:80>
    DocumentRoot /Users/bruno/Sites/wordpress
    ServerName wordpress.air.local
    CustomLog /usr/local/var/log/httpd/wordpress-access.log combined
    ErrorLog /usr/local/var/log/httpd/wordpress-error.log
</VirtualHost>
```



#### Redirection de port:

wiki.js tourne sur localhost:3000 

Pour le rendre disponible sur wiki.silverbook.local:

##### Editer le fichier *httpd.conf* et activer les modules `mod_proxy` et `mod_proxy_http`:

```http
LoadModule proxy_module lib/httpd/modules/mod_proxy.so
LoadModule proxy_http_module lib/httpd/modules/mod_proxy_http.so
```

##### Editer le fichier *hosts*:

```bash
$ sudo nano /etc/hosts
127.0.0.1       airbook.local
127.0.0.1       wiki.airbook.local
```

##### Editer le fichier *httpd-vhosts.conf*:

```http
<VirtualHost *:80>
	ServerName wiki.airbook.local
	ProxyPass / http://localhost:3000/
	ProxyPassReverse / http://localhost:3000/
	ProxyPreserveHost On
</VirtualHost>
```



### SSL:

##### Activer SSL dans *httpd.conf* (décocher les lignes):

```http
LoadModule socache_shmcb_module lib/httpd/modules/mod_socache_shmcb.so
...
LoadModule ssl_module lib/httpd/modules/mod_ssl.so
```

Mac M1

```http
# Secure (SSL/TLS) connections
Include /opt/homebrew/etc/httpd/extra/httpd-ssl.conf
```

Mac intel

```http
# Secure (SSL/TLS) connections
Include /usr/local/etc/httpd/extra/httpd-ssl.conf
```



##### Ouvrir le fichier *httpd-ssl.conf*:

```bash
$ bbedit /opt/homebrew/etc/httpd/extra/httpd-ssl.conf
```

```bash
# Mac intel
$ nano /usr/local/etc/httpd/extra/httpd-ssl.conf

# Mac M1
$ nano /opt/homebrew/etc/httpd/extra/httpd-ssl.conf
```

##### Remplacer les lignes:

| Original                               | Modifié                             |
| -------------------------------------- | ----------------------------------- |
| `Listen 8443`                          | `Listen 443`                        |

et le bloc:

```http
<VirtualHost _default_:8443>

#   General setup for the virtual host
DocumentRoot "/opt/homebrew/var/www"
ServerName www.example.com:8443
ServerAdmin you@example.com
ErrorLog "/opt/homebrew/var/log/httpd/error_log"
TransferLog "/opt/homebrew/var/log/httpd/access_log"
```

par

```http
<VirtualHost _default_:443>

#   General setup for the virtual host
DocumentRoot "/Users/bruno/Sites"
ServerName airbook.local:443
ServerAdmin bruno@clicclac.info
ErrorLog "/opt/homebrew/var/log/httpd/airbook.local-error_log"
TransferLog "/opt/homebrew/var/log/httpd/airbook.local-access_log"
```



##### Ouvrir le fichier *httpd-vhosts.conf*:

Rajouter ce bloc pour chaque Virtual Host.

```http
<VirtualHost *:443>
   DocumentRoot "/Users/bruno/Sites"
   ServerName airbook.local
   SSLEngine on
   SSLCertificateFile "/usr/local/etc/httpd/server.crt"
   SSLCertificateKeyFile "/usr/local/etc/httpd/server.key"
   ErrorLog "/opt/homebrew/var/log/httpd/airbook.local-error_log"
   CustomLog "/opt/homebrew/var/log/httpd/airbook.local-access_log" common
</VirtualHost>
```



### Générer un certificat auto-signé:

#### Méthode 1:

Générer la <u>clé</u> et le <u>certificat</u> (*Common Name* doit correspondre à *ServerName* du *https-vhosts.conf*)

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

Vérifier la configuration Apache et relancer le serveur:

```bash
$ sudo apachectl configtest
$ sudo apachectl -k restart
```

#### Méthode 2:

Installer 2 programmes:

- mkcert: Simple tool to make locally trusted development certificates
- nss: Libraries for security-enabled client and server applications (for Firefox)

```bash
$ brew install mkcert nss
```

##### Installer le serveur de certificat:

```bash
$ mkcert -install
Created a new local CA 💥
Sudo password:
The local CA is now installed in the system trust store! ⚡️
The local CA is now installed in the Firefox trust store (requires browser restart)! 🦊

# 
# Un certificat "mkcert bruno@AirBook (Bruno Pesenti)" est ajouté dans le Trousseau d'accès (Système/Certficats)
```

!!! info "Si Firefox est configuré avec un mot-de-passe"

```bash
$ mkcert -install
The local CA is now installed in the system trust store! ⚡️
Enter Password or Pin for "NSS Certificate DB":

# Entrer le mot-de-passe de Firefox !
# https://github.com/FiloSottile/mkcert/issues/50
```

##### Créer les dossiers pour les certificats:

```bash
$ cd /opt/homebrew/etc/httpd
$ mkdir certs && cd certs
```

##### Générer les certificats (wildcard):

```bash
$ mkcert airbook.local "*.airbook.local" localhost 127.0.0.1 ::1
$ mkcert airbook.local "*.airbook.local" 192.168.2.39 localhost 127.0.0.1 ::1

Created a new certificate valid for the following names 📜
 - "airbook.local"
 - "*.airbook.local"
 - "localhost"
 - "127.0.0.1"
 - "::1"

Reminder: X.509 wildcards only go one level deep, so this won't match a.b.airbook.local ℹ️

The certificate is at "./airbook.local+4.pem" and the key at "./airbook.local+4-key.pem" ✅

It will expire on 18 April 2026 🗓
```

Un certificat et une clé sont créer pour chaque domaine:

```http
/opt/homebrew/etc/httpd/certs/airbook.local+4.pem
/opt/homebrew/etc/httpd/certs/airbook.local+4-key.pem
```

##### Editer le fichier *httpd-ssl.conf* et remplacer les 2 lignes:

```http
SSLCertificateFile "/opt/homebrew/etc/httpd/certs/airbook.local+4.pem"
SSLCertificateKeyFile "/opt/homebrew/etc/httpd/certs/airbook.local+4-key.pem"
```

##### Tester la configuration:

```bash
$ httpd -t
Syntax OK

$ apachectl configtest
```

##### Redémarrer Apache:

```bash
$ brew services restart httpd
```



#### SSL dans Virtual Host

Editer le fichier *httpd-vhosts.conf*:

Rajouter le/les bloc(s) suivant

```http
<VirtualHost *:443>
  ServerAdmin bruno@clicclac.info
    DocumentRoot "/Users/bruno/Sites"
    ServerName airbook.local
    SSLEngine on
    SSLCertificateFile "/opt/homebrew/etc/httpd/certs/airbook.local+4.pem"
    SSLCertificateKeyFile "/opt/homebrew/etc/httpd/certs/airbook.local-key+4.pem"
    ErrorLog "/opt/homebrew/var/log/httpd/airbook.local-error_log"
    CustomLog "/opt/homebrew/var/log/httpd/airbook.local-access_log" common
</VirtualHost>
```

```http
<VirtualHost *:443>
  ServerAdmin bruno@clicclac.info
    DocumentRoot "/Users/bruno/Sites/mbv"
    ServerName maboiteverte.airbook.local
    SSLEngine on
    SSLCertificateFile "/opt/homebrew/etc/httpd/certs/airbook.local+4.pem"
    SSLCertificateKeyFile "/opt/homebrew/etc/httpd/certs/airbook.local+4-key.pem"
    ErrorLog "/opt/homebrew/var/log/httpd/mbv.airbook.local-error_log"
    CustomLog "/opt/homebrew/var/log/httpd/mbv.airbook.local-access_log" common
</VirtualHost>
```

après le bloc

```http
<VirtualHost *:80>
    ServerAdmin bruno@clicclac.info
    DocumentRoot "/Users/bruno/Sites"
    ServerName airbook.local
    #ServerAlias www.dummy-host.example.com
    #ErrorLog "/opt/homebrew/var/log/httpd/dummy-host.example.com-error_log"
    #CustomLog "/opt/homebrew/var/log/httpd/dummy-host.example.com-access_log" common
</VirtualHost>
```



### Warning: getimagesize(): SSL operation failed with code 1. 

Erreur dans Worpdress:

```
Warning: getimagesize(): SSL operation failed with code 1. 
OpenSSL Error messages: error:1416F086:SSL routines:tls_process_server_certificate:certificate verify failed in 
/Users/bruno/Sites/wordpress/wp-content/themes/CreatorThemeRes-child/functions.php on line 1611
```

Il faut rajouter les lignes suivantes au PHP.ini

```ini
[openssl]
openssl.cafile="/opt/homebrew/etc/httpd/certs/airbook.local.pem"

# D'après SSLCertificateFile "/opt/homebrew/etc/httpd/certs/airbook.local.pem" du httpd-ssl.conf

```

https://stackoverflow.com/questions/51532963/tcpdf-getimagesize-ssl-operation-failed-with-code-1-error1416f086ssl-routi

Mais ça donne une erreur  pour les MàJ PECL: `Connection to ssl://pecl.php.net:443' failed: `



### Messages d'erreur personnalisés:

Dans un fichier .htaccess, ajouter:

```html
ErrorDocument 404 /custom_404.html
ErrorDocument 500 /custom_50x.html
```

[:fa-link: https://httpd.apache.org/docs/2.4/fr/custom-error.html](https://httpd.apache.org/docs/2.4/fr/custom-error.html)



### Messages d'erreur personnalisés multilingues:

Ouvrir le fichier *httpd.conf* et dé-commenter les lignes suivantes:

```http
LoadModule include_module lib/httpd/modules/mod_include.so
LoadModule negotiation_module lib/httpd/modules/mod_negotiation.so

Include /opt/homebrew/etc/httpd/extra/httpd-multilang-errordoc.conf
```



### Installer phpmyadmin (Homebrew):

```bash
$ brew install homebrew/php/phpmyadmin
```

Le fichier de configuration se trouve là:`/opt/homebrew/etc/phpmyadmin.config.inc.php`

Ajouter le bloc qui suit dans le **httpd.conf**

```http
  Alias /phpmyadmin /opt/homebrew/share/phpmyadmin
  <Directory /opt/homebrew/share/phpmyadmin/>
    Options Indexes FollowSymLinks MultiViews
    AllowOverride All
    <IfModule mod_authz_core.c>
      Require all granted
    </IfModule>
    <IfModule !mod_authz_core.c>
      Order allow,deny
      Allow from all
    </IfModule>
  </Directory>
```







### Différence installation macOS - homebrew:

***MacOS:***

```bash
/usr/sbin/httpd-wrapper

/System/Library/LaunchDaemons/org.apache.httpd.plist

/Library/WebServer/Documents/index.html

/private/etc/apache2/httpd.conf

/private/etc/apache2/extra/httpd-vhosts.conf
```

***Homebrew:***

Mac intel:

```bash
/usr/local/opt/httpd/bin/httpd

/Users/bruno/Library/LaunchAgents/homebrew.mxcl.httpd.plist
/Users/bruno/Library/LaunchAgents/homebrew.mxcl.php.plist
/Users/bruno/Library/LaunchAgents/homebrew.mxcl.mariadb.plist

/usr/local/var/www/index.html -> /Users/bruno/Sites

/usr/local/etc/httpd/httpd.conf

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

Mac M1:

```bash
/opt/homebrew/opt/httpd/bin/httpd

/Users/bruno/Library/LaunchAgents/homebrew.mxcl.httpd.plist
/Users/bruno/Library/LaunchAgents/homebrew.mxcl.php.plist
/Users/bruno/Library/LaunchAgents/homebrew.mxcl.mariadb.plist

/opt/homebrew/var/www/index.html -> /Users/bruno/Sites

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

/opt/homebrew/etc/httpd/extra/httpd-vhosts.conf
```



### Arrêt brutal de macOS:

Il se peut qu'au redémarrage, Apache ne fonctionne plus.

Regarder *error_log*:

<!--Tue Jan 30 13:24:14.790214 2018] [core:warn][pid 2103] AH00098: pid file /usr/lo 218-->
<!--cal/var/run/httpd/httpd.pid overwritten -- Unclean shutdown of previous Apache run 127.0.0.1 - - [30/Jan/2018:09:57:30 +0100] "GET /favicon.ico HTTP/1.1" 404 209-->
<!--?-->

Puis arrêter et redémarrer Apache:

```bash
$ sudo apachectl stop
$ sudo apachectl start
```



### Nouvelle version de Python:

<u>Apache ne fonctionne plus</u>. Regarder *error_log*:

```http
Fatal Python error: Py_Initialize: unable to load the file system codec
ModuleNotFoundError: No module named 'encodings'
```

Dans *httpd.conf*  commencer par vérifier la version de Python dans la ligne suivante. Si elle est correcte, commenter la ligne:

`WSGIPythonHome "/usr/local/Cellar/python/3.6.4_4/Frameworks/Python.framework/Versions/3.6"`

Arrêter et redémarrer Apache.

Si cela refonctionne, désinstaller et réinstaller <u>mod_wsgi</u> avec <u>pip</u>.

```bash
$ pip3 list --format=columns
Package                  Version
------
...
mod-wsgi                 4.6.4
...

$ pip uninstall mod-wsgi
Uninstalling mod-wsgi-4.6.4:
  Would remove:
    /usr/local/bin/mod_wsgi-express
    /usr/local/lib/python3.6/site-packages/mod_wsgi-4.6.4.dist-info/*
    /usr/local/lib/python3.6/site-packages/mod_wsgi/*
Proceed (y/n)? y
  Successfully uninstalled mod-wsgi-4.6.4
  
$ pip install mod-wsgi
Collecting mod-wsgi
  Downloading https://files.pythonhosted.org/packages/9e/37/dd336068ece37c43957aa337f25c59a9a6afa98086e5507908a2d21ab807/mod_wsgi-4.6.4.tar.gz (2.6MB)
    100% |████████████████████████████████| 2.6MB 1.6MB/s
Building wheels for collected packages: mod-wsgi
  Running setup.py bdist_wheel for mod-wsgi ... done
  Stored in directory: /Users/bruno/Library/Caches/pip/wheels/2d/73/68/9dcbbd0147b3fde4686263a31324ea2372e42f7cefa2f7d181
Successfully built mod-wsgi
Installing collected packages: mod-wsgi
Successfully installed mod-wsgi-4.6.4

$ sudo apachectl stop
$ sudo apachectl start
```

Pour connaître la configuration dans *httpd.conf*, lancez `mod_wsgi-express module-config`

```bash
$ mod_wsgi-express module-config
LoadModule wsgi_module "/usr/local/lib/python3.8/site-packages/mod_wsgi/server/mod_wsgi-py38.cpython-38-darwin.so"
WSGIPythonHome "/usr/local/Cellar/python@3.8/3.8.3_2/Frameworks/Python.framework/Versions/3.8"
```



### Erreur _apr_bucket:

```bash
dyld[15170]: Symbol not found: _apr_bucket_file_set_buf_size
  Referenced from: /opt/homebrew/Cellar/httpd/2.4.52/bin/httpd
  Expected in: /usr/lib/libaprutil-1.0.dylib

```

Il faut réinstaller apr-util

https://stackoverflow.com/questions/69892715/installing-httpd-and-php-in-mac-os-12

```bash
$ brew reinstall apr-util

$ brew services restart httpd
```



### cgi

```http
LoadModule mpm_prefork_module lib/httpd/modules/mod_mpm_prefork.so

<IfModule mpm_prefork_module>
	LoadModule cgi_module lib/httpd/modules/mod_cgi.so
</IfModule>

# Options Indexes FollowSymLinks ExecCGI

ScriptAlias /cgi-bin/ "/opt/homebrew/var/www/cgi-bin/"

<Directory "/opt/homebrew/var/www/cgi-bin">
    AllowOverride None
    Options None
    Require all granted
</Directory>

# AddHandler cgi-script .cgi .sh

```



#### Désinstallation

```bash
$ brew uninstall httpd

$ rm -rf /opt/homebrew/etc/httpd
```



### Liens:

[:fa-link: https://getgrav.org/blog/macos-bigsur-apache-multiple-php-versions](https://getgrav.org/blog/macos-bigsur-apache-multiple-php-versions)

[:fa-link: https://lukearmstrong.github.io/2016/12/setup-apache-mysql-php-homebrew-macos-sierra/](https://lukearmstrong.github.io/2016/12/setup-apache-mysql-php-homebrew-macos-sierra/)

[:fa-link: https://httpd.apache.org/docs/2.4/howto/cgi.html](https://httpd.apache.org/docs/2.4/howto/cgi.html)

