Setup a local web server on macOS Ventura

Prerequisite

You will need the Homebrew package manager for macOS (it works on Linux also).

First of all, check if you have Homebrew already installed and running in you system. Open a terminal and run this command:

brew -v

you should see on output like this one:

Homebrew 4.1.11

if you get on output like

zsh: command not found: brew

it means that you have to install it in your system. Run in your terminal the following command:

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

If you would like to learn more about the Homebrew package manager, feel free to browse the complete Homebrew documentation on the official website.

Apache HTTP server

Run this command and the latest stable version (2.4.57 at the time of writing this article) of the Apache HTTP server will be installed in your system

brew install httpd

The configuration files will be available in /usr/local/etc/httpd/

To start the httpd service now and restart at login enter this command in your terminal:

brew services start httpd

or, if you don’t want/need a background service you can just run:

/usr/local/opt/httpd/bin/httpd -D FOREGROUND

The -D flag is used to define configuration directives or settings for Apache. In this case, it’s followed by FOREGROUND, which typically tells Apache to run in the foreground (as opposed to running as a background daemon). Running in the foreground means that the Apache process will stay in the foreground, and you will see its log output in your terminal. This is useful for debugging and monitoring the server.

 

To start / stop / restart the httpd service:

brew services start httpd
brew services stop httpd
brew services restart httpd

If you would like to configure SSL (https) connections in your local Apache web server, on macOS Ventura, follow this article. If you would like to create multiple local domains for your projects, follow this article.

If you like to know more about the Apache HTTP server, feel free to browse the official website.

MySQL

Run this command and the latest stable version (8.1.0 at the time of writing this article) of the MySQL database server will be installed in your system

brew install mysql

The configuration file will be available in /etc/my.cnf

To start the mysql service now and restart at login enter this command in your terminal:

brew services start mysql

or, if you don’t want/need a background service you can just run:

/usr/local/opt/mysql/bin/mysqld_safe --datadir=/usr/local/var/mysql
  • --datadir: This is an option used to specify the location of the MySQL data directory, which is where MySQL stores its databases and related data
  • /usr/local/var/mysql: This path is the actual location of the data directory where MySQL will store its data files

To start / stop / restart the httpd service:

brew services start mysql
brew services stop mysql
brew services restart mysql

MySQL is configured to only allow connections from localhost by default. To connect to MySQL run this command in your terminal:

mysql -u root

By default, the MySQL user root has no password set.

If you like to know more about the MySQL database server, feel free to browse the official website.

PHP

Run this command and the latest stable version (8.2.10 at the time of writing this article) of the PHP scripting language will be installed in your system

brew install php

The php.ini and php-fpm.ini configuration files can be found in /usr/local/etc/php/8.2/

To enable PHP in Apache add the following to /usr/local/etc/httpd/httpd.conf and restart Apache:

LoadModule php_module /usr/local/opt/php/lib/httpd/modules/libphp.so

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

Finally, check that the DirectoryIndex directive includes index.php

DirectoryIndex index.php index.html

To start PHP now and restart at login, run this command in your terminal:

brew services start php

or, if you don’t want/need a background service you can just run:

/usr/local/opt/php/sbin/php-fpm --nodaemonize

--nodaemonize is an option or flag passed to PHP-FPM. When you include --nodaemonize, it tells PHP-FPM not to run as a background daemon process. In other words, it will stay in the foreground and display its output to the terminal. This can be useful for debugging and monitoring PHP-FPM, as you can see its log output and status messages in real-time.

To start / stop / restart the php service:

brew services start php
brew services stop php
brew services restart php

If you like to know more about the MySQL database server, feel free to browse the official website.