Mac OS上搭建LNMP开发环境

1. 概述

LNMP代表的就是:Linux系统下Nginx+MySQL+PHP这种网站服务器架构。Linux是一类Unix计算机操作系统的统称,是目前最流行的免费操作系统。代表版本有:debian、centos、ubuntu、fedora、gentoo等。Nginx是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP代理服务器。Mysql是一个小型关系型数据库管理系统。PHP是一种在服务器端执行的嵌入HTML文档的脚本语言。这四种软件均为免费开源软件,组合到一起,成为一个免费、高效、扩展性强的网站服务系统。

2. 安装Homebrew

使用Mac的程序员必不可少的一步便是安装Homebrew,他就像是centOS的yum命令和ubuntu的apt-get命令一样,通过brew命令,我们可以快速的安装一些软件包。 使用命令行安装Homebrew的命令如下:

ruby -e "$(curl -fsSL https://raw.github.com/mxcl/homebrew/go)"

使用命令检查是否存在冲突:

brew doctor

然后使用使用命令对brew进行升级:

brew update && brew upgrade

3. 安装nginx

nginx在Mac OS中可以直接使用brew命令进行安装:

brew install nginx

如果需要使用80端口的话,需要将nginx加入root组当中:

sudo cp -v /usr/local/opt/nginx/*.plist /Library/LaunchDaemons/
sudo chown root:wheel /Library/LaunchDaemons/homebrew.mxcl.nginx.plist

然后使用命令启动nginx服务:

sudo nginx

测试nginx是否安装成功,因为默认配置文件监听的是8080端口,所以先对8080端口发起请求:

curl -IL http://127.0.0.1:8080

结果应该类似于下:

HTTP/1.1  OK
Server: nginx/1.9.
Date: Fri, May :: GMT
Content-Type: text/html
Content-Length:
Last-Modified: Fri, May :: GMT
Connection: keep-alive
ETag: "5444dea7-264"
Accept-Ranges: bytes

nginx的相关操作如下:

sudo nginx //启动nginx
sudo nginx -s reload|reopen|quit //重新加载|重启|退出

4. 安装php-fpm

Mac 自带 php-fpm,在终端执行命令:

php-fpm

会报如下错误:

ERROR: failed to open configuration file '/private/etc/php-fpm.conf': No such file or directory ()
ERROR: failed to load configuration file '/private/etc/php-fpm.conf'
ERROR: FPM initialization failed

错误信息显示,不能打开配置文件,cd /private/etc,发现没有 php-fpm.conf 文件,但是有 php-fpm.conf.default 文件。这个文件是默认配置,我们可以复制一份,改名为 php-fpm.conf,然后再根据需要改动配置。

cp /private/etc/php-fpm.conf.default /private/etc/php-fpm.conf

执行 php-fpm,再次报错:

ERROR: failed to open error_log (/usr/var/log/php-fpm.log): No such file or directory ()
ERROR: failed to post process the configuration
ERROR: FPM initialization failed

错误信息显示,不能打开错误日志文件。cd /usr/var/log 发现根本没有这个目录,甚至连 var 目录都没有,加上为了避免权限问题,干脆配置到 /usr/local/var/log 目录。

修改 php-fpm.conf error_log 配置为 /usr/local/var/log/php-fpm.log,并把 user 和 group 改为和当前用户一样。

执行 php-fpm,再次报错:

NOTICE: [pool www] 'user' directive is ignored when FPM is not running as root
NOTICE: [pool www] 'group' directive is ignored when FPM is not running as root

于是 sudo php-fpm,再次报错:

ERROR: unable to bind listening socket for address '127.0.0.1:9000': Address already in use ()
ERROR: FPM initialization failed

编辑 www.conf,修改 listen 为 127.0.0.1:9999。

sudo vim /private/etc/php-fpm.d/www.conf

最后开启php-fpm:

sudo php-fpm -D
#启动成功

5. 安装MySQL

MySQL也可以使用brew命令直接进行安装:

brew install mysql

同样,可以设置MySQL的开机自启动:

ln -sfv /usr/local/opt/mysql/*.plist ~/Library/LaunchAgents
launchctl load ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist

然后执行:

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

启动 MySQL:

mysql.server start

然后进行MySQL的安全安装,使用以下命令,可以更改root密码、删除匿名用户、关闭远程连接等:

mysql_secure_installation

然后会输出以下内容:

> Enter current password for root (enter for none):     //默认没有密码,直接回车即可
> Change the root password? [Y/n] //是否更改root密码,选择是,然后输入并确认密码
> Remove anonymous users? [Y/n] //是否删除匿名用户,选择是
> Disallow root login remotely? [Y/n] //是否禁止远程登录,选择是
> Remove test database and access to it? [Y/n] //是否删除test数据库,选择是
> Reload privilege tables now? [Y/n] //是否重载表格数据,选择是

测试数据库是否安装成功:

mysql -u root -p

然后输入刚才设置的root密码,将会输出以下内容:

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

mysql> exit         //输入exit退出数据库

设置字符集:

SET NAMES 'utf8';  

# 上面这一条就相当于下面的三句指令:
# SET character_set_client = utf8;
# SET character_set_results = utf8;
# SET character_set_connection = utf8;

MySQL7.0版本以上至少要8位及以上的密码,如果想设置123456怎么办?

先登录到mysql的命令界面,修改validate_password_policy参数的值:

mysql> set global validate_password_policy=0;
Query OK, 0 rows affected (0.00 sec)

这样,判断密码的标准就基于密码的长度了。这个由validate_password_length参数来决定。

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

validate_password_length参数默认为8,它有最小值的限制,最小值为:

validate_password_number_count
+ validate_password_special_char_count
+ (2 * validate_password_mixed_case_count)

其中,validate_password_number_count指定了密码中数据的长度,validate_password_special_char_count指定了密码中特殊字符的长度,validate_password_mixed_case_count指定了密码中大小字母的长度。

这些参数,默认值均为1,所以validate_password_length最小值为4,如果你显性指定validate_password_length的值小于4,尽管不会报错,但validate_password_length的值将设为4。如下所示:

mysql> select @@validate_password_length;
+----------------------------+
| @@validate_password_length |
+----------------------------+
| 8 |
+----------------------------+
1 row in set (0.00 sec) mysql> set global validate_password_length=1;
Query OK, 0 rows affected (0.00 sec) mysql> select @@validate_password_length;
+----------------------------+
| @@validate_password_length |
+----------------------------+
| 4 |
+----------------------------+
1 row in set (0.00 sec)

如果修改了validate_password_number_count,validate_password_special_char_count,validate_password_mixed_case_count中任何一个值,则validate_password_length将进行动态修改。

mysql> select @@validate_password_length;
+----------------------------+
| @@validate_password_length |
+----------------------------+
| 4 |
+----------------------------+
1 row in set (0.00 sec) mysql> select @@validate_password_mixed_case_count;
+--------------------------------------+
| @@validate_password_mixed_case_count |
+--------------------------------------+
| 1 |
+--------------------------------------+
1 row in set (0.00 sec) mysql> set global validate_password_mixed_case_count=2;
Query OK, 0 rows affected (0.00 sec) mysql> select @@validate_password_mixed_case_count;
+--------------------------------------+
| @@validate_password_mixed_case_count |
+--------------------------------------+
| 2 |
+--------------------------------------+
1 row in set (0.00 sec) mysql> select @@validate_password_length;
+----------------------------+
| @@validate_password_length |
+----------------------------+
| 6 |
+----------------------------+
1 row in set (0.00 sec)

最后,登录mysql修改密码:

MySQL -u root -p
  mysql> SET PASSWORD FOR 'root'@'localhost' = PASSWORD('');

6. 配置nginx

首先,为我们的配置文件创建一些文件夹,这些是仿照ubuntu的nginx结构进行建立的目录:

mkdir -p /usr/local/etc/nginx/logs
mkdir -p /usr/local/etc/nginx/sites-available
mkdir -p /usr/local/etc/nginx/sites-enabledmkdir -p /usr/local/etc/nginx/ssl sudo mkdir -p /var/www
sudo chown :staff /var/www
sudo chmod /var/www

然后修改nginx配置文件:

vim /usr/local/etc/nginx/nginx.conf

将内容替换为:

worker_processes  ;

error_log  /usr/local/etc/nginx/logs/error.log debug;

events {
worker_connections ;
} http {
include mime.types;
#default_type application/octet-stream;
default_type text/html; 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 /usr/local/etc/nginx/logs/access.log main; sendfile on; keepalive_timeout ; include /usr/local/etc/nginx/sites-enabled/*.conf;
}

然后加入默认站点配置文件:

vim /usr/local/ect/nginx/sites-enabled/default.conf

输入以下内容:

server {
listen ;
server_name localhost;
root /Applications/MAMP/htdocs/www/default/; access_log /usr/local/etc/nginx/logs/default.access.log main; location / {
root /Applications/MAMP/htdocs/www/default/;
index index.html index.php;
} location ~ \.php$ {
try_files $uri = ;
fastcgi_pass 127.0.0.1:;
# fastcgi_pass unix:/Applications/MAMP/Library/logs/fastcgi/nginxFastCGI.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
} error_page /.html;
error_page /.html;
}

然后加入第二个站点配置文件,实现多站点支持:

vim /usr/local/ect/nginx/sites-enabled/pengyunjing.conf

写入内容:

server {
listen ;
server_name test.pengyunjing.com;
root /Applications/MAMP/htdocs/www/pengyunjing/; access_log /usr/local/etc/nginx/logs/default.access.log main; location / {
root /Applications/MAMP/htdocs/www/pengyunjing/;
index index.html index.php;
} location ~ \.php$ {
try_files $uri = ;
fastcgi_pass 127.0.0.1:;
# fastcgi_pass unix:/Applications/MAMP/Library/logs/fastcgi/nginxFastCGI.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
} error_page /.html;
error_page /.html;
}

如果是本地测试,配置多站点记住要修改本地hosts文件,路径:

/private/etc/hosts

补充:

nginx配置文件路径:

/usr/local/etc/nginx/nginx.conf

nginx命令:

重新绑定配置文件:
sudo /usr/local/bin/nginx -c /usr/local/etc/nginx/nginx.conf 判断配置文件是否正确:
sudo /usr/local/bin/nginx -t 启动:
sudo /usr/local/bin/nginx 重启:
sudo /usr/local/bin/nginx -s reload 查询nginx主进程号:
ps -ef|grep nginx 正常停止进程:
sudo kill -QUIT 主进程号 快速停止进程:
sudo kill -TERM 主进程号 强制终止进程:
kill - 进程号 快捷强制关闭nginx进程:
sudo pkill -INT -o nginx

php-fpm配置文件路径:

/private/etc/php-fpm.conf

php-fpm命令:

查看进程:
ps aux | grep 'php-fpm' 启动php-fpm:
sudo php-fpm -D 强制终止进程:
kill - 进程号 快捷强制关闭php-fpm进程:
sudo pkill -INT -o php-fpm

至此,配置完成,在www下写一个测试文件,进行测试即可,如果浏览器出现502的错误,可能9000端口有冲突,终止nginx和php-fpm的进程再重新启动以下试试。

尽情的享受在Mac OS开发PHP的快感吧!

上一篇:在ajax请求后台时在请求标头RequestHeader加token


下一篇:Sublime Text 3 3126 注册码