LNMP开发环境在Mac OS下搭建的步骤详解

黄舟
Libérer: 2023-03-06 15:20:02
original
1455 Les gens l'ont consulté

这篇文章主要介绍了在Mac OS下搭建LNMP开发环境的步骤,文中通过一步步的步骤介绍的非常详细,对大家具有一定的参考价值,需要的朋友们下面来一起看看吧。

一、概述

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

二、安装Homebrew

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

使用命令行安装Homebrew的命令如下:

ruby -e "$(curl -fsSL https://raw.github.com/mxcl/homebrew/go)"
Copier après la connexion

使用brew doctor检查是否存在冲突,然后使用brew update && brew upgrade对brew进行升级。

三、安装nginx

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

brew install nginx
Copier après la connexion

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

sudo cp -v /usr/local/opt/nginx/*.plist /Library/LaunchDaemons/
sudo chown root:wheel /Library/LaunchDaemons/homebrew.mxcl.nginx.plist
Copier après la connexion

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

sudo nginx
Copier après la connexion

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

curl -IL http://www.php.cn/:8080
Copier après la connexion

结果应该类似于下:

HTTP/1.1 200 OK
Server: nginx/1.9.1
Date: Fri, 29 May 2015 14:50:47 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Fri, 29 May 2015 14:40:47 GMT
Connection: keep-alive
ETag: "5444dea7-264"
Accept-Ranges: bytes
Copier après la connexion

nginx的相关操作如下:

sudo nginx //启动nginx
sudo nginx -s reload|reopen|quit //重新加载|重启|退出
Copier après la connexion

四、安装php-fpm

因为brew并没有php-fpm的源,所以首先要添加源:

brew tap homebrew/dupes
brew tap homebrew/php
Copier après la connexion

然后安装php-fpm,输入命令:

brew install php56 --whitout-apache --with-imap --with-tidy --with-debug --with-pgsql --with-mysql --with-fpm
Copier après la connexion

程序会自动安装,等待几分钟后完成安装。

安装完成后,还需要将php加入$PATH当中:

# 如果使用bash的话
vim ~/.bash_profile
export PATH="/usr/local/sbin:$PATH"
source ~/.bash_profile

# 如果使用ZSH的话
vim ~/.zshrc
export PATH="/usr/local/sbin:$PATH"
source ~/.zshrc
Copier après la connexion

然后可以设置php-fpm的开机自启动:

mkdir -p ~/Library/LaunchAgents
ln -sfv /usr/local/opt/php56/homebrew.mxcl.php56.plist ~/Library/LaunchAgents/
launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.php56.plist
Copier après la connexion

使用以下命令监测php-fpm是否启动成功:

lsof -Pni4 | grep LISTEN | grep php
Copier après la connexion

如果启动成功应当有以下类似输出:

php-fpm 27578 wenzhiquan 9u IPv4 0xf29f8b26c08fc27  0t0 TCP 127.0.0.1:9000 (LISTEN)
php-fpm 27628 wenzhiquan 0u IPv4 0xf29f8b26c08fc27  0t0 TCP 127.0.0.1:9000 (LISTEN)
php-fpm 27629 wenzhiquan 0u IPv4 0xf29f8b26c08fc27  0t0 TCP 127.0.0.1:9000 (LISTEN)
php-fpm 27630 wenzhiquan 0u IPv4 0xf29f8b26c08fc27  0t0 TCP 127.0.0.1:9000 (LISTEN)
Copier après la connexion

五、安装MySQL

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

brew install mysql
Copier après la connexion

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

ln -sfv /usr/local/opt/mysql/*.plist ~/Library/LaunchAgents
launchctl load ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist
Copier après la connexion

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

mysql_secure_installation
Copier après la connexion

然后会输出以下内容:

> 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]     //是否重载表格数据,选择是
Copier après la connexion

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

mysql -u root -p
Copier après la connexion

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

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

mysql> exit   //输入exit退出数据库
Copier après la connexion

六、配置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-enabled
mkdir -p /usr/local/etc/nginx/conf.d
mkdir -p /usr/local/etc/nginx/ssl

sudo mkdir -p /var/www
sudo chown :staff /var/www
sudo chmod 775 /var/www
Copier après la connexion

然后修改nginx配置文件:

vim /usr/local/etc/nginx/nginx.conf
Copier après la connexion

将内容替换为:

worker_processes 1;

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

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 /usr/local/etc/nginx/logs/access.log main;

 sendfile   on;

 keepalive_timeout 65;

 index index.html index.php;

 include /usr/local/etc/nginx/sites-enabled/*;
}
Copier après la connexion

然后创建php-fpm配置文件:

vim /usr/local/ect/nginx/conf.d/php-fpm
Copier après la connexion

输入以下内容:

location ~ \.php$ {
 try_files  $uri = 404;
 fastcgi_pass 127.0.0.1:9000;
 fastcgi_index index.php;
 fastcgi_param script_FILENAME $document_root$fastcgi_script_name;
 include  fastcgi_params;
}
Copier après la connexion

然后加入站点配置文件:

vim /usr/local/ect/nginx/sites-enabled/default
Copier après la connexion

输入以下内容:

server {
 listen  80;
 server_name localhost;
 root  /var/www/;

 access_log /usr/local/etc/nginx/logs/default.access.log main;

 location / {
  include /usr/local/etc/nginx/conf.d/php-fpm;
 }

 location = /info {
  allow 127.0.0.1;
  deny all;
  rewrite (.*) /.info.php;
 }

 error_page 404  /404.html;
 error_page 403  /403.html;
}
Copier après la connexion

重启nginx,至此,配置完成,在www下写一个测试文件,进行测试即可

总结

Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!

Étiquettes associées:
source:php.cn
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal
À propos de nous Clause de non-responsabilité Sitemap
Site Web PHP chinois:Formation PHP en ligne sur le bien-être public,Aidez les apprenants PHP à grandir rapidement!