首页 > 编程知识 正文

osx上启用php,osx命令行

时间:2023-12-28 11:56:40 阅读:327535 作者:VRWP

本文目录一览:

mac怎么启动apache php环境

Apache与PHP的配置

Mavericks同以往的OSX一样自带了apache2.2.24和php5.4.17,但默认情况下没有开启,打开终端

sudo apachectl start

这时在浏览器中输入localhost应该就会出现apache标准的It Works!

下面开启php,修改apache配置文件

sudo vim /etc/apache2/httpd.conf

找到

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

默认的www文件夹非常不方便,还是改成在home里开发起来比较顺手,修改apache配置

,还是上面的文件

#将引号中的目录修改为自己的目录

DocumentRoot "/Library/WebServer/Documents"

......

#将引号中的目录修改为和上面一样的目录

Directory "/Library/WebServer/Documents"

在自己的目录中创建文件index.php,写入

?php phpinfo(); ?

在浏览器中访问localhost,应该显示出php的info页

osx 怎么打开php目录

1、如果你用过smarty模板引擎、MVC类似框架如tp,那么你自己写一个框架噢!

2、你要对php的继承实现以及其中的小知识点熟知;

3、先列出你的框架目录,什么地方放什么,然后写你的配置文件

4、用到的高频函数有:require 、 require_once 、 file_exites等!

如:sybase_connect连上数据库。

语法: int sybase_connect(string [servername], string [username], string [password]);

返回值: 整数函数种类: 数据库功能 本函数用来打开与 Sybase 数据库的连接。

参数 servername 为欲连上的数据库服务器名称。

参数 username 及 password 可省略,分别为连接使用的帐号及密码。

使用本函数需注意早点关闭数据库,以减少系统的负担。

连接成功则返回数据库的连接代号,失败返回 false 值。

如何在Macbook Pro搭建PHP开发环境

先介绍几个命令

// 启动Apache服务

sudo apachectl start

// 重启Apache服务

sudo apachectl restart

// 停止Apache服务

sudo apachectl stop

// 查看Apache版本

httpd -v

Mac OS自带Apache,只需要启动Apache就行。

打开终端,输入命令:sudo apachectl start

打开浏览器,在地址栏中输入localhost,出现It Works字符串,就说明Apache已经成功启动

在Macbook pro下,Apache的网站服务器根目录在/Library/WebServer/Documents路径下

配置PHP

Mac OS 同样自带PHP,只需要在Apache的配置文件中添加Apache对PHP的支持就好了

在终端中输入命令:

sudo vim /etc/apache2/httpd.conf

打开httpd.conf文件

去掉红框标注内容的注释符号

LoadModule php5_module libexec/apache2/libphp5.so

然后保存

此处暂不介绍如何使用VIM编辑内容

重启Apache服务

进入/Library/WebServer/Documents,

在该目录下新建一个测试的PHP页面,输入命令:sudo vim test.php

在test.php中输入以下代码

?php phpinfo(); ?

在浏览器中打开页面localhost/test.php,测试PHP是否可用

mac下如何配置PHP apache?

Mac OS X 内置了Apache 和 PHP

运行“sudo apachectl start”,再输入帐号密码,这样Apache就运行了。

运行“sudo apachectl -v”,你会看到Mac OS X 10.6.3中的Apache版本号

测试是否开启Apache成功:在浏览器中输入“”,就可以看到出现一个内容为“It works!”的页面,它位于“/Library(资源库)/WebServer/Documents/”下,这是Apache的默认根目录。

注意:开启了Apache就是开启了“Web共享”,这时联网的用户就会通过“http://[本地IP]/”来访问“/Library(资源库)

/WebServer/Documents/”目录,通过“http://[本地IP]/~[用户名]”来访问“/Users/[用户名]/Sites

/”目录,可以通过设置“系统偏好设置”的“安全(Security)”中的“防火墙(Firewall)”来禁止这种访问。

运行PHP

(网上大多数操作出下)

在终端中运行“sudo vi

