MacBook搭建服务器环境

来源:互联网 发布:linux arch 编辑:程序博客网 时间:2024/05/23 23:08

1.运行Apache
Mac OS 系统自带Apache、Nginx和PHP。在终端根目录输入如下命令开启Apache:

开启Apachesudo apachectl start关闭Apachesudo apachectl stop重启Apachesudo apachectl restart查看Apache版本sudo apachectl -v

开启后,可以通过浏览器访问:http://localhost,页面显示“It works” 表示已经成功。

2.备份文件

sudo cp /etc/apache2/httpd.conf /etc/apache2/httpd.conf.baksudo cp etc/apache2/extra/httpd-vhosts.conf etc/apache2/extra/httpd-vhosts.conf.bak

3.配置PHP

编辑Apache的配置文件,终端输入:

sudo vim /etc/apache2/httpd.conf

在文件里找到 #LoadModule php5_module libexec/apache2/libphp5.so 这一行,然后去掉前面的“#”号注释,输入 :wq 保存退出

重启Apache,使其生效

sudo apachectl restart

4.查看本地服务器路径
此时服务器已经配置成功,目录在/Library/WebServer/Documents下,在终端输入以下命令进入:

cd /cd /Library/WebServer/Documentsls

文件index.html.en就是访问http://localhost所看到的页面。

通过finder的前往文件夹命令,输入/Library/WebServer/Documents可直接看到本地服务器中的文件。

5.修改本地服务器路径

先创建一个文件夹作为要修改的路径

sudo mkdir ~/sites

然后编辑配置文件

sudo vim etc/apache2/httpd.conf

输入/DocumentRoot查找服务器路径,按下 i 进入编辑模式
可以看到有两个路径/Library/WebServer/Documents 把他们都改成刚才建的那个sites 文件夹的路径 ,重启服务器

6.打开Apache目录浏览功能

sudo /etc/apache2/extra/httpd-vhosts.conf //在<VirtualHost *:80>虚拟目录里设置以下内容 <Directory "/Users/lax/site">                Options Indexes FollowSymLinks MultiViews                AllowOverride None                Require all granted    </Directory>
sudo vim /etc/apache2/httpd.confOptions FollowSymLinks MultiviewsAllowOverride NoneOptions Indexes  FollowSymLinks MultiviewsAllowOverride None