Mac - Apache MySql PHP Server mit Homebrew

Aus Wikizone
Wechseln zu: Navigation, Suche
https://help.macstadium.com/articles/how-to-install-apache-mysql-and-php-using-homebrew-on-macos
https://gist.github.com/DragonBe/0faebe58deced34744953e3bf6afbec7

Requirements

Requirements

XCode über den App Store installieren Homebrew wie im Wiki beschrieben.

Apache

MacOs System Apache Autostart abschalten

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

System Apache stoppen

sudo apachectl stop

Wahrscheinlich läuft aber keiner, es sei denn du hast ihn vorher aktiviert. Daher kann es gut sein dass eine

‘could not find specified service’.

Meldung kommt.

Homebrew Apache installieren

Now we can proceed with the installation of Apache 2.4:

brew install httpd

Achte darauf wie der Apache eingerichtet wird. Bei mir ist das folgendermaßen:

DocumentRoot is /usr/local/var/www.

The default ports have been set in /usr/local/etc/httpd/httpd.conf to 8080 and in
/usr/local/etc/httpd/extra/httpd-ssl.conf to 8443 so that httpd can run without sudo.

To have launchd start httpd now and restart at login:
  brew services start httpd
Or, if you don't want/need a background service you can just run:
  apachectl start

Check of Apache läuft

Achte auf den Pfad der während der Homebrew-Cask Installation genutzt wurde. Du könntest Ihn verändert haben:

which apachectl

Ausgabe:

/usr/local/bin/apachectl

Du kannst auch

httpd -v

Ausgabe:

Server version: Apache/2.4.39 (Unix)
Server built:   Apr 19 2019 17:54:07

Nutzen. Das gibt dann die Version und das built ähnlich wie oben aus.

Apache starten

Jetzt können wir den Apache starten

sudo apachectl start

Die Webseite erreichst du im Browser über den localhost (Port 8080 vgl. Ausgabe oben)

http://localhost:8080/

oder die Lokale IP Adresse. Es sollte die typische Apache Seite kommen.

“It Works!”

Apache Autostart (optional)

Wenn du den Apache beim Mac Start automatisch starten willst, kannst du einen Homebrew Service ienrichten

sudo brew services start httpd

Ich mache das nicht, da ich auch noch den XAMPP für kleinere Sachen nutze, da muss nicht der brew apache die ganze Zeit laufen.

Apache Module und Port konfigurieren

Den Port kannst du hier ändern. Standardmäßig richtet brew den 8080 ein. Eigentlich ganz gut bei mir, da der XAMPP auf dem Standardport 80 läuft, so kann ich theoretisch beide auch mal gleichzeitig nutzen.

open -e /usr/local/etc/httpd/httpd.conf

Finde die Zeile mit

Listen 8080

und passe sie z.B. auf 80 an. Ich lasse es auf 8080

Erlaubte Direktiven für .htaccess - AllowOverride

Mit AllowOverride controls regelt der Server wo Apache Zugriff hat. Hier sollte man sorgfältig vorgehen, auf keinen Fall sollte er überall Zugriff haben. Im ersten Block verbietet man erstmal alles um dann in den Web Directories wieder einzeln Zugriff zu geben.

Im httpd.conf File ist das meiste ganz gut beschrieben. Im Zweifel Suchmaschine o.ä. bemühen.

#
# Deny access to the entirety of your server's filesystem. You must
# explicitly permit access to web content directories in other 
# <Directory> blocks below.
#
<Directory />
    AllowOverride none
    Require all denied
</Directory>

Oft sinnvoll ist eine Regelung über htaccess Files

  1. AllowOverride controls what directives may be placed in .htaccess files.
  2. It can be "All", "None", or any combination of the keywords:
  3. AllowOverride FileInfoAuthConfig Limit

AllowOverride All

mod_rewrite aktivieren

Suche nach

LoadModule rewrite_module lib/httpd/modules/mod_rewrite.so

und entferne den Hash vorne

Servername

Ein Domainname benötigt eine DNS hier kann man auch einfach die IP Adresse eingeben unter der der Host Rechner erreichbar ist. Oder man läßt es leer, wenn man es nicht benötigt.

#ServerName www.example.com:8080 

Replace it with your IP Address or domain:

ServerName 192.1.2.24
Apache neu starten
sudo apachectl -k restart 

PART 2 – PHP

Mit Brew kann man verschiedene Versionen installieren. Wir entscheiden uns hier für 7.2

Install PHP 7.2 using brew.

brew install php@7.2

Statt 7.2 geht auch eine ander php Version z.B. 5.6, 7.0, 7.1. Ohne @x.y installiert er die neueste (bei mir war das 7.3, die ich wieder deinstalliert habe, da es mit älteren Tools Probleme gibt)

PHP Konfigurieren

Once the installation finishes you can begin editing files, beginning with php.ini. It is the default configuration file for running applications.

You can decide to change the memory_limit, upload_max_filesize or max_execution_time. You can also activate safe mode.

Now you need to go back to your httpd.conf file which we edited during the Apache installation process and add a line which will connect PHP with Apache once again use:

open -e /usr/local/etc/httpd/httpd.conf Add libphp somewhere below the last edit we made by uncommenting LoadModule:

LoadModule php7_module /usr/local/opt/php@7.2/lib/httpd/modules/libphp7.so At your discretion depending on the project you’re working on, you can also edit an option which will allow you to load index.php or index.html

Look for:

<IfModule dir_module>

  DirectoryIndex index.html

</IfModule>

And replace it with:

<IfModule dir_module>

  DirectoryIndex index.htmlindex.htm index.php 

</IfModule> Now you can stop and start apache once again to validate the changes.

PART 3 – MYSQL

At last you can install mysql and the process is very straightforward. You can even use the Native Packages to run the whole installation process.

For this example, we will go through the process of installing it with Homebrew:

brew install mysql Now you can load and start services:

brew services start mysql It will tell you to secure your installation, run the following command:

mysql_secure_installation Set a new root password, remove anonymous users and other options to limit access to localhost only.

You can now restart the MySQL server:

brew services restart mysql You’re all set.