/etc/apache2/httpd.conf”,打开Apache的配置文件。(如果不习惯操作终端和vi的可以设置在Finder中显示所有的系统

隐藏文件,记得需要重启Finder,这样就可以找到对应文件,随心所欲编辑了,但需要注意的是某些文件的修改需要开启root帐号,但整体上还是在终端

上使用sudo来临时获取root权限比较安全。)

找到“#LoadModule php5_module libexec/apache2/libphp5.so”,把前面的#号去掉,保存(在命令行输入:w)并退出vi(在命令行输入:q)。

运行“sudo cp /etc/php.ini.default /etc/php.ini”,这样就可以通过php.ini来配置各种PHP功能了。比如:

;通过下面两项来调整PHP提交文件的最大值,比如phpMyAdmin中导入数据的最大值

upload_max_filesize = 2M

post_max_size = 8M

;比如通过display_errors来控制是否显示PHP程序的报错

display_errors = Off

运行“sudo apachectl restart”,重启Apache,这样PHP就可以用了。

运行“cp /Library/WebServer/Documents/index.html.en

/Library/WebServer/Documents/info.php”,即在Apache的根目录下复制index.html.en文件并重命

名为info.php

在终端中运行“vi

/Library/WebServer/Document/info.php”,这样就可以在vi中编辑info.php文件了。在“It’s

works!”后面加上“?php phpinfo();

?”,然后保存之。这样就可以在中看到有关PHP的信息,比如版本号是5.3.1。

如果用终端不方便的话(我就是不方便的),可以直接到定位的文件夹,把对应的文件复制出来,修改完再覆盖回去,下面操作就相同了

如何在 OSX 上架設 Nginx+MariaDB+PHP 伺服器

打开「Mac App Store」下载并安装「Xcode」。

打开「Xcode」,按下同意并接受Xcode的使用条款。

打开「终端机(Terminal)」,输入下面的指令并且安装Homebrew。

ruby -e "$(curl -fsSL )"

输入下面的指令让其他程式知道Xcode的位置。

sudo xcode-select -switch /Applications/Xcode.app/Contents/Developer

之後,「按此」下载最新版本的X11并且安装。

之後建立X11的符号链接(软链接)。

sudo ln -s /opt/X11 /usr/X11

之後使用下面的指令查看透过Homebrew安装的套件。

brew list

之後输入下面指令查看当前系统变数。

$PATH

打开「.bash_profile」并修改系统变数。

vim ~/.bash_profile

将「/usr/local/bin」放在「/usr/local/sbin」之前。例如︰

export PATH=~/bin:/usr/local/bin:/usr/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/X11/bin:/usr/local/git/bin:/opt/local/bin

接着,请参考文章「[教学] 在 OSX 10.8 上设定 Postfix 邮件传送代理 (MTA)」设定Postfix。

启用Postfix。

sudo postfix start

安装 dnsmasq 设定 DNS

透过Homebrew安装dnsmasq,以加速网路存取速度。

brew install dnsmasq

复制和编辑dnsmasq.conf。

mkdir /usr/local/etc

cp $(brew --prefix dnsmasq)/dnsmasq.conf.example /usr/local/etc/dnsmasq.conf

vim /usr/local/etc/dnsmasq.conf

更改dnsmasq.conf内的数值。

resolv-file=/etc/resolv.dnsmasq.conf

address=/.ld/127.0.0.1

listen-address=127.0.0.1

建立并编辑DNS解析文件。

sudo vim /etc/resolv.dnsmasq.conf

在「resolv.dnsmasq.conf」文件内贴上以下内容。

# Google DNS IPv6:

nameserver 2001:4860:4860::8888

nameserver 2001:4860:4860::8844

# OpenDNS IPv6:

nameserver 2620:0:ccd::2

nameserver 2620:0:ccc::2

# Google DNS:

nameserver 8.8.8.8

nameserver 8.8.4.4

# OpenDNS:

nameserver 208.67.222.222

nameserver 208.67.220.220

设定开机自动执行dnsmasq。

