xglys 发表于 2018-11-21 08:41:13

在mac中配置apache+php5.3+mysql的环境,修改hosts

  众所周知,mac中有自带的apache
  

  以下是关于apache的几个命令

  sudo apachectl start       开启
  sudo apachectl restart    重启
  sudo apachectl stop      停止

  sudo apachectl -v         查看本机apache版本
  

  以下是apache的配置文件的路径
  /etc/apache2/httpd.conf
  

  理论上,在终端输入 sudo apachectl start,输入密码。在浏览器中输入localhost,则能显示“It work!”字样。
  Apache的网站服务器根目录在/Library/WebServer/Documents。
  可以使用ln -s在桌面建立软链接~类似于windows中的快捷方式
  

  在Mac OS中一班已经有PHP了,只要添加apache对PHP的支持就行了

  编辑http.conf配置文件 sudo vim /etc/apache2/http.conf
  

  按 esc 可以进入vim的命令模式,输入:php5/s可以查找到有关php5的内容

  找到   #LoadModule php5_module libexec/apache2/libphp5.so,把前面的#去掉

  重启apache

  sudo apachectl restart

  

  在根目录中写一个phpinfo.php的文件,里面写一个phpinfo()函数,这样就能看见有关php的版本及相关数据。
  

  看到的版本是Max系统内置的,如果需要安装别的版本的PHP,可以使用brew命令
  

  
  首先在终端输入以下内容,来安装brew
  ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

  

  继续输入

  brew update
brew tap homebrew/dupes
brew tap homebrew/php
brew tap josegonzalez/homebrew-php
  

  安装PHP53
  brew install php53
  

  安装好的php53的配置文件在/usr/local/etc/php/5.3/php.ini

  
由于Mac自带了php和php-fpm,因此需要添加系统环境变量PATH来替代自带PHP版本:
  

  echo 'export PATH="$(brew --prefix homebrew/php/php53)/bin:$PATH"' >> ~/.bash_profile#for php
echo 'export PATH="$(brew --prefix homebrew/php/php53)/sbin:$PATH"' >> ~/.bash_profile#for php-fpm
echo 'export PATH="/usr/local/bin:/usr/local/sbin:$PATH"' >> ~/.bash_profile #for other brew install soft
source ~/.bash_profile#更新配置
  

  测试看看 php的版本
$ php -v
  

  $ php-fpm -v
  

  这时,如果使用phpinfo()函数显示,显示的版本不是php53,是因为apache配置文件中php模块的指向还是原来的那个,现在更改一下路径
  

  首先还是打开配置文件sudo vim /etc/apache2/http.conf
  esc 然后进入命令行模式,输入:/php5找到php模块,更改内容成为如下

  LoadModule php5_module    /usr/local/opt/php53/libexec/apache2/libphp5.so
  

  重启apache,然后phpinfo显示的内容就是5.3了
  

  接下来安装mysql,这个通过mysql官网就可以安装了 非常方便。
  

  接下来设置虚拟主机,使得输入一个虚拟的网址,可以访问根目录下的项目。
  

  在终端输入sudo vim /etc/apache2/httpd.conf    打开Apche的配置文件
  在httpd.conf中找到“#Include /private/etc/apache2/extra/httpd-vhosts.conf”,去掉前面的“#”,保存并退出。
  然后重启Apache
  输入sudo vi /etc/apache2/extra/httpd-vhosts.conf    打开了配置虚拟主机文件
  文件中有两个例子,可以都注释了

  

  然后添加

    DocumentRoot "/Library/WebServer/Documents"#根目录
    ServerName localhost
    ErrorLog "/private/var/log/apache2/localhost-error_log"
    CustomLog "/private/var/log/apache2/localhost-access_log" common



    DocumentRoot "/Library/WebServer/Documents/test"#路径改成你的项目文件,以test为例

    ServerName www.test.com   #虚拟域名,以www.test.com为例

    ErrorLog "/private/var/log/apache2/sites-error_log"
    CustomLog "/private/var/log/apache2/sites-access_log" common
   
                Options Indexes FollowSymLinks MultiViews
                AllowOverride None
                Order deny,allow
                Allow from all
      

  

  保存退出。
  

  然后修改hosts文件,windows的话在system32下

  输入sudo vi /etc/hosts”,打开hosts配置文件,加入127.0.0.1 www.test.com

  
保存 然后重启apache
  这样就能在浏览器输入 www.test.com 访问/Library/WebServer/Documents/test 下的项目了
  

  




页: [1]
查看完整版本: 在mac中配置apache+php5.3+mysql的环境,修改hosts