# Apache



# Installer Apache (homebrew)

### Installation:

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



### Arrêter / redémarrer Apache

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



### Log:

```bash
$ tail -f /usr/local/var/log/httpd/error_log
```

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



### Version:

```bash
$ httpd -v
```



### Modules activés:

```bash
$ httpd -M

$ apachectl -M
```



### Configuration:

Ouvrir le fichier *httpd.conf*:

```bash
$ open -e /usr/local/etc/httpd/httpd.conf
$ bbedit /usr/local/etc/httpd/extra/httpd-vhosts.conf
```



### Virtual Hosts:

#### Redirection:

Editer le fichier *hosts*:

```bash
$ sudo nano /etc/hosts

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

Editer le fichier *httpd-vhosts.conf*:

```http
<VirtualHost *:80>
    DocumentRoot /Users/bruno/Sites/wordpress
    ServerName wordpress.silverbook.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`:
```bash
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       silverbook.local
127.0.0.1       wiki.silverbook.local
```

Editer le fichier *httpd-vhosts.conf*:

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

```



### SSL:

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

```http
LoadModule socache_shmcb_module lib/httpd/modules/mod_socache_shmcb.so
...
LoadModule ssl_module lib/httpd/modules/mod_ssl.so
...
Include /usr/local/etc/httpd/extra/httpd-ssl.conf
```

<u>Ouvrir le fichier *httpd-ssl.conf*:</u>

Remplacer le port 8443 par défaut par le port 443 et commenter 2 lignes.

```http
Listen 443
...
<VirtualHost _default_:443>
\# General setup for the virtual host
\# DocumentRoot "/usr/local/var/www"
\# ServerName www.example.com:443
```

<u>Ouvrir le fichier *httpd-vhosts.conf*:</u>

Rajouter ce bloc pour chaque Virtual Host.

```http
<VirtualHost *:443>
   DocumentRoot "/Users/bruno/Sites"
   ServerName silverbook.local
   SSLEngine on
   SSLCertificateFile "/usr/local/etc/httpd/server.crt"
   SSLCertificateKeyFile "/usr/local/etc/httpd/server.key"
</VirtualHost>
```

https://deliciousbrains.com/https-locally-without-browser-privacy-errors/



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

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
```



### 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="/usr/local/etc/httpd/server.crt"

# D'après SSLCertificateFile "/usr/local/etc/httpd/server.crt" 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
```



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

Ouvrir le fichier *httpd-ssl.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 /usr/local/etc/httpd/extra/httpd-multilang-errordoc.conf
```



### Voir si Apache tourne:

```bash
$ ps -aef | grep httpd
0 23417     1   0  2:48PM ??         0:00.06 /usr/local/opt/httpd24/bin/httpd -D FOREGROUND
```



### M-à-J:

directement depuis Homebrew



### Installer phpmyadmin (Homebrew):

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

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

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

```http
  Alias /phpmyadmin /usr/local/share/phpmyadmin
  <Directory /usr/local/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>
```



### Message d’erreur personnalisé:

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



### Différence installation macOS - homebrew:

***MacOS:***

```
/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:***

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

/Library/LaunchDaemons/homebrew.mxcl.httpd.plist

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

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

/usr/local/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:

`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"
```



### Liens:

[:fa-link: https://getgrav.org/blog/macos-sierra-apache-multiple-php-versions](https://getgrav.org/blog/macos-sierra-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/)