MAC系统中运行mysql+apache+php环境,方便快速搭

2019-07-23  本文已影响0人  deathearth

最近要尝试一些数据导入功能,是将获取的文本内容,导入到discuz系统库表中。由于ip动态变动原因,不适合直连线上库。所以本地搭建一套方便调试与测试。

根本没想到mac系统原来自带apache和php。按照以往使用windows的尿性,肯定要装一套lmnp/lmap的环境了。既然已经有了部分软件,剩下的就是配置了。程序猿感到很轻松啊

  1. 启动Apache

启动apache必须要用root账户,否则失败

deathearth:lamp-master chenhailong$  apachectl start
This operation requires root.
deathearth:lamp-master chenhailong$ sudo apachectl start
Password:
Sorry, try again.
Password:
Sorry, try again.
Password:
Sorry, try again.
sudo: 3 incorrect password attempts
deathearth:lamp-master chenhailong$

mac上切换root账户

su root 
这种方式,输入密码,死也登录不进去

sudo -i
这种方式可以正常切换用户

启动apache

启动Apache服务  sudo apachectl start
重启Apache服务  sudo apachectl restart
停止Apache服务  sudo apachectl stop
查看Apache服务  sudo apachectl -v

#启动后查看信息如下:
deathearth:some_tools root# sudo apachectl -v
Server version: Apache/2.4.16 (Unix)
Server built:   Jul 22 2015 21:03:09
  1. 配置php

找到 /etc/apache2目录下的httpd.conf文件,并找到以下位置
LoadModule php5_module libexec/apache2/libphp5.so去掉注释

#LoadModule asis_module libexec/apache2/mod_asis.so
#LoadModule info_module libexec/apache2/mod_info.so
#LoadModule cgi_module libexec/apache2/mod_cgi.so
#LoadModule dav_fs_module libexec/apache2/mod_dav_fs.so
#LoadModule dav_lock_module libexec/apache2/mod_dav_lock.so
#LoadModule vhost_alias_module libexec/apache2/mod_vhost_alias.so
LoadModule negotiation_module libexec/apache2/mod_negotiation.so
LoadModule dir_module libexec/apache2/mod_dir.so
#LoadModule imagemap_module libexec/apache2/mod_imagemap.so
#LoadModule actions_module libexec/apache2/mod_actions.so
#LoadModule speling_module libexec/apache2/mod_speling.so
#LoadModule userdir_module libexec/apache2/mod_userdir.so
LoadModule alias_module libexec/apache2/mod_alias.so
#LoadModule rewrite_module libexec/apache2/mod_rewrite.so
LoadModule php5_module libexec/apache2/libphp5.so
LoadModule hfs_apple_module libexec/apache2/mod_hfs_apple.so

这个文件中可以配置服务端口、错误日志等,很多。修改后记得重启apache服务

  1. 运行php程序

通过apache的配合文件可以看到,webServer的项目目录在
DocumentRoot "/Library/WebServer/Documents"

创建php文件如下

<?php
phpinfo()
?>

访问127.0.0.1时,出现的是It works。这是因为apache服务器默认的首页只配置了index.html,根据需要添加index.php,index.htm的默认地址

如果正常的话,会显示php的基本环境信息(我这里只摘取页面上部分信息)

PHP Version 5.5.38

System  Darwin deathearth.local 14.5.0 Darwin Kernel Version 14.5.0: Sun Jun 4 21:40:08 PDT 2017; root:xnu-2782.70.3~1/RELEASE_X86_64 x86_64
Build Date  Mar 30 2017 12:07:10
Configure Command   '/BinaryCache/apache_mod_php/apache_mod_php-102.1.7~1/Objects/php/configure' '--prefix=/usr' '--mandir=/usr/share/man' '--infodir=/usr/share/info' '--disable-dependency-tracking' '--sysconfdir=/private/etc' '--with-libdir=lib' '--enable-cli' '--with-iconv=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.10.Internal.sdk/usr' '--with-config-file-path=/etc' '--with-config-file-scan-dir=/Library/Server/Web/Config/php' '--with-libxml-dir=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.10.Internal.sdk/usr' '--with-openssl=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.10.Internal.sdk/usr/local
Server API  Apache 2.0 Handler
Virtual Directory Support   disabled
  1. mysql安装

我这里很早之前就安装过了,在目录 /usr/local/mysql/bin。运行以下信息,表示可以正常连接mysql

deathearth:bin root# ./mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 626
Server version: 5.7.9 MySQL Community Server (GPL)

Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> select @@version
    -> ;
+-----------+
| @@version |
+-----------+
| 5.7.9     |
+-----------+
1 row in set (0.00 sec)

  1. 测试php是否能连接mysql

修改 /Library/WebServer/Documents/index.php内容如下

<?php
header("content-type:text/html;charset=utf-8"); //设置中文编码
$servername = "127.0.0.1";
$username = "root";
$password = "123456";

// 创建连接
$conn = new mysqli($servername, $username, $password);

// 检测连接
if ($conn->connect_error) {
    die("连接失败: " . $conn->connect_error);
}
echo "连接成功";
?>

重新运行 http:/127.0.0.1/index.php,出现“连接成功”信息说明正常

到这里,php运行环境的搭建就完成了,开始装php三方系统吧,祝大家好运

上一篇下一篇

猜你喜欢

热点阅读