Home >Backend Development >PHP Tutorial >Detailed explanation of the steps to build LNMP development environment under Mac OS

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

黄舟
黄舟Original
2017-03-13 16:38:591622browse

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