Return to site

Apache Web Server For Mac Os

broken image


This was developed into Rhapsody in 1997, Mac OS X Server 1.0 in 1999, Mac OS X Public Beta in 2000, and Mac OS X 10.0 in 2001. In 1999, Apple announced it would release the Mach 2.5 microkernel, BSD Unix 4.4 OS, and the Apache Web server components of Mac OS X Server.

macOS Update: While these instructions still work, there are new posts for recent versions of macOS, the latest being Install Apache, PHP, and MySQL on macOS Mojave.

PHP Update: Mac OS X El Capitan comes pre-installed with PHP version 5.5 which has reached its end of life. After you complete this post, you should upgrade PHP on Mac OS X.

Note: This post is for new installations. If you have installed Apache, PHP, and MySQL for Mac OS X Yosemite, read my post on Updating Apache, PHP, and MySQL for Mac OS X El Capitan.

  1. Change web root apache mac OS X. Ask Question Asked 8 years, 7 months ago. Active 2 years, 3 months ago. Viewed 82k times 21. I cant change my web root in apache, for some reason it points to /Sites/folder1/folder2 instead of it pointing to /Sites. How can I start PostgreSQL server on Mac OS X? Changing documentroot for apache on.
  2. On my Mac OS X, Web Sharing is disabled in System Preferences - Sharing - Services - 'Personal Web Sharing'. Still, when I type 'localhost' in the browser, I get the 'It works!' Message, meaning that Apache is still running.

Mac OS X runs atop UNIX. So most UNIX software installs easily on Mac OS X. Furthermore, Apache and PHP come packaged with Mac OS X. To create a local web server, all you need to do is configure Apache and install MySQL.

I am aware of the web server software available for Mac OS X, notably MAMP. These get you started quickly. But they forego the learning experience and, as most developers report, can become difficult to manage.

Running Commands

First, open the Terminal app and switch to the root user so you can run the commands in this post without any permission issues:

Enable Apache on Mac OS X

Verify It works! by accessing http://localhost

Enable PHP for Apache

First, make a backup of the default Apache configuration. This is good practice and serves as a comparison against future versions of Mac OS X.

Now edit the Apache configuration. Feel free to use TextEdit if you are not familiar with vi.