sudo cp $(brew --prefix dnsmasq)/homebrew.mxcl.dnsmasq.plist /Library/LaunchDaemons

sudo launchctl load -w /Library/LaunchDaemons/homebrew.mxcl.dnsmasq.plist

设定你的Hostname。

sudo scutil --set HostName foolegg

打开「系统设定」,在「网络」内分别选择「Wi-fi」和「Ethernet」,在「进阶」内设定「127.0.0.1」为DNS伺服器。

安装 Nginx

输入下面指令关闭自动启用Apache。

sudo launchctl unload -w /System/Library/LaunchDaemons/org.apache.httpd.plist

之後透过Homebrew安装Nginx。

brew install nginx

安装完成後,复制和备份nginx.conf。

cp /usr/local/etc/nginx/nginx.conf /usr/local/etc/nginx/nginx.conf.bak

打开nginx.conf,删除所有内容,使用下面的内容取代。

# Nginx web server main configuration file nginx.conf

#

user www-data staff;

worker_processes 4;

worker_rlimit_nofile 8192;

error_log /usr/local/var/log/nginx/error.log;

#pid /var/run/nginx.pid;

events {

worker_connections 1024;

}

http {

include mime.types;

default_type application/octet-stream;

sendfile on;

keepalive_timeout 10;

tcp_nodelay on;

gzip on;

client_max_body_size 100M;

#access_log /usr/local/var/log/nginx/access.log main;

log_format main '$remote_addr - $remote_user [$time_local] "$request" '

'$status $body_bytes_sent "$http_referer" '

'"$http_user_agent" "$http_x_forwarded_for"';

## FastCGI.

include /usr/local/etc/nginx/fastcgi.conf;

## For the filefield_nginx_progress module to work. From the

## README. Reserve 1MB under the name 'uploads' to track uploads.

#upload_progress uploads 1m;

#include /etc/nginx/conf.d/*.conf;

#include /usr/local/etc/nginx/aegir.conf;

server {

listen 80;

server_name localhost;

server_tokens off;

#access_log /usr/local/var/log/nginx/access.log main;

root /usr/share/nginx/www/public_html;

location / {

root /usr/share/nginx/www/public_html;

index index.html index.htm;

##### Use this if you're going to install wordpress #####

#if (-f $request_filename/index.html) {

# rewrite (.*) $1/index.html break;

#}

#if (-f $request_filename/index.php) {

# rewrite (.*) $1/index.php;

#}

#if (-f $request_filename) {

# rewrite (.*) /index.php;

#}

#if (!-e $request_filename) {

# rewrite ^.+?(/wp-.*) $1 last;

# rewrite ^.+?(/.*.php)$ $1 last;

# rewrite ^ /index.php last;

#}

#rewrite /wp-admin$ $scheme://$host$uri/ permanent;

##### End #####

}

error_page 500 502 503 504 /50x.html;

location = /50x.html {

root /usr/share/nginx/www/public_html;

}

location ~ .php$ {

fastcgi_pass 127.0.0.1:9000;

fastcgi_index index.php;

fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

include fastcgi_params;

}

}

}

之後建立Nginx的log资料夹和其它必要的目录。

sudo mkdir -p /usr/local/var/log/nginx/

touch /usr/local/var/log/nginx/error.log

touch /usr/local/var/log/nginx/access.log

sudo mkdir -p /usr/share/nginx/www/public_html

安装 MariaDB

透过Homebrew安装MariaDB。

brew install mariadb --use-llvm --env=std

取消TMPDIR设定。

unset TMPDIR

初始化MySQL数据库。请将5.5.30更改为你当前使用的版本编号。

cd /usr/local/Cellar/mariadb/5.5.30/scripts

mysql_install_db --user=`whoami` --basedir="$(brew --prefix mariadb)" --datadir=/usr/local/var/mysql --tmpdir=/tmp

系统会提示错误,请忽略有关错误。下面的教学设定会更正错误。

安装 PHP

虽然OSX上已经安装了PHP,但是我们不会使用Apple的PHP。

输入下面的指令安装PHP。

brew tap josegonzalez/homebrew-php

brew tap homebrew/dupes

brew install php53 --with-mysql --with-fpm --with-imap

brew install php53-xhprof

brew install php53-xdebug

brew install php53-uploadprogress

brew install php53-memcached

brew install php53-imagick

打开并修改php.ini。

vim /usr/local/etc/php/5.3/php.ini

搜索

extension=php_zip.dll

在下面加上以下内容,请更改版本编号。

extension="/usr/local/Cellar/php53-xhprof/0.9.2/xhprof.so"

extension="/usr/local/Cellar/php53-uploadprogress/1.0.3.1/uploadprogress.so"

extension="/usr/local/Cellar/php53-memcached/2.1.0/memcached.so"

extension="/usr/local/Cellar/php55-imagick/3.1.0RC2/imagick.so"

zend_extension="/usr/local/Cellar/php53-xdebug/2.2.1/xdebug.so"

到「PHP手册」寻找你身处地区的时区。修改php.ini内的「date.timezone」变数的数值。

date.timezone = Asia/Hong_Kong

寻找以下3个变数的数值。如果找不到,请在档案的最尾部份加上3个变数和数值。

magic_quotes_gpc = Off

magic_quotes_runtime = Off

magic_quotes_sybase = Off

修改记忆体和上传限制的数值。如果变量前出现分号「;」,请将之删除。

memory_limit = 256M

post_max_size = 100M

upload_max_filesize = 100M

打开并修改php-fpm.conf档案。

vim /usr/local/etc/php/5.3/php-fpm.conf

搜索

pid = run/php-fpm.pid

在下面加上

pid = /usr/local/var/run/php-fpm.pid

之後删除下面4行变数和数值前的分号「;」。

pm.start_servers = 3

pm.min_spare_servers = 3

pm.max_spare_servers = 5

pm.max_requests = 500

按照下面修改「error_log」的数值。

error_log = /usr/local/var/log/php-fpm.log

之後建立log的符号链接(软链接)。

sudo ln -s $(brew --prefix josegonzalez/php/php53)/var/log/php-fpm.log /usr/local/var/log/php-fpm.log

开机启用服务

设定Nginx的启用服务。

sudo cp $(brew --prefix nginx)/homebrew.mxcl.nginx.plist /Library/LaunchDaemons/

sudo chown root:wheel /Library/LaunchDaemons/homebrew.mxcl.nginx.plist

打开并修改「homebrew.mxcl.nginx.plist」。

sudo vim /Library/LaunchDaemons/homebrew.mxcl.nginx.plist

在「homebrew.mxcl.nginx.plist」内删除以下内容。

keyKeepAlive/key

true/

keyUserName/key

string[YourUserName]/string

开启Nginx。

launchctl load -w /Library/LaunchDaemons/homebrew.mxcl.nginx.plist

为软件建立资料夹。

mkdir -p ~/Library/LaunchAgents

设定MariaDB的启用服务。

cp $(brew --prefix mariadb)/homebrew.mxcl.mariadb.plist ~/Library/LaunchAgents/

launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.mariadb.plist

设定PHP的启用服务。

cp $(brew --prefix josegonzalez/php/php53)/homebrew-php.josegonzalez.php53.plist ~/Library/LaunchAgents/

launchctl load -w ~/Library/LaunchAgents/homebrew-php.josegonzalez.php53.plist

完成MariaDB的设定。

sudo $(brew --prefix mariadb)/bin/mysql_secure_installation

为MySQL设定密码。

Enter current password for root (enter for none): [Enter]

Set root password? [Y/n] y

New password: [password]

Re-enter new password: [password]

Remove anonymous users? [Y/n] y

Disallow root login remotely? [Y/n] y

Remove test database and access to it? [Y/n] y

Reload privilege tables now? [Y/n] y

测试PHP

建立并修改index.php。

vim /usr/share/nginx/www/public_html/index.php

