配置nginx环境
配置网络
vim /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0
HWADDR=00:0C:29:BF:6C:21
TYPE=Ethernet
UUID=67c5e294-6187-4415-b363-52f3cd591806
ONBOOT=yes
NM_CONTROLLED=yes
BOOTPROTO=static
IPADDR=192.168.192.14
NETMASK=255.255.255.0
GATEWAY=192.168.192.1
重启
service network restart
配置yum源 使用网上yum源
只需要将 /etc/yum.repos.d/Base-CentOS.repo 文件的baseurl代码打开
如果报错如下
Could not retrieve mirrorlist http://mirrorlist.centos.org/?release=6&arch=i386&repo=os error was
14: PYCURL ERROR 6 - "Couldn't resolve host 'mirrorlist.centos.org'"
Could not retrieve mirrorlist http://mirrorlist.centos.org/?release=6&arch=i386&repo=extras error was
14: PYCURL ERROR 6 - "Couldn't resolve host 'mirrorlist.centos.org'"
Error: Cannot find a valid baseurl for repo: extras
解决方式
DNS服务器错误
vi /etc/resolv.conf
添加一个
search localdomain
nameserver 8.8.8.8
完成
然后
删除缓存 yum clean all
重新生成缓存
yum makecache
安装nginx
groupadd www OR /usr/sbin/groupadd www
useradd -g www www OR /usr/sbin/useradd -g www www
(1)获取nginx的安装包 wget http://nginx.org/download/nginx-1.8.0.tar.gz
(2)解压nginx的安装包
yum -y install gcc
yum -y install gcc-c++
yum -y install zlib;
yum -y install yum-fastestmirror
yum -y install pcre-devel
yum –y install pcre;
yum -y install openssl openssl-devel
yum –y install openssl
解压安装包
tar -zxvf nginx-1.8.0.tar.gz
跳转到目录
cd nginx-1.8.0
设置编译路径为 /usr/local/nginx
./configure --prefix=/usr/local/nginx
编译 :
Make
编译安装:
Make install
4.遇到的问题:
[root@localhost sbin]# ./nginx -s reload
nginx: [error] open() "/usr/local/nginx/logs/nginx.pid" failed (2: No such file or directory)
解决方法:
[root@localhost nginx]# /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
使用nginx -c的参数指定nginx.conf文件的位置
5.NGINX常用操作 启动操作
/usr/nginx/sbin/nginx (/usr/local/nginx/sbin/nginx -t 查看配置信息是否正确)
停止操作 停止操作是通过向nginx进程发送信号(什么是信号请参阅linux文 章)来进行的 步骤1:查询nginx主进程号 ps -ef | grep nginx 在进程列表里 面找master进程,它的编号就是主进程号了。 步骤2:发送信号 从容停止Nginx: kill -QUIT 主进程号 快速停止Nginx: kill -TERM 主进程号 强制停止Nginx: pkill -9 nginx
另外, 若在nginx.conf配置了pid文件存放路径则该文件存放的就是Nginx主进程号,如果没指定则放在nginx的logs目录下。有了pid文 件,我们就不用先查询Nginx的主进程号,而直接向Nginx发送信号了,命令如下: kill -信号类型 ‘/usr/local/nginx/logs/nginx.pid’
平滑重启 如果更改了配置就要重启Nginx,要先关闭Nginx再打开?不是的,可以向Nginx 发送信号,平滑重启。 平滑重启命令: kill -HUP 住进称号或进程号文件路径
或者使用
/usr/local/nginx/sbin/nginx -s reload
注意,修改了配置文件后最好先检查一下修改过的配置文件是否正 确,以免重启后Nginx出现错误影响服务器稳定运行。判断Nginx配置是否正确命令如下:
nginx -t -c /usr/local/nginx/conf/nginx.conf
或者
/usr/local/nginx/sbin/nginx -t
完整server.conf
server {
listen 80;
server_name www.b.com;
root /var/www/html;
autoindex on;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
index index.html index.htm index.php;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
6、PHP安装
wget http://cn2.php.net/distributions/php-5.5.36.tar.gz
wget ftp://ftp.gnome.org/pub/GNOME/sources/libxml2/2.6/libxml2-2.6.30.tar.gz
wget http://120.52.73.49/jaist.dl.sourceforge.net/project/mcrypt/Libmcrypt/2.5.8/libmcrypt-2.5.8.tar.gz
wget http://zlib.net/zlib-1.2.8.tar.gz
wget ftp://ftp.gnu.org/gnu/autoconf/autoconf-2.69.tar.gz
开始安装:
注意:按如下顺序安装
1.安装libxml2
cd /usr/local/src/
tar zxvf libxml2-2.6.30.tar.gz
cd libxml2-2.6.30
./configure --prefix=/usr/local/libxml2
make && make install
4.安装autoconf
cd /usr/local/src/
tar zxvf autoconf-2.69.tar.gz
cd autoconf-2.69
./configure
make && make install
yum install bzip2 bzip2-devel
yum -y install curl-devel
yum install libjpeg libpng freetype libjpeg-devel libpng-devel freetype-devel -y
yum -y install readline-devel
ok,以上过程应该不会遇到问题,接下来安装PHP。
问题从这里开始,我们一步一步说明:
cd /usr/local/src/
tar zxvf php-5.5.6.tar.gz
cd php-5.5.6
编译PHP
./configure \
--prefix=/usr/local/php7 \
--exec-prefix=/usr/local/php7 \
--with-config-file-path=/usr/local/php7/etc \
--with-curl \
--with-freetype-dir \
--with-gd \
--with-gettext \
--with-iconv-dir \
--with-kerberos \
--with-libdir=lib64 \
--with-libxml-dir \
--with-mysqli \
--with-openssl \
--with-pcre-regex \
--with-pdo-mysql \
--with-pdo-sqlite \
--with-pear \
--with-png-dir \
--with-xmlrpc \
--with-xsl \
--with-zlib \
--with-zlib-dir \
--with-mhash \
--with-openssl-dir \
--with-jpeg-dir \
--enable-gd-jis-conv \
--enable-fpm \
--enable-bcmath \
--enable-libxml \
--enable-inline-optimization \
--enable-gd-native-ttf \
--enable-mbregex \
--enable-mbstring \
--enable-opcache \
--enable-pcntl \
--enable-shmop \
--enable-soap \
--enable-sockets \
--enable-sysvsem \
--enable-xml \
--enable-maintainer-zts \
--enable-zip
./configure \ //执行当前目录下软件自带的配置命令
--prefix=/usr/local/php \ //设置PHP5 的安装路径
--with-config-file-path=/usr/local/php/etc \ //指定PHP5 配置文件存入的路径
--with-apxs2=/usr/local/apache2/bin/apxs \ //告诉PHP 查找Apache 2 的地方
--with-libxml-dir=/usr/local/libxml2/ \ //告诉PHP 放置libxml2 库的地方
--with-png-dir=/usr/local/libpng/ \ //告诉PHP 放置libpng 库的地方
--with-jpeg-dir=/usr/local/jpeg6/ \ //告诉PHP 放置jpeg 库的地方
--with-freetype-dir=/usr/local/freetype/ \ //告诉PHP 放置freetype 库的地方
--with-gd=/usr/local/gd2/ \ //告诉PHP 放置gd 库的地方
--with-zlib-dir=/usr/local/zlib/ \ //告诉PHP 放置zlib 库的地方
--with-mcrypt=/usr/local/libmcrypt/ \ //告诉PHP 放置libmcrypt 库的地方
--with-mysql \ //激活MySQL功能,缺省为mysqlnd
--with-mysqli \ //变量激活新增加的MySQLi 功能,缺省为mysqlnd
--enable-soap \ //变量激活SOAP 和Web services 支持
--enable-mbstring=all \ //使多字节字符串支持
--enable-sockets \ //变量激活socket 通信特性
--enable-pdo \ //激活pdo
--with-pdo-mysql //激活pdo-mysql,缺省为mysqlnd
--enable-fpm [重要,LINUX要配置FPM必须加这个参数]
接下来就都是重点了
问题1:
按照以上配置,make时可能报如下错误:
/usr/local/src/php-5.5.6/ext/gd/gd.c:57:22: error: X11/xpm.h: No such file or directory
make: *** [ext/gd/gd.lo] Error 1
问题说明:这是提示gd库不支持xpm
解决方法:
1.安装libxpm
yum install libXpm-devel.x86_64
2.按照上面的方法重新安装gd库
安装成功后会看到如下信息,说明gd已经支持xpm了:
** Configuration summary for libgd 2.1.0:
Support for Zlib: yes
Support for PNG library: yes
Support for JPEG library: yes
Support for VPX library: no
Support for TIFF library: no
Support for Freetype 2.x library: yes
Support for Fontconfig library: no
Support for Xpm library: yes
Support for pthreads: yes
3.再次按照上面的方法安装php:
问题2:
报错:
ext/gd/gd.c:1330: undefined reference to `XpmLibraryVersion'
问题说明:没有找到xpm库的安装位置
解决方法:
1.rpm -ql libXpm :查询出libXpm的安装位置,发现在/usr/lib64/ 下
2.重新编译php,./configure中增加 --with-xpm-dir=/usr/lib64/
3.make && make install 成功!
7、PHP和nginx整合
Php-fpm配置
修改配置文件
cd /home/likang/package/php-5.6.2
cp php.ini-production /usr/local/php/etc/php.ini
cp /usr/local/php/etc/php-fpm.conf.default.conf php-fpm.conf
保存配置文件后,检验配置是否正确的方法为:
/usr/local/php/sbin/php-fpm -t
如果出现诸如 “test is successful” 字样,说明配置没有问题。
启动php-fpm
cp /usr/local/src/php-5.3.27/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
chmod 755 /etc/init.d/php-fpm
service php-fpm start
如果想让它开机启动,执行:
chkconfig php-fpm on
检测是否启动:
ps aux |grep php-fpm
netstat -ant |grep 9000
Nginx.Conf加入:
include vhost/*.conf;
vhost文件配置示例:
server {
listen 80;
server_name _;
access_log logs/likang.default.access.log main;
error_log logs/error.log error;
root /var/www/html/likang/default;
location / {
index index.html index.htm index.php;
if (!-e $request_filename) {
#rewrite . /index.php last;
}
}
location ~ .php$ {
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME /var/www/html/likang/default/$fastcgi_script_name;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
}
}
8、添加PHP扩展 安装gd为例子说明
安装GD之前要先安装GD需要的扩展
安装需要的扩展:
yum -y install libxml2-devel libcurl-devel libjpeg-devel libpng-devel freetype-devel
1、到PHP源码包内
cd /home/package/php-5.5.6
跳转到ext目录下,看到GD之后,跳转到GD目录 [ 需要装什么扩展就到那个目录 ]
2、使用phpize 生成configure文件
/usr/local/php/bin/phpize
3、生成编译文件
./configure --with-php-config=/usr/local/php/bin/php-config
./configure --with-php-config=/usr/local/php/bin/php-config -with-png-dir --with-freetype-dir --with-jpeg-dir=/usr/local/jpeg -with-zlib-dir --with-gd
4、编译
make
make test
Do you want to send this report now? [Yns] n
5、安装
make install
6、修改配置文件
注意目录
修改php.ini文件
注意要用绝对路径extension=/usr/local/php/lib/php/extensions/no-debug-non-zts-20121212/gd.so
7、重启nginx 和 php-fpm
Configure