ad_728x90

2013年6月19日 星期三

Install and configure Apache, MySQL, PHP and phpMyAdmin on OSX 10.8 Mountain Lion [I]

Get your Local Web Development Server Purring on all Fours

With the new cat out of the bag, getting the AMP stack running is a little different on OS X Mountain Lion 10.8 than is its predecessor OS X 10.7 Lion, here is the lowdown on getting Apache, MySQL, PHP and phpMyAdmin running on the new Apple operating system. (OSX 10.7 AMP guide is here).
If you have upgraded from a previous OS X your hosting environment will be whacked and you need to fix certain configuration files to get that environment back, in particular enabling PHP, and if you used the username/Sites document root folder in your home account you will need to add back in a user configuration file.
Apache/WebSharing

The first difference in the new OS X 10.8 is the dropping of the GUI option in System Preferences > Sharing to turn onWeb Sharing, it may be gone but the webserverApache is definitely under the hood of the OS and ready to go.


No Web Sharing Option in System Preferences
Apache is pre-installed in the OS and needs to be enabled via the command line - this needs to be done in Terminal which is found at /Applications/Utilities/Terminal
to start it

sudo apachectl startto stop it

sudo apachectl stopto restart it

sudo apachectl restartTo find the Apache version

httpd -vThe version installed in Mountain Lion is Apache/2.2.22

After starting Apache - test to see if the webserver is working in the browser -http://localhost - you should see the "It Works!" text.
If you want a GUI point and click web sharing toggle switch in System Preferences, this is one from clickontyler. Some people have had issues with this sys pref so use at your own decision.
Document Root

Document root is the location where the files are shared from the file system and is similar to the traditional names of 'public_html' and 'htdocs', OSX has historically had 2 web roots one at a system level and one at a user level - you can set both up or just run with one, the user level one allows multiple acounts to have their own web root whilst the system one is global for all users. It seems there is less effort from Apple in continuing with the user level one but it still can be set up with a couple of extra tweaks.
System Level Web Root

- the default system document root is still found at - 

http://localhost/The files are shared in the filing system at - 

/Library/WebServer/Documents/User Level Root

Interestingly the user document root level is missing the '~/Sites' folder in the User account on a clean installation, you need to make a "Sites" folder at the root level of your account and then it will work. Upgrading from a previous OS X version preserves the Sites folder but removes the ability to web serve from it - this is where you need to add in a 'username.conf' file.
Create a Sites folder at the account root level 
Check that you have a “username.conf” filed under:

/etc/apache2/users/If you don’t (very likely), then create one named by the short username of the account with the suffix.conf, it's location and permissions/ownership is best tackled by using the Terminal, the text editor 'nano' would be the best to deal with this. 
Launch Terminal, (Applications/Utilities), and follow the commands below, first one gets you to the right spot, 2nd one cracks open the text editor on the command line (swap 'username' with your account's shortname, if you don't know your account shortname type 'whoami' the Terminal prompt):

cd /etc/apache2/users
sudo nano username.confThen add the content below swapping in your 'username' in the code below:

<Directory "/Users/username/Sites/">Options Indexes MultiViewsAllowOverride AllOrder allow,denyAllow from all</Directory>Permissions on the file should be:

-rw-r--r-- 1 root wheel 298 Jun 28 16:47 username.confIf not you need to change...

sudo chmod 644 username.confRestart Apache for the new file to be read:

sudo apachectl restartThen this user level document root will be viewable at:

http://localhost/~username/

COMMENTS HAVE BEEN DISABLED FOR THIS POST [文章的評論已被禁用]

Ratings and Recommendations by outbrain