Uncomment the following line (remove #):

Restart Apache:

You can verify PHP is enabled by creating a phpinfo() page in your DocumentRoot.

The default DocumentRoot for Mac OS X El Capitan is /Library/WebServer/Documents. You can verify this from your Apache configuration.

Now create the phpinfo() page in your DocumentRoot:

Verify PHP by accessing http://localhost/phpinfo.php

Install MySQL on Mac OS X El Capitan

Download and install the latest MySQL generally available release DMG for Mac OS X.

The README suggests creating aliases for mysql and mysqladmin. However there are other commands that are helpful such as mysqldump. Instead, you can update your path to include /usr/local/mysql/bin.

Note: You will need to open a new Terminal window or run the command above for your path to update.

Finally, you should run mysql_secure_installation. While this isn't necessary, it's good practice to secure your database.

Connect PHP and MySQL

You need to ensure PHP and MySQL can communicate with one another. There are several options to do so. I do the following:

Additional Configuration (optional)

The default configuration for Apache 2.4 on Mac OS X seemed pretty lean. For example, common modules like mod_rewrite were disabled. You may consider enabling this now to avoid forgetting they are disabled in the future.

I edited my Apache Configuration:

I uncommented the following lines (remove #):

If you develop multiple projects and would like each to have a unique url, you can configure Apache VirtualHosts for Mac OS X.

If you would like to install PHPMyAdmin, return to my original post on installing Apache, PHP, and MySQL on Mac OS X.

Find this interesting? Let's continue the conversation on Twitter.

16 13 likes 31,725 views Last modified Jan 31, 2019 11:25 AM

Here is my definitive guide to getting a local web server running on OS X 10.14 'Mojave'. This is meant to be a development platform so that you can build and test your sites locally, then deploy to an internet server. This User Tip only contains instructions for configuring the Apache server, PHP module, and Perl module. I have another User Tip for installing and configuring MySQL and email servers.


Note: This user tip is specific to macOS 10.14 'Mojave'. Pay attention to your OS version. There have been significant changes since earlier versions of macOS.Another note: These instructions apply to the client versions of OS X, not Server. Server does a few specific tricks really well and is a good choice for those. For things like database, web, and mail services, I have found it easier to just setup the client OS version manually.


Requirements:

  1. Basic understanding of Terminal.app and how to run command-line programs.
  2. Basic understanding of web servers.
  3. Basic usage of vi. You can substitute nano if you want.


Optional: Xcode is required for adding PHP modules.


Lines in bold are what you will have to type in. Lines in bold courier should be typed at the Terminal.Replace with your short user name.


Here goes... Enjoy!


To get started, edit the Apache configuration file as root:

sudo vi /etc/apache2/httpd.conf


Enable PHP by uncommenting line 177, changing:

#LoadModule php7_module libexec/apache2/libphp7.so

to

LoadModule php7_module libexec/apache2/libphp7.so

(If you aren't familiar with vi, go to line 177 by typing '177G' (without the quotes). Then just press 'x' over the '#' character to delete it. Then type ':w!' to save, or just 'ZZ' to save and quit. Don't do that yet though. More changes are still needed.)


If you want to run Perl scripts, you will have to do something similar:


Enable Perl by uncommenting line 178, changing:

#LoadModule perl_module libexec/apache2/mod_perl.so

to

LoadModule perl_module libexec/apache2/mod_perl.so


Enable personal websites by uncommenting the following at line 174:

#LoadModule userdir_module libexec/apache2/mod_userdir.so

to

LoadModule userdir_module libexec/apache2/mod_userdir.so


and do the same at line 511:

#Include /private/etc/apache2/extra/httpd-userdir.conf

to

Include /private/etc/apache2/extra/httpd-userdir.conf

Now save and quit.


Open the file you just enabled above with:

sudo vi /etc/apache2/extra/httpd-userdir.conf

and uncomment the following at line 16:

#Include /private/etc/apache2/users/*.conf

to

Include /private/etc/apache2/users/*.conf

Save and exit.


Lion and later versions no longer create personal web sites by default. If you already had a Sites folder in Snow Leopard, it should still be there. To create one manually, enter the following:

mkdir ~/Sites

echo '

My site works

' > ~/Sites/index.html.en


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/.conf.

Download Apache Web Server


That file may not exist and if you upgrade from an older version, you may still not have it. It does appear to be created when you create a new user. If that file doesn't exist, you will need to create it with:

sudo vi /etc/apache2/users/.conf


Use the following as the content:

/Sites/'>

AddLanguage en .en

Mac Os Web Server

AddHandler perl-script .pl

PerlHandler ModPerl::Registry

Options Indexes MultiViews FollowSymLinks ExecCGI

AllowOverride None

Require host localhost


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:

apachectl configtest


If this command returns 'Syntax OK' then you are ready to go. It may also print a warning saying 'httpd: Could not reliably determine the server's fully qualified domain name'. 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't need to do anything. You can just ignore that warning. You can safely ignore other warnings too.


Turn on the Apache httpd service by running the following command in the Terminal:

sudo launchctl load -w /System/Library/LaunchDaemons/org.apache.httpd.plist


In Safari, navigate to your web site with the following address:

http://localhost/


It should say:


It works!


Now try your user home directory:

http://localhost/~


It should say:

Configure Apache Web Server Mac Os X


My site works


Apache Web Server For Mac Os

Now try PHP. Create a PHP info file with:

echo '' > ~/Sites/info.php


And test it by entering the following into Safari's address bar:

http://localhost/~/info.php


You should see your PHP configuration information.


To test Perl, try something similar. Create a Perl test file with:

Apache Web Server Mac Os

echo 'print $ENV{MOD_PERL} . qq{n};' > ~/Sites/info.pl


And test it by entering the following into Safari's address bar:

http://localhost/~/info.pl


You should see the string 'mod_perl/2.0.9'.


If you want to setup MySQL, see my User Tip on Installing MySQL.


If you want to add modules to PHP, I suggest the following site. I can't explain it any better.


Install Apache Web Server Mac Os X

If you want to make further changes to your Apache system or user config files, you will need to restart the Apache server with:

Install Apache On Mac

sudo apachectl graceful





broken image