{"id":101,"date":"2025-06-12T23:05:30","date_gmt":"2025-06-12T13:05:30","guid":{"rendered":"http:\/\/abmservermanagement.com.au\/?p=101"},"modified":"2025-06-12T23:05:30","modified_gmt":"2025-06-12T13:05:30","slug":"setting-up-a-web-server-on-macos","status":"publish","type":"post","link":"http:\/\/abmservermanagement.com.au\/index.php\/2025\/06\/12\/setting-up-a-web-server-on-macos\/","title":{"rendered":"Setting up a web server on macOS"},"content":{"rendered":"\n<p>Lines in <strong>bold<\/strong> are what you will have to type in. Replace &lt;your short user name&gt; with your short user name.<\/p>\n\n\n\n<p>Here goes&#8230; Enjoy!<\/p>\n\n\n\n<p>To get started, edit the Apache configuration file as root:<\/p>\n\n\n\n<p><strong>sudo vi \/etc\/apache2\/httpd.conf<\/strong><\/p>\n\n\n\n<p>Sorry, but PHP is <strong>NOT<\/strong> included in Sonoma. Apple even includes the following note at line 187 of this file:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#PHP was deprecated in macOS 11 and removed from macOS 12\n<\/code><\/pre>\n\n\n\n<p>Unfortunately, Perl will also not work with Apache in Sonoma. Sonoma ships with a broken version of Perl that will not work with the mod_perl module. See this bug report: <a href=\"https:\/\/github.com\/Perl\/perl5\/issues\/17154\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/github.com\/Perl\/perl5\/issues\/17154<\/a><\/p>\n\n\n\n<p>So for now, we\u2019ll go old-school and use the ancient mod_cgi method. As an added benefit, if you install your own PHP interpreter, you can run PHP scripts this way too. Your CGI scripts will run more slowly than with the mod_* versions.<\/p>\n\n\n\n<p>If you want to run CGI scripts, you will need to do the following:<\/p>\n\n\n\n<p>Enable mod_cgi by uncommenting line 174 changing:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\t#LoadModule cgi_module libexec\/apache2\/mod_cgi.so\n<\/code><\/pre>\n\n\n\n<p>to<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\tLoadModule cgi_module libexec\/apache2\/mod_cgi.so\n<\/code><\/pre>\n\n\n\n<p>(Note that this is dependent on the state of the mpm_prefork_module, which is enabled by default. This is User Tip is meant to be as simple as possible. Don\u2019t overcomplicate things.)<\/p>\n\n\n\n<p>Enable personal websites by uncommenting the following at line 184:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#LoadModule userdir_module libexec\/apache2\/mod_userdir.so\n<\/code><\/pre>\n\n\n\n<p>to<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>LoadModule userdir_module libexec\/apache2\/mod_userdir.so\n<\/code><\/pre>\n\n\n\n<p>(See below for an important new ACL change that are required in Sonoma for personal websites.)<\/p>\n\n\n\n<p>and do the same at line 521:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#Include \/private\/etc\/apache2\/extra\/httpd-userdir.conf\n<\/code><\/pre>\n\n\n\n<p>to<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Include \/private\/etc\/apache2\/extra\/httpd-userdir.conf\n<\/code><\/pre>\n\n\n\n<p>Now save and quit.<\/p>\n\n\n\n<p>Open the file you just enabled above with:<\/p>\n\n\n\n<p><strong>sudo vi \/etc\/apache2\/extra\/httpd-userdir.conf<\/strong><\/p>\n\n\n\n<p>and uncomment the following at line 16:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#Include \/private\/etc\/apache2\/users\/*.conf\n<\/code><\/pre>\n\n\n\n<p>to<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Include \/private\/etc\/apache2\/users\/*.conf\n<\/code><\/pre>\n\n\n\n<p>Save and exit.<\/p>\n\n\n\n<p>Lion and later versions no longer create personal web sites by default. If you already had a Sites folder, it should still be there. To create one manually, enter the following:<\/p>\n\n\n\n<p><strong>mkdir ~\/Sites<\/strong><\/p>\n\n\n\n<p><strong>echo &#8220;&lt;html&gt;&lt;body&gt;&lt;h1&gt;My site works&lt;\/h1&gt;&lt;\/body&gt;&lt;\/html&gt;&#8221; &gt; ~\/Sites\/index.html.en<\/strong><\/p>\n\n\n\n<p>While you are in \/etc\/apache2, double-check to make sure you have a user config file. It should exist at the path: \/etc\/apache2\/users\/&lt;your short user name&gt;.conf.&nbsp;<\/p>\n\n\n\n<p>That file may not exist and if you upgrade from an older version, you may still not have it. If that file doesn&#8217;t exist, you will need to create it with:<\/p>\n\n\n\n<p><strong>sudo vi \/etc\/apache2\/users\/&lt;your short user name&gt;.conf<\/strong><\/p>\n\n\n\n<p>Even if the file does exist, double check the content.&nbsp; Use the following as the content:<\/p>\n\n\n\n<p><strong>&lt;Directory &#8220;\/Users\/&lt;your short user name&gt;\/Sites\/&#8221;&gt;&nbsp;<\/strong><\/p>\n\n\n\n<p><strong>&nbsp; AddLanguage en .en&nbsp;<\/strong><\/p>\n\n\n\n<p><strong>&nbsp; AddHandler cgi-script .cgi .pl .php<\/strong><\/p>\n\n\n\n<p><strong>&nbsp; Options Indexes MultiViews FollowSymLinks ExecCGI<\/strong>&nbsp;<\/p>\n\n\n\n<p><strong>&nbsp; AllowOverride None&nbsp;<\/strong><\/p>\n\n\n\n<p><strong>&nbsp; Require host localhost<\/strong><\/p>\n\n\n\n<p><strong>&lt;\/Directory&gt;<\/strong><\/p>\n\n\n\n<p>You may need to make changes to this configuration to enable things such as additional languages, access from devices other than localhost, and .htaccess file overrides.<\/p>\n\n\n\n<p>There were new security defaults in macOS 13 \u201cVentura\u201d. By default, other users have no access to another user&#8217;s home directory. This includes the special \u201c_www\u201d user that is running the Apache web server. Run the following command to give the Apache web server access to the Sites folder in your home directory.<\/p>\n\n\n\n<p><strong>chmod +a &#8220;_www allow execute&#8221; ~<\/strong><\/p>\n\n\n\n<p>This will add an ACL permission to your home directory that will allow the Apache web server access to all subdirectories inside your home directory. The permissions on those subdirectories may allow, or deny, access to other users such as the _www user. If you wanted to be extra paranoid, you could change the permissions on all subdirectories (except for the \u201cSites\u201d folder, obviously) to disallow any access from other users. But this User Tip is a minimal setup guide, so we will just do the minimum here.<\/p>\n\n\n\n<p>Now you are ready to turn on Apache itself. But first, do a sanity check. Sometimes copying and pasting from an internet forum can insert invisible, invalid characters into config files. Check your configuration by running the following command in the Terminal:<\/p>\n\n\n\n<p><strong>apachectl configtest<\/strong><\/p>\n\n\n\n<p>If this command returns &#8220;Syntax OK&#8221; then you are ready to go. It may also print a warning saying &#8220;httpd: Could not reliably determine the server&#8217;s fully qualified domain name&#8221;. You could fix this by setting the ServerName directive in \/etc\/apache2\/httpd.conf and adding a matching entry into \/etc\/hosts. But for a development server, you don&#8217;t need to do anything. You can just ignore that warning. You can safely ignore other warnings too.<\/p>\n\n\n\n<p>Turn on the Apache httpd service by running the following command in the Terminal:<\/p>\n\n\n\n<p><strong>sudo launchctl load -w \/System\/Library\/LaunchDaemons\/org.apache.httpd.plist<\/strong><\/p>\n\n\n\n<p>This command could fail with an error: \u201cLoad failed: 37: Operation already in progress\u201d. If so, that means your web server is already running from a previous operating system version. However, you will still need to bump Apache to reload with the configuration changes you\u2019ve just made. Use the following command:<\/p>\n\n\n\n<p><strong>sudo apachectl graceful<\/strong><\/p>\n\n\n\n<p>In Safari, navigate to your web site by entering the following into Safari&#8217;s address bar:<\/p>\n\n\n\n<p><a href=\"http:\/\/localhost\/\" target=\"_blank\" rel=\"noreferrer noopener\"><strong>http:\/\/localhost\/<\/strong><\/a><\/p>\n\n\n\n<p>It should say:<\/p>\n\n\n\n<p><strong>It works!<\/strong><\/p>\n\n\n\n<p>Now try your user home directory by entering the following into Safari&#8217;s address bar:<\/p>\n\n\n\n<p><strong>http:\/\/localhost\/~&lt;your short user name&gt;<\/strong><\/p>\n\n\n\n<p>It should say:<\/p>\n\n\n\n<p><strong>My site works<\/strong><\/p>\n\n\n\n<p>To test Perl, try something similar. Create a Perl test file with:<\/p>\n\n\n\n<p><strong>vi ~\/Sites\/info.pl<\/strong><\/p>\n\n\n\n<p>Use the following as the content:<\/p>\n\n\n\n<p><strong>#!\/usr\/bin\/perl<\/strong><\/p>\n\n\n\n<p><strong>use CGI;<\/strong><\/p>\n\n\n\n<p><strong>my $cgi = CGI-&gt;new;<\/strong><\/p>\n\n\n\n<p><strong>print $cgi-&gt;header( -type =&gt; &#8216;text\/plain&#8217; );<\/strong><\/p>\n\n\n\n<p><strong>print $ENV{SERVER_SOFTWARE};<\/strong><\/p>\n\n\n\n<p>And test it by entering the following into Safari&#8217;s address bar:<\/p>\n\n\n\n<p><strong>http:\/\/localhost\/~&lt;your short user name&gt;\/info.pl<\/strong><\/p>\n\n\n\n<p>Make sure the file can be executed by the _www user by doing this:<\/p>\n\n\n\n<p><strong>chmod ugo+x ~\/Sites\/info.pl<\/strong><\/p>\n\n\n\n<p>You should see the following string:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Apache\/2.4.56 (Unix)\n<\/code><\/pre>\n\n\n\n<p>Doing the same for PHP is left as an exercise for the reader. You will have to download and install a PHP interpreter. For extra credit, try something more modern like Python or Rust.<\/p>\n\n\n\n<p>If you want to setup MySQL, see <a href=\"https:\/\/discussions.apple.com\/docs\/DOC-3082\" target=\"_blank\" rel=\"noreferrer noopener\">my User Tip on Installing MySQL<\/a>.<\/p>\n\n\n\n<p>If you want to make further changes to your Apache system or user config files, you will need to restart the Apache server with:<\/p>\n\n\n\n<p><strong>sudo apachectl graceful<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Lines in bold are what you will have to type in. Replace &lt;your short user name&gt; with your short user name. Here goes&#8230; Enjoy! To get started, edit the Apache configuration file as root: sudo vi \/etc\/apache2\/httpd.conf Sorry, but PHP is NOT included in Sonoma. Apple even includes the following note at line 187 of [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-101","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"http:\/\/abmservermanagement.com.au\/index.php\/wp-json\/wp\/v2\/posts\/101","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/abmservermanagement.com.au\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/abmservermanagement.com.au\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/abmservermanagement.com.au\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/abmservermanagement.com.au\/index.php\/wp-json\/wp\/v2\/comments?post=101"}],"version-history":[{"count":1,"href":"http:\/\/abmservermanagement.com.au\/index.php\/wp-json\/wp\/v2\/posts\/101\/revisions"}],"predecessor-version":[{"id":102,"href":"http:\/\/abmservermanagement.com.au\/index.php\/wp-json\/wp\/v2\/posts\/101\/revisions\/102"}],"wp:attachment":[{"href":"http:\/\/abmservermanagement.com.au\/index.php\/wp-json\/wp\/v2\/media?parent=101"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/abmservermanagement.com.au\/index.php\/wp-json\/wp\/v2\/categories?post=101"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/abmservermanagement.com.au\/index.php\/wp-json\/wp\/v2\/tags?post=101"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}