Table of Contents
安装 Ubuntu Server
安装依赖软件
源码安装 Nginx
编译 Nginx
启动 Nginx
停止 Nginx
验证 Nginx 是否已经启动
源码安装 PHP fpm fastcgi
编译 PHP
验证 PHP 是否已经安装成功
创建 php.ini 配置文件
修改 Nginx 配置文件支持 PHP
启动 php-cgi
验证 Nginx + PHP 已经正常工作
用 fpm 方式启动 php-cgi 进程
创建 php-fpm.conf
创建 nobody 用户
启动和停止 php-fpm
单独编译安装 PHP 模块
Related posts:
Home php教程 php手册 初学者的 Linux 安装 Nginx PHP fpm 配置教程

初学者的 Linux 安装 Nginx PHP fpm 配置教程

Jun 06, 2016 pm 08:11 PM
fpm linux nginx php beginner Install Configuration

网上的很多 Linux 操作系统下安装 Nginx + PHP 的教程都很老, 而且各种依赖, 对于初学者来说比较容易混淆, 所以, 我重新写了此篇博客, 用于介绍最简单的 Linux 下安装 Nginx + PHP 的方法. 此文会经常更新, 保持与时俱进. 如果你不是在 ideawu.net 网站看到

网上的很多 Linux 操作系统下安装 Nginx + PHP 的教程都很老, 而且各种依赖, 对于初学者来说比较容易混淆, 所以, 我重新写了此篇博客, 用于介绍最简单的 Linux 下安装 Nginx + PHP 的方法.

此文会经常更新, 保持与时俱进. 如果你不是在 ideawu.net 网站看到本文, 请访问 ideawu.net, 阅读最新的版本.

安装 Ubuntu Server

你可能听过别的 Linux 发行版, 但如果你是初学者, 并且对 Nginx + PHP 安装不了解, 我建议你安装 Ubuntu Server. 请到 Ubuntu 的官网下载最新版本的 ISO 文件, 我用的是 12.04.3-server-amd64.iso.

安装依赖软件

在 Ubuntu 下安装软件, 可以用 sudo aptitude 命令. 这是一个类似图形界面的软件, 启动后, 按 “/” 输入软件名进行搜索, 选中后按 “+”, 再按 “g” 即可安装.

需要安装的依赖软件包是: libpcre3-dev, libxml2-dev, zlib1g-dev.

源码安装 Nginx

编译 Nginx

到 Nginx 的官网下载最新版本的 Nginx 源码, 当前(2013-11-11)的最新版本是 1.5.6, 未来会有升级, 所以下载的 wget 只是一个示例, 你应该替换成最新的版本.

wget http://nginx.org/download/nginx-1.5.6.tar.gz
tar xzf nginx-1.5.6.tar.gz
cd nginx-1.5.6
./configure
make
sudo make install
Copy after login

Nginx 会被安装在 /usr/local/nginx 目录下.

启动 Nginx

sudo /usr/local/nginx/sbin/nginx
Copy after login

停止 Nginx

sudo /usr/local/nginx/sbin/nginx -s stop
Copy after login

验证 Nginx 是否已经启动

打开浏览器, 访问 http://127.0.0.1/ 看看网页是否能打开. 如果你在另一台机器上访问, 把 127.0.0.1 换成 Ubuntu 服务器的 IP 地址即可. 如下图

图

更多的 Nginx 配置文件的修改, 等我们安装完 PHP 之后再关心, 先使用默认的配置文件运行起来. 默认的配置文件在 /usr/local/nginx/conf/nginx.conf 文件.

源码安装 PHP fpm fastcgi

编译 PHP

同样, 到 PHP 的官网下载最新版本的 PHP 源码.

wget http://us1.php.net/get/php-5.5.5.tar.bz2/from/cn2.php.net/mirror
tar xjf mirror
cd php-5.5.5
./configure --enable-fpm
make
sudo make install
Copy after login

PHP 的可执行文件(php, php-cgi)会被安装到 /usr/local/bin 目录下. 配置文件是 /usr/local/lib/php.ini, 这个文件并不存储, 你需要自己创建.

验证 PHP 是否已经安装成功

php -v
Copy after login

上面的命令应该有版本信息输出.

创建 php.ini 配置文件

sudo cp php.ini-production /usr/local/lib/php.ini
Copy after login

不过, 现在你还用不上, 等用的时候再修改这个文件.

修改 Nginx 配置文件支持 PHP

编译 Nginx 的配置文件 /usr/local/nginx/conf/nginx.conf, 加入下面的内容. 原配置文件里有类似的内容, 你加在那段内容的紧跟后面即可.

location ~ \.php$ {
    root           html;
    fastcgi_pass   127.0.0.1:9000;
    fastcgi_index  index.php;
    fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
    include        fastcgi_params;
}
Copy after login

然后重启 Nginx

sudo /usr/local/nginx/sbin/nginx -s reload
Copy after login

这时候, Nginx + PHP 还不能工作, 你还需要启动 php-cgi 进程.

启动 php-cgi

