Mac OS (Catalina) Laravel + Apache + PHP 7.4

Vlad
3 min readMay 18, 2021

Just recently I’ve been struggling trying to put these together for my development environment and turned out there are some issues, that make things really difficult.

  1. Xcode comes with PHP 7.3, but latest Laravel version requires you to use PHP 7.4. You can install PHP 7.4 version with brew, BUT that version is not compatible with default Mac’s web server (which is Apache). Now you need to install Apache/Nginx from brew as well. Which brings other problems, for example: a) you need to disable default Apache. b) If you want to run brew Apache on port 80, then you need to run brew service start httpd as root, otherwise your server can’t run on ports lower that 1024. BTW I’ve failed when tried out Apache and Nginx from brew. It didn’t work out of the box with PHP provided. And I could not afford to spend any more of my time to figure it all out
  2. In my production environment on the server all CORS headers are handled by nginx. Thats the reason I don’t want to use any Laravel CORS plugins. But without those plugins there is no way to use API calls with builtin Laravel web-server on my local machine since my front-end is totally different application which runs on Nuxt.js (I aware of axios proxy option, but I also noticed there are issues with SSL)

To sum these up: can’t make default Apache to work with PHP 7.4; cant’t use builtin server php artisan serve with API. There also was another issue I’ve stumbled about openssl and cipher when aes-256-ctr (after installing/reinstalling PHP) became unavailable and I could not login using Laravel’s Passport due of lacking methods. All available cipher methods on your system you can browse by running following PHP code: var_dump(\openssl_get_cipher_methods());

The solution I came up with was to use default Apache server as HTTP proxy to localhost:8000, then run Laravel’s builtin server.

First off you need to enable mod_proxy in your /etc/apache2/httpd.conf. You would need to find these lines and uncomment them:

LoadModule proxy_module libexec/apache2/mod_proxy.so
LoadModule proxy_http_module libexec/apache2/mod_proxy_http.so
LoadModule proxy_balancer_module libexec/apache2/mod_proxy_balancer.so

Then create config file for your local website. Let’s say you wanna run api.mydomain.local. Then create a virtualhost config file /etc/apache2/other/api.mydomain.local.conf

<VirtualHost *:80>ServerName api.mydomain.local
Header set Access-Control-Allow-Origin "*"
Header add Access-Control-Allow-Headers "*"
Header add Access-Control-Allow-Methods "PUT, GET, POST, DELETE, OPTIONS, PATCH"
ProxyPreserveHost On
ProxyRequests Off
ProxyPass / http://localhost:8000/
ProxyPassReverse / http://localhost:8000/
ErrorLog /var/log/apache2/api.mydomain.local-error_log</VirtualHost>

And restart apache: sudo apachectl restart OR sudo killall httpd for a few times, then wait for a few second till the system will bring it back.

Don’t forget to add api.mydomain.local into your /etc/hosts file (sudo vim /etc/hosts):

127.0.0.1 api.mydomain.local

Install PHP 7.4 version with brew. To check if all was okay run php -v

shirker@VladsMac ~ % php -vPHP 7.4.16 (cli) (built: Apr 25 2021 08:08:01) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies

Now you can launch php artisan serve and voilà — your API website is up and running on php 7.4 and accessible by http://api.mydomain.localand you can manage your CORS headers via your apache virtualhost config file

--

--

Vlad

Just another web developer among the millions