分类目录归档:Nginx

PHP7.0+Nginx-1.8.0禁止跨虚拟主机执行脚本

在你的Nginx每个虚拟主机的Server中写如下代码(貌似PHP5.3以上版本就支持了,不过我是7.0的版本)

#PHP Prohibit cross-Hosting
fastcgi_param  PHP_VALUE  "open_basedir=$document_root:/tmp/";

当然,你也可以把这段代码放在Conf的fastcgi.conf或者fastcgi_params里,这要看你在Server中include了哪个文件。

启动smaba后Nginx出现open() XXX failed (11: Resource temporarily unavailable)

该问题出现的症状是,当在启用smb的服务器上修改css或者js文件时,第一次刷新Nginx出现500错误,往后刷新却返回正常

查看Nginx日志发现所有资源错误为open() “资源路径 failed (11: Resource temporarily unavailable)

解决方案:请在smb的配置文件上加上,重启即可

oplocks = no
level2 oplocks = no

Nginx访问PHP文件的File not found错误处理,两种情况

这个错误很常见,原有有下面两种几种

1. php-fpm找不到SCRIPT_FILENAME里执行的php文件

2. php-fpm不能访问所执行的php,也就是权限问题

第一种情况

可以在你的location php 里面添加当文件不存在时返回404而不是交给php-fpm进行处理

location ~ \.php$
{
	...
	#文件不存在转404
	try_files	$uri = 404;
	...
}

然后,在你的配置文件中找到下面这段

fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;

替换成下面

fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;

然后重新加载nginx配置文件

/etc/init.d/nginx reload

第二种情况

两种解决方法:
第一种,就是把你root文件夹设为其他用户允许
第二种,找到你的php-fpm的配置文件,找到下面这段,把apache替换成你要的用户组

; RPM: apache Choosed to be able to access some dir as httpd
user = apache
; RPM: Keep a group allowed to write in log dir.
group = apache

CentOS的Nginx下安装高版本php+mysql

1. 首先安装 Remi Repository

## Install Remi & Epel Repository on RHEL/CentOS 6.4-6.0 - 32 Bit ##
rpm -Uvh http://download.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarch.rpm
rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-6.rpm 

## Install Remi & Epel Repository on RHEL/CentOS 6.4-6.0 - 64 Bit ## (6.5也可用)
rpm -Uvh http://download.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-6.rpm 

## Install Remi Repository on RHEL/CentOS 5.9-5.0 - 32 Bit ##
rpm -Uvh http://dl.fedoraproject.org/pub/epel/5/i386/epel-release-5-4.noarch.rpm
rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-5.rpm

## Install Remi Repository on RHEL/CentOS 5.9-5.0 - 64 Bit ##
rpm -Uvh http://dl.fedoraproject.org/pub/epel/5/x86_64/epel-release-5-4.noarch.rpm
rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-5.rpm

2. 安装mysql

yum --enablerepo=remi,remi-test install mysql mysql-server

3. 配置mysql的root密码并开机启用mysql,这里密码我用123456

service mysqld start
/usr/bin/mysqladmin -u root password '123456'
chkconfig mysqld on

4. 安装php,php-fpm和php扩展

yum --enablerepo=remi,remi-test install php php-fpm php-common php-mysql php-gd php-mbstring php-xml

5. 启动php-fpm并设置为开机启动

service php-fpm start
chkconfig php-fpm on

回到你nginx的配置文件,也就是nginx.conf,如果你不是编译安装的也有可能是/etc/nginx/conf.d/的default.conf,把下面这部分的#号(注释)都去掉。

location ~ \.php$ {
    root           html;
    fastcgi_pass   127.0.0.1:9000;
    fastcgi_index  index.php;
    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    include        fastcgi_params;
}

然后重新加载nginx配置文件

/etc/init.d/nginx reload

注意:

这时如果你访问php文件出现file not found错误,请返回nginx配置文件,将下面这部分的文字

fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;

替换成下面

fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;

然后重新加载nginx配置文件,刷新浏览即可恢复正常

/etc/init.d/nginx reload

提示:

如果想让conf包含其他的conf,可以写追加