/usr/local/bin/php-cgi -b 9000
Copy after login

验证 Nginx + PHP 已经正常工作

首先, 你需要创建一个 PHP 脚本, 新建一个文件 /usr/local/nginx/html/index.php, 这个文件的内容只有一行:

<?php phpinfo();
Copy after login

然后修改它的文件属性

sudo chmod ugo+rwx -R /usr/local/nginx/html
Copy after login

然后, 用浏览器访问 http://127.0.0.1/index.php 你应该能看到 PHP 的信息, 如下图

图

用 fpm 方式启动 php-cgi 进程

刚才的一行命令虽然启动了 php-cgi, Nginx + PHP 也正常工作了, 但这不是正式环境的做法, 只是一种临时方法. 正式的生产环境一般用 fpm 方式启动 php-cgi 为守护进程.

php-fpm 程序安装在 /usr/local/sbin/php-fpm.

创建 php-fpm.conf

PHP 默认安装了一个 php-fpm.conf.default 的模板文件, 需要将它改名:

sudo cp /usr/local/etc/php-fpm.conf.default /usr/local/etc/php-fpm.conf
Copy after login

然后修改 /usr/local/etc/php-fpm.conf, 将

;pid = run/php-fpm.pid
Copy after login

前面的分号(注释)去掉.

创建 nobody 用户

sudo groupadd nobody
sudo useradd -g nobody nobody
Copy after login

启动和停止 php-fpm

# 启动
sudo /usr/local/sbin/php-fpm
# 停止
sudo kill `cat /usr/local/var/run/php-fpm.pid`
Copy after login

这时候, php-cgi 已经作为守护进程启动了.

单独编译安装 PHP 模块

上面编译的 PHP 可能没有包含一些你需要的模块, 不过别担心, 你可以不必重新编译整个 PHP, 而是只编译和安装缺少的那个模块. 这也是 PHP 模块化的好处. 下面以单独安装 PHP 的 sockets 模块为例.

进入你的 PHP 源码的 ext/sockets 目录. ext 目录是所有模块的总目录. 然后执行

/usr/local/bin/phpize
./configure
make
sudo cp modules/sockets.so /usr/local/lib/php/extensions/no-debug-non-zts-20121212/
Copy after login

然后修改 php.ini 配置文件, 找到

;;;;;;;;;;;;;;;;;;;;;;
; Dynamic Extensions ;
;;;;;;;;;;;;;;;;;;;;;;
Copy after login

在其后面加上

extension=sockets.so
Copy after login

然后重启 php-fpm. 这样, sockets 模块就已经安装成功了.

  1. Nginx + PHP 配置和启动脚本
  2. Nginx 499 错误码以及 AJAX 调用失败
  3. Linux+Keepalived双机互备
  4. nginx-push-stream-module 笔记
  5. HBase 在 Linux 下安装和配置

初学者的 Linux 安装 Nginx PHP fpm 配置教程 你现在看的文章是: 初学者的 Linux 安装 Nginx PHP fpm 配置教程

Linode VPS - 美国虚拟主机 | IT牛人博客聚合网站

Statement of this Website
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

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)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

deepseek web version entrance deepseek official website entrance deepseek web version entrance deepseek official website entrance Feb 19, 2025 pm 04:54 PM

DeepSeek is a powerful intelligent search and analysis tool that provides two access methods: web version and official website. The web version is convenient and efficient, and can be used without installation; the official website provides comprehensive product information, download resources and support services. Whether individuals or corporate users, they can easily obtain and analyze massive data through DeepSeek to improve work efficiency, assist decision-making and promote innovation.

How to install deepseek How to install deepseek Feb 19, 2025 pm 05:48 PM

There are many ways to install DeepSeek, including: compile from source (for experienced developers) using precompiled packages (for Windows users) using Docker containers (for most convenient, no need to worry about compatibility) No matter which method you choose, Please read the official documents carefully and prepare them fully to avoid unnecessary trouble.

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

BITGet official website installation (2025 beginner's guide) BITGet official website installation (2025 beginner's guide) Feb 21, 2025 pm 08:42 PM

BITGet is a cryptocurrency exchange that provides a variety of trading services including spot trading, contract trading and derivatives. Founded in 2018, the exchange is headquartered in Singapore and is committed to providing users with a safe and reliable trading platform. BITGet offers a variety of trading pairs, including BTC/USDT, ETH/USDT and XRP/USDT. Additionally, the exchange has a reputation for security and liquidity and offers a variety of features such as premium order types, leveraged trading and 24/7 customer support.

Ouyi okx installation package is directly included Ouyi okx installation package is directly included Feb 21, 2025 pm 08:00 PM

Ouyi OKX, the world's leading digital asset exchange, has now launched an official installation package to provide a safe and convenient trading experience. The OKX installation package of Ouyi does not need to be accessed through a browser. It can directly install independent applications on the device, creating a stable and efficient trading platform for users. The installation process is simple and easy to understand. Users only need to download the latest version of the installation package and follow the prompts to complete the installation step by step.

See all articles