Skip to content

Instantly share code, notes, and snippets.

@PeitongWang
Last active August 17, 2022 15:42
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save PeitongWang/9e8de1fb514906f1b2e9caad4791f5cc to your computer and use it in GitHub Desktop.
Save PeitongWang/9e8de1fb514906f1b2e9caad4791f5cc to your computer and use it in GitHub Desktop.
Mac自带Apache Web服务器基本使用
想着如何在Mac OS下部署静态网页(纯粹的html,css,js),用惯了windows下的iis,可惜Mac OS下也许只能通过Tomcat或者Apache之类的作为部署容器。听说Mac OS下自带了apache,于是就折腾起来。
环境:macbook air os x 10.9.5
找到apache藏身之所
系统默认是隐藏apache安装目录的,但我们可以通过“命令行”或者“文件夹前往”的方式找到它。它是安装在系统的私有目录下,也就是/private/etc下面,因为它是隐藏的,所以我们无法通过界面找到它。
ps:/ect目录是mac os 系统存放系统配置信息的地方,里面都是xxx.conf的配置文件。
命令行
在终端输入 open /etc 命令即可打开它的“住所”了。请看:
终端调用apache
文件夹前往
打开Finder > 前往 >前往文件夹。请看:
文件夹前往
apache下部署web资源
跟windows不一样,它的部署包不是放在htdocs(windows下的存放目录),而是放在 “/资源库/WebServer/Documents/”下面:
部署包所在文件夹
那么我们的静态资源就可以丢到这个目录下去了。
这个目录是apache的默认目录,有时候为了方便操作,可能需要指向特定的文件夹,该如何修改apache的配置呢?
修改默认部署路径
找到下面httpd.conf文件,配置转发,模块启动停用之类操作都在该文件里面。
httpd.conf文件
找到这个DocumentRoot,修改成你想要的地址即可
修改DocumentRoot
启动停用apache
启用apache的命令: sudo apachectl start/restart
如果需要password,输入即可,如果启动失败了,就可以去看apache的日志,找到错误的原因(前提是在httpd.conf中配置了日志的路径)
启动apache
在浏览器中输入localhost或者127.0.0.1即可看到“It works!”的提示。恭喜,apache启动成功了。停止apache的命令是:sudo apachectl stop
修改apache默认端口
通过localhost或者127.0.0.1访问,表示默认的端口是80,有时候如果80端口被占用了,就得换个端口试试了。同样是在httpd.conf下面,找到Listen 80 那一行,修改成你想要的端口即可。
修改端口
修改端口为81
其实,apache的用法还有非常多,比如 转发配置、ssl配置、日志配置等,后面慢慢补充,以备不时之需。
启动Apache
打开“终端(terminal)”,输入 sudo apachectl -v,(可能需要输入机器秘密)。如下显示Apache的版本
接着输入 sudo apachectl start,这样Apache就启动了。打开Safari浏览器地址栏输入 “http://localhost”,可以看到内容为“It works!”的页面。其位于“/Library(资源库)/WebServer/Documents/”下,这就是Apache的默认根目录。
Apache的安装目录在:/etc/apache2/,etc默认是隐藏的。有三种方式查看:
dock下右键Finder,选择"前往文件夹",输入"/etc"
在finder下----》前往---》前往文件夹,然后输入/etc
可以在terminal 输入 "open /etc"
设置虚拟主机
在终端运行“sudo vi /etc/apache2/httpd.conf”,打开Apche的配置文件
在httpd.conf中找到“#Include /private/etc/apache2/extra/httpd-vhosts.conf”,去掉前面的“#”,保存并退出。
运行“sudo apachectl restart”,重启Apache后就开启了虚拟主机配置功能。
运行“sudo vi /etc/apache2/extra/httpd-vhosts.conf”,就打开了配置虚拟主机文件httpd-vhost.conf,配置虚拟主机了。需要注意的是该文件默认开启了两个作为例子的虚拟主机:
<VirtualHost *:80>
ServerAdmin webmaster@dummy-host.example.com
DocumentRoot "/usr/docs/dummy-host.example.com"
ServerName dummy-host.example.com
ErrorLog "/private/var/log/apache2/dummy-host.example.com-error_log"
CustomLog "/private/var/log/apache2/dummy-host.example.com-access_log" common
</VirtualHost>
<VirtualHost *:80>
ServerAdmin webmaster@dummy-host2.example.com
DocumentRoot "/usr/docs/dummy-host2.example.com"
ServerName dummy-host2.example.com
ErrorLog "/private/var/log/apache2/dummy-host2.example.com-error_log"
CustomLog "/private/var/log/apache2/dummy-host2.example.com-access_log" common
</VirtualHost>
而实际上,这两个虚拟主机是不存在的,在没有配置任何其他虚拟主机时,可能会导致访问localhost时出现如下提示:
Forbidden
You don't have permission to access /index.php on this server
最简单的办法就是在它们每行前面加上#,注释掉就好了,这样既能参考又不导致其他问题。
增加如下配置
<VirtualHost *:80>
DocumentRoot "/Library/WebServer/Documents"
ServerName localhost
ErrorLog "/private/var/log/apache2/localhost-error_log"
CustomLog "/private/var/log/apache2/localhost-access_log" common
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "/Users/snandy/work"
ServerName mysites
ErrorLog "/private/var/log/apache2/sites-error_log"
CustomLog "/private/var/log/apache2/sites-access_log" common
<Directory />
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order deny,allow
Allow from all
</Directory>
</VirtualHost>
保存退出,并重启Apache。
运行“sudo vi /etc/hosts”,打开hosts配置文件,加入"127.0.0.1 mysites",这样就可以配置完成sites虚拟主机了,可以访问“http://mysites”了,在10.8之前Mac OS X版本其内容和“http://localhost/~[用户名]”完全一致。
注意,记录log的“ErrorLog "/private/var/log/apache2/sites-error_log"”也可以删掉,但记录日志其实是一个好习惯,在出现问题时可以帮助我们判断。如果保留这些log代码,一定log文件路径都是存在的,如果随便修改一个不存在的,会导致Apache无法服务而没有错误提示。
Apache配置
1- 启动
sudo apachectl start
启动后,访问 http://localhost/ 应该能看到"It works!"的初始页面,
vi /etc/apache2/httpd.conf
197行可以看到如下代码片段:
<Directory "/Library/WebServer/Documents">
#
# Possible values for the Options directive are "None", "All",
# or any combination of:
# Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
#
# Note that "MultiViews" must be named *explicitly* --- "Options All"
# doesn't give it to you.
#
# The Options directive is both complicated and important. Please see
# http://httpd.apache.org/docs/2.2/mod/core.html#options
# for more information.
#
Options Indexes FollowSymLinks MultiViews
#
# AllowOverride controls what directives may be placed in .htaccess files.
# It can be "All", "None", or any combination of the keywords:
# Options FileInfo AuthConfig Limit
#
AllowOverride None
#
# Controls who can get stuff from this server.
#
Order allow,deny
Allow from all
</Directory>
cd /Library/WebServer/Documents
It Works 内容在 index.html.en 这是apache的默认页
2- 停止/重启
sudo apachectl stop
sudo apachectl restart
3- 创建个人站点目录
cd ~
mkdir Sites
echo "helloWorld" >> index.html
sudo apachectl restart
然后再访问 http://localhost/~shelley/ 应该就能看到"helloWorld"的个人目录初始页面(注:~shelley需换成~你的用户名)
如果失败
sudo vi /etc/apache2/users/Guest.conf
<Directory "/Users/shelley/Sites">
Options Indexes MultiViews
AllowOverride None
Order allow,deny
Allow from all
</Directory>
为何一定是Sites目录名,
vi /etc/apache2/extra/httpd-userdir.conf
第10行
# Settings for user home directories
#
# Required module: mod_userdir
#
# UserDir: The name of the directory that is appended onto a user's home
# directory if a ~user request is received. Note that you must also set
# the default access control for these directories, as in the example below.
#
UserDir Sites
#
# Users might not be in /Users/*/Sites, so use user-specific config files.
#
Include /private/etc/apache2/users/*.conf
<IfModule bonjour_module>
RegisterUserSite customized-users
</IfModule>
4- 启动虚拟主机
默认情况下,apache的虚拟主机功能是关闭的
sudo vi /etc/apache2/httpd.conf
放开注释
#Virtual hosts
#Include /private/etc/apache2/extra/httpd-vhosts.conf
修改文件
sudo vi /etc/apache2/extra/httpd-vhosts.conf
类似以下内容
NameVirtualHost *:80
<VirtualHost *:80>
DocumentRoot "/Users/shelley/Sites"
ServerName www.shelleymyl.com
ErrorLog "/Users/shelley/Sites/log/error.log"
CustomLog "/Users/shelley/Sites/log/access.log" common
<Directory />
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order deny,allow
Allow from all
</Directory>
</VirtualHost>
  
5- URL转发
先打开httpd.conf,确保下面这二行没有被注释掉:
LoadModule proxy_module libexec/apache2/mod_proxy.so
LoadModule proxy_http_module libexec/apache2/mod_proxy_http.so
然后在httpd.conf最后加上
ProxyPass /HelloWorldApp http://localhost:8080/HelloWorldApp/ProxyPassReverse /HelloWorldApp http://localhost:8080/HelloWorldApp/
这样访问 http://localhost/HelloWorldApp、http://ip/HelloWorldApp、http://www.shelleymyl.com/HellpWorldApp 都相当于访问 http://localhost:8080/HelloWorldApp
6- 端口转发
假如服务器上有一个应用 http://x.x.x.x:8080/ ,如果想通过类似 http://www.shelleymyl.com 的域名来直接访问,就需要做端口转发,仍然打开httpd.conf
LoadModule proxy_connect_module modules/mod_proxy_connect.so
LoadModule proxy_ftp_module modules/mod_proxy_ftp.so
在"5、URL转发"的基础上,再打开这二项
然后修改
sudo vi /etc/apache2/extra/httpd-vhosts.conf
NameVirtualHost *:80
<VirtualHost *:80>
ProxyPreserveHost On
ServerName www.yjmyzz.com
ProxyPass / http://www.yjmyzz.com:8000/
ProxyPassReverse / http://www.yjmyzz.com:8000/
ServerAdmin webmaster@localhost
</VirtualHost>
这样就相当于把 80端口转发到8080端口上了
PHP配置
PHP的配置非常简单,就一个事
vi /etc/apache2/httpd.conf
LoadModule php5_module libexec/apache2/libphp5.so
放开注释
然后sudo apachectl restart 重启,在用户目录的Sites文件夹下,新建一个index.php,里面echo phpinfo() ,就可以看到效果了
在配置Linux的 Apache服务时,经常会遇到http403错误,我今天配置测试时也出现了,最后解决了,总结了一下。http 403错误是拒绝访问的意思,有很多原因的。还有,这些问题在win平台的Apache里一样会发生!我按照经验总结的主要有以下4种原因!
本人测试的环境 是:Scientific Linux 5.3(与RHEL和CentOS百分百兼容!),其它版本的Linux应该通用,没测试。
1. 访问的文档权限不够。要755以上权限。解决方法:用命令chmod 755 /var/www/ 或其他相应目录。
2. SELinux或防火墙的原因。解决方法:先关闭SELinux和让防火墙通过WWW服务。
3. 虚拟主机配置错误。例如我遇到过一次的:
httpd.conf里加载了虚拟主机的配置文件:
# Virtual hosts
Include conf/extra/httpd-vhosts.conf
而conf/extra/httpd-vhosts.conf并没有配置好,而且虚拟主机功能暂时还没有用,所以把Include conf/extra/httpd-vhosts.conf注释掉,重启apache后正常了。
解决方法:重新配置虚拟主机或暂时关闭。
4. DocumentRoot的设置。解决方法如下:
打开 apache的配置文件httpd.conf,找到这段代码:
<Directory />
Options FollowSymLinks
AllowOverride None
Order deny,allow
Deny from all
</Directory>
有时候由于配置了php后,这里的“Deny from all”已经拒绝了一切连接。把该行改成“allow from all”,修改后的代码如下,问题解决。
<Directory />
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order deny,allow
Allow from all
</Directory>
事实上 Mac OS X 中想要搭建这样的环境很简单,接下来就会将详细的教程分享给大家。
首先需要说明的是,Mac OS X 系统其实已经集成了 Apache + PHP 环境,用户手动开启即可。不过在最近的的 10.8/10.9 中苹果取消了这个共享功能的图形界面,只能从命令行开启。
1.启用 Apache/Web 共享
打开终端,运行启动 Apache 命令:
sudo apachectl start
随后要求输入系统密码,这个密码输入的过程中是看到不显示的,输入完成后直接回车即可
关闭命令:
sudo apachectl stop
重启命令:
sudo apachectl restart
查看 Apache 版本命令:
httpd -v
检查配置中时候存在问题:
sudo apachectl -t
10.9中集成了如下 Apache 版本
Server version: Apache/2.2.24 (Unix)
Server built: Aug 24 2013 21:10:43
启用 Apache 之后,你可以直接在浏览器中访问 http://localhost,如果出现”It works!”就表示运行正常。
2.Root 目录
启用 Apache 之后,你首先得知道网页文件应该放到哪个目录才能正常运行,相信有过 Linux 服务器配置经验的对此不会陌生。Mac OS X 中默认有两个目录可以直接运行你的 Web 程序,一个是系统级的 Web 根目录,一个是用户级的根目录,大家记下即可。
系统级的根目录是:
/Library/WebServer/Documents/
它对应的网址是:
http://localhost
用户级的根目录是:
~/Sites
这里需要注意的~/Sites 也就是你用户目录下面的”站点”目录,在 OS X 10.8/10.9 中,这个目录可能没有,所以你需要手动建立一个同名目录。建立方式很简单,直接在终端中运行:
sudo mkdir ~/Sites
建立”站点”文件夹之后,检查下面这个文件夹下面是不是有”你的用户名.conf”这个文件。
/etc/apache2/users/
如果没有,那么你需要创建一个,取名为”你的用户名.conf”,你可以使用 vi 或者 nano 这两种编辑器之一来创建。
sudo nano /etc/apache2/users/你的用户名.conf
创建之后将下面的这几行内容写到上面的 conf 文件中:
<Directory "/Users/username/Sites/">
Options Indexes MultiViews
AllowOverride All
Order allow,deny
Allow from all
</Directory>
(请将username改成你的用户名)
文件保存之后,给它赋予相应的权限:
sudo chmod 755 /etc/apache2/users/你的用户名.conf
最后运行
sudo chown root:wheel /etc/apache2/users/你的用户名.conf
完成后重启 Apache
此时CMD+SHIFT+A,找到你的用户名下的Sites,CMD+I,修改权限
最后将 /Library/WebServer/Documents/index.html.en 复制到 Sites 下面
接下来重启 Apache,以使该配置文件生效:
sudo apachectl restart
之后你就可以通过浏览器访问你的用户级目录网页了,你可以随便防个网页进去测试一下。根目录地址为:
http://localhost/~username/
(请将username改成你的用户名)
请将这句代码前面的#去掉,然后保存文件。
接下来再一次重启 Apache:
sudo apachectl restart
现在 PHP 应该已经开始工作了,你可以在用户级根目录下(~/Sites/)放一个PHP测试文件,代码如下:
<?php phpinfo(); ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment