osx上啟用php,osx命令行

本文目錄一覽:

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。這樣甚至好過在公司里架設一個開發伺服器,每個人互不影響,每個環境都是一致而且獨立的,不在辦公室辦公的時候也不依賴於內網的開發伺服器才能幹活

原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/159247.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
小藍的頭像小藍
上一篇 2024-11-19 18:58
下一篇 2024-11-19 18:58

相關推薦

  • PHP和Python哪個好找工作?

    PHP和Python都是非常流行的編程語言,它們被廣泛應用於不同領域的開發中。但是,在考慮擇業方向的時候,很多人都會有一個問題:PHP和Python哪個好找工作?這篇文章將從多個方…

    編程 2025-04-29
  • PHP怎麼接幣

    想要在自己的網站或應用中接受比特幣等加密貨幣的支付,就需要對該加密貨幣擁有一定的了解,並使用對應的API進行開發。本文將從多個方面詳細闡述如何使用PHP接受加密貨幣的支付。 一、環…

    編程 2025-04-29
  • 使用PHP foreach遍歷有相同屬性的值

    本篇文章將介紹如何使用PHP foreach遍歷具有相同屬性的值,並給出相應的代碼示例。 一、基礎概念 在講解如何使用PHP foreach遍歷有相同屬性的值之前,我們需要先了解幾…

    編程 2025-04-28
  • PHP獲取301跳轉後的地址

    本文將為大家介紹如何使用PHP獲取301跳轉後的地址。301重定向是什麼呢?當我們訪問一個網頁A,但是它已經被遷移到了另一個地址B,此時若伺服器端做了301重定向,那麼你的瀏覽器在…

    編程 2025-04-27
  • Python命令行pip3 install語法錯誤

    對於Python編程開發者而言,安裝和使用第三方模塊是非常頻繁的操作。而pip3作為Python官方推薦的模塊管理工具,具備包括搜索、安裝、卸載和升級等眾多功能,是大家較為常用的方…

    編程 2025-04-27
  • PHP登錄頁面代碼實現

    本文將從多個方面詳細闡述如何使用PHP編寫一個簡單的登錄頁面。 1. PHP登錄頁面基本架構 在PHP登錄頁面中,需要包含HTML表單,用戶在表單中輸入賬號密碼等信息,提交表單後服…

    編程 2025-04-27
  • PHP與Python的比較

    本文將會對PHP與Python進行比較和對比分析,包括語法特性、優缺點等方面。幫助讀者更好地理解和使用這兩種語言。 一、語法特性 PHP語法特性: <?php // 簡單的P…

    編程 2025-04-27
  • SOXER: 提供全面的音頻處理功能的命令行工具

    SOXER是一個命令行工具,提供了強大、靈活、全面的音頻處理功能。同時,SOXER也是一個跨平台的工具,支持在多個操作系統下使用。在本文中,我們將深入了解SOXER這個工具,並探討…

    編程 2025-04-27
  • PHP版本管理工具phpenv詳解

    在PHP項目開發過程中,我們可能需要用到不同版本的PHP環境來試驗不同的功能或避免不同版本的兼容性問題。或者我們需要在同一台伺服器上同時運行多個不同版本的PHP語言。但是每次手動安…

    編程 2025-04-24
  • Windows命令行全面解析

    一、基本概念 1、什麼是命令行? 命令行是操作系統提供的一種交互方式,用戶可以通過輸入特定的命令和參數來完成系統操作,例如創建文件、執行程序等。 2、為什麼需要使用命令行? 雖然現…

    編程 2025-04-24

發表回復

登錄後才能評論