说明:本教程主要包括以下三个部分: 1.源代码编译安装Nginx 2.源代码编译安装php以及mysql、redis扩展模块 3.配置虚拟主机 文中所涉及安装包程序均提供下载链接,欢迎使用 运行环境以及前置条件:Ubuntu 12.04 LTS 已安装g编译环境 所有源程序路径位于:roo
说明:本教程主要包括以下三个部分:
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
pcre库下载地址
tar –zxvf pcre-8.21.tar.gz
cd pcre-8.21
./configure --prefix=/usr/local/pcre-8.21
make
make install
安装zlib库——为了gzip压缩
zlib下载地址
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加密
openssl下载地址
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——服务器软件
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前置条件:
ncurses下载地址
tar -zxvf ncurses-5.4.tar.gz
cd ncurses-5.4
./configure
make
make install
安装mysql——此处使用是源码包编译安装
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请求
curl库下载地址
tar -zxvf curl-7.39.0.tar.gz
./configure --prefix=/usr/local/curl-7.39.0
make
make install
安装libxml2库——用于xml解析
libxml2下载地址
libxml2-devel下载地址
tar –xjf libxml2-2.6.23.tar.bz
cd libxml2-2.6.23
./configure
make
make install
rpm –ivh libxml2-devel-2.6.23-1.i386.rpm
安装php
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-libxml2
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扩展模块:
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-fpm restart
第三部分:配置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

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 Linux new version
SublimeText3 Linux latest version
