1.安装编译环境
yum -yinstall gcc gcc-c++ automake autoconf libtool make glibc
yum -y install libmcrypt-devel mhash-devellibxslt-devel libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devellibxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2bzip2-devel ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-develkrb5 krb5-devel libidn libidn-devel openssl openssl-devel
2.安装nginx
一般我们都需要先装pcre,zlib,前者为了重写rewrite,后者为了gzip压缩
2.1选定源码目录
可以是任何目录,本文选定的是/usr/local/src
cd /usr/local/src
2.2安装PCRE库
ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/
下载最新的 PCRE 源码包,使用下面命令下载编译和安装 PCRE 包:
cd /usr/local/src
wgetftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.37.tar.gz
tar –zxvf pcre-8.37.tar.gz
cd pcre-8.37
./configure --enable-utf8
make && make install
2.3安装zlib库
http://zlib.net/zlib-1.2.8.tar.gz下载最新的 zlib 源码包,使用下面命令下载编译和安装 zlib包:
cd /usr/local/src
wget http://zlib.net/zlib-1.2.8.tar.gz
tar -zxvf zlib-1.2.8.tar.gz
cd zlib-1.2.8
./configure
make && make install
2.4安装ssl(某些vps默认没装ssl)(直解压,不安装)
cd /usr/local/src
wget http://www.openssl.org/source/openssl-1.0.2g.tar.gz
tar -zxvf openssl-1.0.2g.tar.gz
2.5安装nginx
Nginx 一般有两个版本,分别是稳定版和开发版,您可以根据您的目的来选择这两个版本的其中一个,下面是把 Nginx 安装到/usr/local/nginx 目录下的详细步骤:
cd /usr/local/src
wget http://nginx.org/download/nginx-1.9.12.tar.gz
tar -zxvf nginx-1.9.12.tar.gz
cd nginx-1.9.12
./configure --sbin-path=/usr/local/nginx/nginx
--conf-path=/usr/local/nginx/nginx.conf
--pid-path=/usr/local/nginx/nginx.pid
--with-http_ssl_module
--with-pcre=/usr/local/src/pcre-8.37
--with-zlib=/usr/local/src/zlib-1.2.8
--with-openssl=/usr/local/src/openssl-1.0.2g
make && make install
--with-pcre=/usr/src/pcre-8.37 指的是pcre-8.34 的源码路径。
--with-zlib=/usr/src/zlib-1.2.8指的是zlib-1.2.8 的源码路径。
检测nginx是否安装成功
[[email protected] nginx-1.9.12]#/usr/local/nginx/nginx -t
nginx: the configuration file/usr/local/nginx/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/nginx.conftest is successful
安装成功后/usr/local/nginx 目录下如下
fastcgi.conf koi-win nginx.conf.default
fastcgi.conf.default logs scgi_params
fastcgi_params mime.types scgi_params.default
fastcgi_params.default mime.types.default uwsgi_params
html nginx uwsgi_params.default
koi-utf nginx.conf win-utf
6.启动
确保系统的 80 端口没被其他程序占用,运行/usr/local/nginx/nginx 命令来启动 Nginx,
netstat –tunlp| grep 80
如果查不到结果后执行,有结果则忽略此步骤
打开浏览器访问此机器的IP,如果浏览器出现 Welcome to nginx! 则表示 Nginx 已经安装并运行成功。到这里nginx就安装完成了,如果只是处理静态html就不用继续安装了
如果你需要处理php脚本的话,还需要安装php-fpm
常见问题:
(2)启动nginx出现下面的报错信息:
Nginx [emerg]: bind() to 0.0.0.0:80failed (98: Address already in use)
解决方法:
使用命令关闭占用80端口的程序,然后再启用
#fuser -k 80/tcp
(3)启动php-fpm报错:
ERROR: unable to bind listening socket for address ‘127.0.0.1:9000‘:Address already in use (98)
ERROR: FPM initialization failed
解决方法:
解决办法:关闭进程然后再启动
killall php-fpm
/usr/local/php/sbin/php-fpm
(4)命令行下执行php,提示找不到命令
-bash:/usr/bin/php: No such file or directory
vi/etc/profile
在文件底部增加一行配置
export PATH=/usr/local/php/bin:$PATH
保存退出
source/etc/profile
常用命令:
(1)查看php-fpm进程数:
ps -aux | grep php-fpm
(2)启动nginx:/usr/local/nginx/nginx
二、安装php-fpm
安装前准备
centos下执行
yum -y install gcc gcc-c++ glibc automake autoconf libtool make
yum -y install libmcrypt-devel mhash-devel libxslt-devel
libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel
zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel
ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel
krb5 krb5-devel libidn libidn-devel openssl openssl-devel
新版php-fpm安装(推荐安装方式)
或者先安装libmcrypt-dev,教程在后面
wget http://cn2.php.net/distributions/php-5.6.19.tar.gz
tar zvxf php-5.6.19.tar.gz
cd php-5.6.19
./configure --prefix=/usr/local/php --enable-fpm --with-mcrypt --enable-mbstring--disable-pdo --with-curl --disable-debug --disable-rpath --enable-inline-optimization --with-bz2 --with-zlib --enable-opcache=no --enable-pcntl--enable-mbregex --with-mhash --with-pcre-regex --with-mysql --with-mysqli --with-gd --with-jpeg-dir
make all install
安装安装成功后的提示:
Wrote PEAR system config file at: /usr/local/php/etc/pear.conf
You may want to add: /usr/local/php/lib/php to your php.ini include_path
/usr/local/src/php-5.6.19/build/shtoolinstall -c ext/phar/phar.phar /usr/local/php/bin
ln -s -f phar.phar /usr/local/php/bin/phar
在/usr/local/lib目录下没有php.ini文件。在这我们就先复制php安装文件提供的模版,如下:
cp php.ini-production /usr/local/php/lib/php.ini
vim /etc/php.ini (增加/usr/local/php/lib/php到include_path)
include_path = "/usr/local/php/lib/php"
/usr/local/src/php-5.6.19/build/shtoolinstall -c ext/phar/phar.phar /usr/local/php/bin
ln -s -f phar.phar /usr/local/php/bin/phar
cp /usr/local/php/etc/php-fpm.conf.default/usr/local/php/etc/php-fpm.con
为了让php-fpm已服务的形式启动。我们需要复制php安装目录下/sapi/fpm/init.d.php-fpm文件。如下:
cp./sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
后期扩展:
将opcache 作为一个扩展安装
将zip作为php的扩展安装
配置nginx,使其支持php,如下:
location ~ /.php$ {
root /usr/local/nginx/html;
fastcgi_pass127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME$document_root$fastcgi_script_name;
include fastcgi_params;
}
然后在nginx的网站根目录下新建index.php文件内容如下:
vi/usr/local/nginx/html/index.php
<?php
echo phpinfo();
?>
通过http://IP/index.php进行访问
(1)./configure时报错:mcrypt.h not found. Pleasereinstall libmcrypt
解决方法:apt-get install libmcrypt-dev
或者
cd /usr/local/src
wget http://softlayer.dl.sourceforge.net/sourceforge/mcrypt/libmcrypt-2.5.8.tar.gz
tar -zxvf libmcrypt-2.5.8.tar.gz
cd /usr/local/src/libmcrypt-2.5.8
./configure --prefix=/usr/local
make
make install
(2)nginx/php-fpm访问php文件直接下载而不运行
一、网站根目录
默认是在 /usr/local/nginx/html文件
配置在
location / {
root /home/www/wwwroot;
index index.html index.htm;
}
二、修改文件中对应的php配置部分
location ~ \.php$ {
root /home/www/wwwroot;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
特别需要注意的是:fastcgi_param这个参数默认的是$fastcgi_script_name;最好改 为$document_root$fastcgi_script_name;我在实际配置中出现了php找不到需要解析文件而返回404或者500错误的问题。所以最好是带上网站根目录的路径变量$document_root