search
HomeBackend DevelopmentPHP TutorialDetailed explanation of the steps to build LNMP development environment under Mac OS

This article mainlyintroduces the steps to build an LNMP development environment under Mac OS. The article introduces the step-by-step steps in very detail and has certain reference value for everyone. Friends who need it can follow Let’s take a look together.

1. Overview

Everyone should know that LNMP stands for: LinuxNginx+# under the system ##MySQL+PHP is a website server architecture. Linux is the collective name for a class of Unix computer operating systems and is currently the most popular free operating system. Representative versions include: debian, centos, ubuntu, fedora, gentoo, etc. Nginx is a high-performance HTTP and reverse proxy server, as well as an IMAP/POP3/SMTP proxy server. Mysql is a small relational database management system. PHP is a scripting language that is executed on the server side and embedded in HTML documents. These four kinds of software are all free and open source software. When combined together, they become a free, efficient, and scalable website service system. Let’s take a look at the details of this article.

2. Install Homebrew

An essential step for programmers using Mac is to install Homebrew, he is like The

yum command of centOS is the same as the apt-get command of ubuntu. Through the brew command, we can quickly install some software packages.

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

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

Use

brew doctor to check if there is a conflict, and then use brew update && brew upgradeUpgrade brew.

3. Install nginx

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

brew install nginx

If you need to use port 80, you need to add nginx to the root group:

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

Then use the command to start the nginx service:

sudo nginx

Test whether nginx is installed successfully, because the default

configuration file The listening port is 8080, so make a request to port 8080 first:

curl -IL http://www.php.cn/: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

nginx related operations are as follows:

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

4. Install php-fpm

Because brew does not have the source of php-fpm, you must first add the source:

brew tap homebrew/dupes
brew tap homebrew/php

Then install php-fpm, 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, wait for a few minutes to complete the installation.

After the installation is complete, you need to add php to

$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

Then you can set php-fpm to start automatically at boot:

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

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

lsof -Pni4 | grep LISTEN | grep php

If it starts successfully, there should be output similar to the following:

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 the brew command:

brew install mysql

Similarly, you can set MySQL to start automatically at boot:

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

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

mysql_secure_installation

Then the following content will be output:

> 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]     //是否重载表格数据,选择是

Test whether the database is installed successfully:

mysql -u root -p

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

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

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

6. Configure nginx

First, for Our configuration file creates some folders. These are directories modeled after Ubuntu's nginx structure:

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

Then modify the nginx configuration file:

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

Replace the content with:

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/*;
}

Then create the php-fpm configuration file:

vim /usr/local/ect/nginx/conf.d/php-fpm

Enter the following content:

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;
}

Then add the site configuration file:

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

Enter the following content:

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;
}

Restart nginx. At this point, the configuration is completed. Write a test file under www and test it

Summary

The above is the detailed content of Detailed explanation of the steps to build LNMP development environment under Mac OS. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
PHP's Purpose: Building Dynamic WebsitesPHP's Purpose: Building Dynamic WebsitesApr 15, 2025 am 12:18 AM

PHP is used to build dynamic websites, and its core functions include: 1. Generate dynamic content and generate web pages in real time by connecting with the database; 2. Process user interaction and form submissions, verify inputs and respond to operations; 3. Manage sessions and user authentication to provide a personalized experience; 4. Optimize performance and follow best practices to improve website efficiency and security.

PHP: Handling Databases and Server-Side LogicPHP: Handling Databases and Server-Side LogicApr 15, 2025 am 12:15 AM

PHP uses MySQLi and PDO extensions to interact in database operations and server-side logic processing, and processes server-side logic through functions such as session management. 1) Use MySQLi or PDO to connect to the database and execute SQL queries. 2) Handle HTTP requests and user status through session management and other functions. 3) Use transactions to ensure the atomicity of database operations. 4) Prevent SQL injection, use exception handling and closing connections for debugging. 5) Optimize performance through indexing and cache, write highly readable code and perform error handling.

How do you prevent SQL Injection in PHP? (Prepared statements, PDO)How do you prevent SQL Injection in PHP? (Prepared statements, PDO)Apr 15, 2025 am 12:15 AM

Using preprocessing statements and PDO in PHP can effectively prevent SQL injection attacks. 1) Use PDO to connect to the database and set the error mode. 2) Create preprocessing statements through the prepare method and pass data using placeholders and execute methods. 3) Process query results and ensure the security and performance of the code.

PHP and Python: Code Examples and ComparisonPHP and Python: Code Examples and ComparisonApr 15, 2025 am 12:07 AM

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP in Action: Real-World Examples and ApplicationsPHP in Action: Real-World Examples and ApplicationsApr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP: Creating Interactive Web Content with EasePHP: Creating Interactive Web Content with EaseApr 14, 2025 am 12:15 AM

PHP makes it easy to create interactive web content. 1) Dynamically generate content by embedding HTML and display it in real time based on user input or database data. 2) Process form submission and generate dynamic output to ensure that htmlspecialchars is used to prevent XSS. 3) Use MySQL to create a user registration system, and use password_hash and preprocessing statements to enhance security. Mastering these techniques will improve the efficiency of web development.

PHP and Python: Comparing Two Popular Programming LanguagesPHP and Python: Comparing Two Popular Programming LanguagesApr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

The Enduring Relevance of PHP: Is It Still Alive?The Enduring Relevance of PHP: Is It Still Alive?Apr 14, 2025 am 12:12 AM

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor