Mac下 NMP(nginx,mysql,php)环境搭建

时间:2022-02-03 19:53:11

Mac下 NMP(nginx,mysql,php)环境搭建

<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);"><span style="white-space:pre"></span>今天尝试在Mac环境下搭建NMP环境,其中遇到了些问题,做一下纪录以备后用。</span>

Mac下默认安装了apache,可以关掉它

sudo apachectl stop

1、首先安装homebrew

安装成功后,brew命令应该在/usr/local/bin/下,如果cmd中输入brew提示“cmd not found”

那么安装完成后需要配置下环境变量

如果没错的话,应该在/etc下有个profile文件,加上这个路径就可以了

#homebrew
25 export PATH=$PATH:/usr/local/bin

2、安装nginx

brew install nginx
sudo nginx

可以启动Linux

nginx默认监听端口为8080

这样子在浏览器中需要输入

localhost:8080

才可以出现成功界面

如果不希望输入端口的话可以改一下端口

$vi /usr/local/etc/nginx/nginx.conf
server { 36         listen       80; 37         server_name  localhost;

Mac下 NMP(nginx,mysql,php)环境搭建

这个成功页面的文件地址一般是在/usr/local/var/www/下

3、安装mysql

brew install mysql
用homebrew安装的话mysql应该是在

/usr/local/Cellar/mysql/*.*.**
文件夹下,*.*.**因具体版本号而不同

当然也可以通过

brew info mysql
看下

4、启动mysql服务

如果直接在cmd中输入mysql后会提示

ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock'
首先要启动mysql服务

mysql.server start
然后在

/etc/profile

文件中添加path

#mysql
20 alias mysql=/usr/local/Cellar/mysql/5.6.24/bin/mysql
21 alias mysqladmin=/usr/local/Cellar/mysql/5.6.24/bin/mysqladmin

5、配置php环境

Mac下默认安装了php,但是在/usr/local/var/www/下创建一个php文件,在浏览器中还是显示不出来的,所以要配置一下php与nginx的环境

locate php.ini
找到php的配置文件,把 ; cgi.fix_pathinfo= 1前面的注释号“;”去掉

; to use SCRIPT_FILENAME rather than PATH_TRANSLATED.
; http://php.net/cgi.fix-pathinfo
cgi.fix_pathinfo=1
然后再找到nginx的配置文件

#user  nobody;
worker_processes 1;

#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;

#pid logs/nginx.pid;


events {
worker_connections 1024;
}


http {
include mime.types;
default_type application/octet-stream;

#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';

#access_log logs/access.log main;

sendfile on;
#tcp_nopush on;

#keepalive_timeout 0;
keepalive_timeout 65;

#gzip on;

server {
listen 80;
server_name localhost;

#charset koi8-r;

#access_log logs/host.access.log main;

location / {
root /usr/local/var/www;
index index.html index.htm index.php;
}

#error_page 404 /404.html;

# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}

#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/local/var/www/$fastcgi_script_name;
include fastcgi_params;
}

# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}


# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;

# location / {
# root html;
# index index.html index.htm;
# }
#}


# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;

# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;

# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;

# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;

# location / {
# root html;
# index index.html index.htm;
# }
#}

}
include servers/*;
根据上面的改一下就好,注意其中的路径要与自己的一致
此时php文件也可以再浏览器中显示了

Mac下 NMP(nginx,mysql,php)环境搭建