SoFunction
Updated on 2025-03-09

Detailed explanation of the steps to build an LNMP development environment under Mac OS

1. Overview

Everyone should know that LNMP represents: Nginx+MySQL+PHP website server architecture under Linux system. Linux is a general term for a type of Unix computer operating system and is currently the most popular free operating system. Representative versions include: debian, centos, ubuntu, fedora, gentoo, etc.NginxIt is a high-performance HTTP and reverse proxy server, and it is also an IMAP/POP3/SMTP proxy server.MysqlIt is a small relational database management system.PHPIt is a scripting language that embeds HTML documents executed on the server side. These four software are free and open source software, and they are combined to form a free, efficient and highly extensible website service system. Let’s take a look at the details of this article below.

2. Install Homebrew

One essential step for programmers using Mac is to install Homebrew, which is like centOSyumCommands and ubuntuapt-getLike command, passbrewWe can quickly install some software packages.

The command to install Homebrew using the command line is as follows:

ruby -e "$(curl -fsSL /mxcl/homebrew/go)"

usebrew doctorCheck for conflicts and usebrew update && brew upgradeUpgrade brew.

3. Install nginx

nginx can be installed directly using brew command in Mac OS:

brew install nginx

If you need to use port 80, nginx needs to be added to the root group:

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

Then use the command to start nginx service:

sudo nginx

Test whether nginx is installed successfully, because the default configuration file listens to port 8080, so first initiate a request for port 8080:

curl -IL http://127.0.0.1:8080

The result should be similar to the following:

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

The related operations of nginx are as follows:

sudo nginx //Start nginxsudo nginx -s reload|reopen|quit //Reload|Restart|quit

4. Install php-fpm

Because brew does not have a source of php-fpm, we need to add the source first:

brew tap homebrew/dupes
brew tap homebrew/php

Then install php-fpm and enter the command:

brew install php56 --whitout-apache --with-imap --with-tidy --with-debug --with-pgsql --with-mysql --with-fpm

The program will be installed automatically and will be installed after waiting for a few minutes.

After the installation is completed, you need to add php$PATHamong:

# If using bashvim ~/.bash_profile
export PATH="/usr/local/sbin:$PATH"
source ~/.bash_profile

# If using ZSHvim ~/.zshrc
export PATH="/usr/local/sbin:$PATH"
source ~/.zshrc

Then you can set the php-fpm startup:

mkdir -p ~/Library/LaunchAgents
ln -sfv /usr/local/opt/php56/. ~/Library/LaunchAgents/
launchctl load -w ~/Library/LaunchAgents/.

Use the following command to monitor whether php-fpm is started successfully:

lsof -Pni4 | grep LISTEN | grep php

If the startup is successful, the following output should be:

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)

5. Install MySQL

MySQL can also be installed directly using brew command:

brew install mysql

Similarly, you can set up MySQL's boot boot:

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

Then perform a secure installation of MySQL. Use the following commands to change the root password, delete anonymous users, close remote connections, etc.

mysql_secure_installation

Then the following will be output:

> Enter current password for root (enter for none):  //There is no password by default, just press Enter> Change the root password? [Y/n]      //Whether to change the root password, select Yes, then enter and confirm the password> Remove anonymous users? [Y/n]       // Whether to delete anonymous users, select Yes> Disallow root login remotely? [Y/n]     // Whether to prohibit remote login, select Yes> Remove test database and access to it? [Y/n]   // Whether to delete the test database, select Yes> Reload privilege tables now? [Y/n]     //Whether to overload the table data,Choose Yes

Test whether the database is installed successfully:

mysql -u root -p

Then enter the root password you just set, and the following content will be output:

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

mysql> exit   //enterexitExit the database

6. Configure nginx

First, create some folders for our configuration file, which are directories created based on the nginx structure of ubuntu:

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/
mkdir -p /usr/local/etc/nginx/ssl

sudo mkdir -p /var/www
sudo chown :staff /var/www
sudo chmod 775 /var/www

Then modify the nginx configuration file:

vim /usr/local/etc/nginx/

Replace the content with:

worker_processes 1;

error_log /usr/local/etc/nginx/logs/ debug;

events {
 worker_connections 1024;
}

http {
 include    ;
 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/ main;

 sendfile   on;

 keepalive_timeout 65;

 index  ;

 include /usr/local/etc/nginx/sites-enabled/*;
}

Then create the php-fpm configuration file:

vim /usr/local/ect/nginx//php-fpm

Enter the following:

location ~ \.php$ {
 try_files  $uri = 404;
 fastcgi_pass 127.0.0.1:9000;
 fastcgi_index ;
 fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
 include  fastcgi_params;
}

Then add the site configuration file:

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

Enter the following:

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

 access_log /usr/local/etc/nginx/logs/ main;

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

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

 error_page 404  /;
 error_page 403  /;
}

Restart nginx, and at this point, the configuration is completed, write a test file under www and perform the test

Summarize

The above are all the steps to build an LNMP development environment on Mac OS. Enjoy the pleasure of developing PHP on Mac OS! I hope the content of this article will be of some help to your study or work. If you have any questions, you can leave a message to communicate. Thank you for your support.