include /opt/nginx/conf.d/*.conf;

CentOS编译安装nginx-1.8.0

首先去http://nginx.org/en/download.html下载nginx-1.8.0,这里我下载到opt目录

后面我要设置nginx使用nginx用户和nginx用户组,所以执行下面命令

groupadd nginx
useradd -g nginx -s /sbin/nologin -M nginx

接下来安装编译环境

yum -y install pcre-devel
yum -y install gcc gcc-c++ ncurses-devel perl
yum -y install zlib zlib-devel
yum -y install openssl openssl-devel

然后解压文件准备编译

cd /opt
tar -zxvf nginx-1.8.0.tar.gz
cd nginx-1.8.0

开始编译和安装

./configure --user=nginx --group=nginx --prefix=/opt/nginx --with-http_stub_status_module  --with-http_ssl_module  --with-http_flv_module --with-http_gzip_static_module
make
make install

编译完成之后,测试安装效果,出现版本号为安装成功

cd /opt/nginx/sbin
./nginx -v

QQ截图20150720002918

将以下脚本保存为nginx文件就在/etc/init.d目录下,注意如果你用外部编辑器请确认换行符为LINUX的,编码正确,其中/opt/nginx/请替换成你的安装目录

#!/bin/bash
# nginx Startup script for the Nginx HTTP Server
# this script create it by jackbillow at 2007.10.15.
# it is v.0.0.2 version.
# if you find any errors on this scripts,please contact jackbillow.
# and send mail to jackbillow at gmail dot com.
#
# chkconfig: - 85 15
# description: Nginx is a high-performance web and proxy server.
#              It has a lot of features, but it's not for everyone.
# processname: nginx
# pidfile: /usr/local/nginx/logs/nginx.pid
# config: /usr/local/nginx/conf/nginx.conf
 
nginxd=/opt/nginx/sbin/nginx
nginx_config=/opt/nginx/conf/nginx.conf
nginx_pid=/opt/nginx/logs/nginx.pid
 
RETVAL=0
prog="nginx"
 
# Source function library.
. /etc/rc.d/init.d/functions
 
# Source networking configuration.
. /etc/sysconfig/network
 
# Check that networking is up.
[ ${NETWORKING} = "no" ] && exit 0
 
[ -x $nginxd ] || exit 0
 
 
# Start nginx daemons functions.
start() {
 
if [ -e $nginx_pid ];then
   echo "nginx already running...."
   exit 1
fi
 
   echo -n $"Starting $prog: "
   daemon $nginxd -c ${nginx_config}
   RETVAL=$?
   echo
   [ $RETVAL = 0 ] && touch /var/lock/subsys/nginx
   return $RETVAL
 
}
 
 
# Stop nginx daemons functions.
stop() {
        echo -n $"Stopping $prog: "
        killproc $nginxd
        RETVAL=$?
        echo
        [ $RETVAL = 0 ] && rm -f /var/lock/subsys/nginx /usr/local/nginx/logs/nginx.pid
}
 
 
# reload nginx service functions.
reload() {
 
    echo -n $"Reloading $prog: "
    #kill -HUP `cat ${nginx_pid}`
    killproc $nginxd -HUP
    RETVAL=$?
    echo
 
}
 
# See how we were called.
case "$1" in
start)
        start
        ;;
 
stop)
        stop
        ;;
 
reload)
        reload
        ;;
 
restart)
        stop
        start
        ;;
 
status)
        status $prog
        RETVAL=$?
        ;;
*)
        echo $"Usage: $prog {start|stop|restart|reload|status|help}"
        exit 1
esac
 
exit $RETVAL

然后执行下列命令,把nginx加入系统服务并设置成开机启动。如果你的配置跟我不一样请注意目录,用户名和用户组的替换

chmod 755 /etc/init.d/nginx
chkconfig --add nginx
chkconfig nginx on
chown -R nginx:nginx /opt/nginx

最后启动nginx就可以了,别忘了开放80端口

service nginx start
iptables -I INPUT -p tcp --dport 80 -j ACCEPT
service iptables save
chkconfig iptables on