输入并储存以下内容。

?php phpinfo(); ?

从新启动Nginx。

sudo nginx -s reload

打开浏览器,输入「」查看是否成功运行PHP。

启动或停止MNMP

在桌面或其他位置建立Shell Script「mnmp.sh」。

vim mnmp.sh

在Shell Script「mnmp.sh」内加入以下内容,然後储存档案。

#!/bin/bash

case "$1" in

start)

# Start MariaDB

echo -e "Starting mariadb..."

launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.mariadb.plist

# Start PHP

echo -e "Starting php..."

launchctl load -w ~/Library/LaunchAgents/homebrew-php.josegonzalez.php53.plist

# Start Nginx

echo -e "Starting nginx..."

sudo launchctl load -w /Library/LaunchDaemons/homebrew.mxcl.nginx.plist

;;

stop)

# Stop MariaDB

echo -e "Stopping mariadb..."

launchctl unload -w ~/Library/LaunchAgents/homebrew.mxcl.mariadb.plist

# Stop PHP

echo -e "Stopping php..."

launchctl unload -w ~/Library/LaunchAgents/homebrew-php.josegonzalez.php53.plist

# Stop Nginx

echo -e "Stopping nginx..."

sudo launchctl unload -w /Library/LaunchDaemons/homebrew.mxcl.nginx.plist

;;

esac

exit 0

为Shell Script「mnmp.sh」加入可执行权限。

chmod 700 mnmp.sh

使用以下指令启动MNMP。

./mnmp.sh start

使用以下指令停止MNMP。

./mnmp.sh stop

PHP档案存取错误

有部份读者反映在存取PHP档案时出现错误。大家可以打开档案「/usr/local/var/log/nginx/error.log」查看错误的原因。

如果出现下面的原因,可能是因为php-fpm的问题。

[error] NUMBER#0: *NUMBER kevent() reported that connect() fa iled (NUMBER: Connection refused) while connecting to upstream, client: 127.0.0. 1, server: localhost, request: “GET /index.php HTTP/1.1″, upstream: “fastcgi ://127.0.0.1:9000″, host: “127.0.0.1″

大家可以使用「netstat -anp tcp | grep 9000」或者「lsof -i tcp:9000」查看占用Port 9000的进程,并且将其终止。之後使用下面指令开启php-fpm。

sudo /usr/local/sbin/php-fpm --fpm-config /usr/local/etc/php/5.3/php-fpm.conf

请再次测试是否成功存取php。如果成功,请依照下面设定自动启动php-fpm。

设定自动启动php-fpm

建立文件「~/Library/LaunchAgents/org.php-fpm.plist」。

mac 下怎么配置php开发环境

homestead是laravel提供给开发者的一个vagrant环境,当然用来做其他php框架或者原生php代码的服务器都很适合,与本机环境无关,可以和virtualhost 共享目录,包括nginx,redis,mysql,memcache等服务都帮你装好了。

以目前来看,还是用vagrant或者docker之类的架设服务最好,在mac上虽然自带apache和php,但每次mac osx升级都会自动升级php和apache,听起来很好是不是,但实际上每次都是噩梦,我在本机php安装了一些额外模块,升级的时候全给抹了,php.ini里的设置全都还原了(当然升级操作系统前的php.ini系统帮你改名备份了),apache的设置也还原了,于是又要把所有的用到的php扩展安装一次。

而且在本机上自己配环境不利于团队开发,每个人的开发环境php、mysql等版本都有细微差别,包括操作系统linux下和windows下的mysql版本不同处理的结果也有细微不同,我就遇到过很多次这样的坑,所以还是采用vagrant的虚拟host吧,只要配置好一份环境,可以把vhost当成vagrant的box导出,分发给团队里的其他人,这样所有人都保持在一个环境下工作,无论他本机是mac还是windows。这样甚至好过在公司里架设一个开发服务器,每个人互不影响,每个环境都是一致而且独立的,不在办公室办公的时候也不依赖于内网的开发服务器才能干活

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。