Nginx+Php-fpm+MySQL+Redis源代码编译安装指南
说明:本教程主要包括以下三个部分:
1. 源代码编译安装Nginx
2. 源代码编译安装php以及mysql、redis扩展模块
3. 配置虚拟主机
文中所涉及安装包程序均提供下载链接,欢迎使用
运行环境以及前置条件:Ubuntu 12.04 LTS 已安装g++编译环境
所有源程序路径位于:root@ubuntu:/home/shihai/Desktop/Nginx文件夹下,如下图所示:
程序安装路径位于:/usr/local文件夹下
第一部分:安装Nginx
安装Nginx前需要安装依赖库PCRE库、zlib库、SSL库
安装PCRE库??为了rewrite
tar ?zxvf pcre-8.21.tar.gz
cd pcre-8.21
./configure --prefix=/usr/local/pcre-8.21
make
make install
安装zlib库??为了gzip压缩
tar ?zxvf zlib-1.2.8.tar.gz
cd zlib-1.2.8
./configure --prefix=/usr/local/zlib-1.2.8
make
make install
安装ssl库??支持ssl加密
tar -zxvf openssl-1.0.1c.tar.gz
cd openssl-1.0.1c
./config --prefix=/usr/local/openssl-1.0.1
make
make install
安装nginx??服务器软件
tar -zxvf nginx-1.2.8.tar.gz
cd nginx-1.2.8
./configure --prefix=/usr/local/nginx-1.2.8 \
--with-pcre=../pcre-8.21/ \
--with-zlib=../zlib-1.2.8/
make
make install
设定nginx启动的配置文件
/usr/local/nginx-1.2.8/sbin# ./nginx -c /usr/local/nginx-1.2.8/conf/nginx.conf
/usr/local/nginx-1.2.8/sbin# ./nginx -s reload
查看nginx进程
ps ?ef|grep nginx
打开localhost
当你看到上图所示内容时,说明Nginx服务器已经安装成功
第二部分:安装php以及mysql、redis扩展模块
安装ncurses??安装mysql前置条件:
tar -zxvf ncurses-5.4.tar.gz
cd ncurses-5.4
./configure
make
make install
安装mysql??此处使用是源码包编译安装
tar -zxvf mysql-5.1.73.tar.gz
cd mysql-5.1.73
./configure --prefix=/usr/local/mysql-5.1.73
make
make install
安装curl库??用于curl请求
tar -zxvf curl-7.39.0.tar.gz
./configure --prefix=/usr/local/curl-7.39.0
make
make install
安装php
tar -zxvf php-5.2.14.tar.gz
gunzip php-5.2.14-fpm-0.5.14.diff.gz
patch -d php-5.2.14 -p1
cd php-5.2.14
./configure --prefix=/usr/local/php-5.2.14\
--enable-fastcgi \
--enable-fpm \
--enable-sockets \
--enable-mbstring \
--with-mysql=/usr/local/mysql-5.1.73 \
--with-mysqli=/usr/local/mysql-5.1.73/bin/mysql_config\
--with-pdo-mysql=/usr/local/mysql-5.1.73 \
--with-curl=/usr/local/curl-7.39.0 \
--with-openssl=/usr/local/openssl-1.0.1 \
--with-mcrypt
make
make install
启动php-fpm使用如下命令:
/usr/local/php-5.2.14/sbin# ./php-fpm start
启动php-fpm的时候出现
Startingphp_fpm Dec 29 15:27:32.502790 [ERROR] fpm_unix_conf_wp(), line 124: pleasespecify user and group other than root, pool 'default'
解决办法:进入目录:/usr/local/php-5.2.14/etc只需要修改php-fpm.conf
Unix user of processes
Unix group of processes
将去掉即可。至于user/group根据实际情况修改(www)。
重新启动 /usr/local/php-5.2.14/sbin# ./php-fpm restart 成功了
安装redis扩展模块:
unzip phpredis-master.zip
exportPATH=/usr/local/php-5.2.14/bin/:$PATH
cp -r phpredis-master php-5.2.14/ext/
cd php-5.2.14/ext/phpredis-master
phpize
./configure--with-php-config=/usr/local/php-5.2.14/bin/php-config
make
make install
扩展库路径:/usr/local/php-5.2.14/lib/php/extensions/no-debug-non-zts-20060613/
在php扩展库路径下,可以找到编译生成的redis扩展库文件redis.so
打开路径/usr/local/php-5.2.14/lib/php.ini下的php.ini文件,使用命令如下:
vim php.ini
文件内容快速查找(按下“N”可以查找下一个匹配位置),使用命令如下:
:?extension
找到如图所示的指定位置后插入以下内容:
extension = redis.so
php会自动到扩展库路径下加载redis.so文件
如果没找到php.ini文件,可以全盘搜索该文件,使用命令如下:
find / -name php.ini
搜索结果显示此路径下存在/etc/php5/cli/php.ini文件,于是拷贝至/usr/local/php-5.2.14/lib,使用命令如下:
/usr/local/php-5.2.14/lib# cp/etc/php5/cli/php.ini php.ini
此处需要重启php-fpm才能生效,使用命令如下:
/usr/local/php-5.2.14/sbin# ./php-fpmrestart
第三部分:配置nginx虚拟主机
新建虚拟主机配置文件目录
/usr/local/nginx-1.2.8# mkdir vhosts
cd vhosts
touch scott.qq.com.conf
vim scottshi.qq.com.conf
输入以下内容配置自定义虚拟主机:
server {
listen 8001;/*监听端口号*/
server_name scott.qq.com;/*域名*/
access_log/usr/local/nginxweb/htdocs/access.log;/*站点访问日志*/
location / {
root /usr/local/nginxweb/htdocs/;/*页面文件目录*/
index index.php index.html index.htm;
}
error_page 500 502 503 504 /50x.html;/*服务器错误页面*/
location = /50x.html {
root html;
}
# pass the PHP scripts to FastCGI serverlistening on 127.0.0.1:9000
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000; /*Nginx转发请求地址*/
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME/usr/local/nginxweb/htdocs/$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
}
保存退出后,进入nginx的配置文件nginx.conf
/usr/local/nginx-1.2.8/conf# vim nginx.conf
与默认server层级并列且位于http层级之内,添加如下内容,使得自定义虚拟主机生效:
include /usr/local/nginx-1.2.8/vhosts/*;
保存退出后,重启nginx服务器,重新载入配置文件,使用命令如下:
/usr/local/nginx-1.2.8/sbin# ./nginx ?s reload
编写php测试页面:
进入目录/usr/local/nginxweb/htdocs/,新建test.php文件,输入test.php页面内容:
Phpinfo();
?>
保存退出
使用浏览器,访问以下地址:
scott.qq.com:8001/test.php
此页面会显示配置php时的指令还有各个功能模块,包括fastcgi、mysql、curl、redis等

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Alipay PHP...

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

The enumeration function in PHP8.1 enhances the clarity and type safety of the code by defining named constants. 1) Enumerations can be integers, strings or objects, improving code readability and type safety. 2) Enumeration is based on class and supports object-oriented features such as traversal and reflection. 3) Enumeration can be used for comparison and assignment to ensure type safety. 4) Enumeration supports adding methods to implement complex logic. 5) Strict type checking and error handling can avoid common errors. 6) Enumeration reduces magic value and improves maintainability, but pay attention to performance optimization.

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.
