1.编译环境
gcc gcc-c++ autoconf automake libtool make cmake
zlib zlib-devel #为nginx提供gzip模块,需要zlib库支持
openssl openssl-devel #为nginx提供ssl功能
pcre pcre-devel #为支持地址重写rewrite功能
pcre可通过源码编译安装
2.创建nginx的用户和组
groupadd nginx
useradd -g nginx -M nginx
#禁用它的ssh登陆权限
vi /etc/passwd
#找到nginx,将后面的/bin/bash改为/sbin/nologin
3.编译安装nginx
- 自行在官网下载最新版本nginx源码包
wget http://nginx.org/download/nginx-1.11.4.tar.gz
- 解压并进入目录
tar zxvf nginx-1.11.4.tar.gz
cd nginx-1.11.4
- 配置参数
./configure --prefix=/data/app/nginx \
--pid-path=/data/app/nginx/run/nginx.pid \
--with-http_ssl_module \
--with-http_stub_status_module \
--user=nginx \
--group=nginx \
--with-pcre \
--without-mail_pop3_module \
--without-mail_imap_module \
--without-mail_smtp_module
- 安装
make
make test
make install
#make的地方有一个小技巧,如果服务器是双核,可以通过-j2来指定用双核进行编译,-j4代表4核编译
4.服务器载入
/data/app/nginx/sbin/nginx -s raload
#如果想用service nginx start来运行的话,要在/etc/init.d/下面加入nginx启动程序
touch /etc/init.d/nginx
chmod +x /etc/init.d/nginx
chkconfig --add nginx
chkconfig nginx on
vi /etc/init.d/nginx
#然后写入如下脚本代码
nginx启动脚本
#!/bin/bash
# nginx Startup script for the Nginx HTTP Server
# this script create it by Saway at 2016.10.11.
# processname: nginx
nginxd=/data/app/nginx/sbin/nginx
nginx_config=/data/app/nginx/conf/nginx.conf
nginx_pid=/data/app/nginx/run/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 $nginx_pid
}
#reload nginx service functions.
reload(){
echo -n $"Reloading $proc:"
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
5.nginx代理php
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /$document_root$fastcgi_script_name;
include fastcgi_params;
}
举个栗子:
server {
listen 80;
server_name www.******.com;
index index.php index.html index.htm index.jsp;
root /data/www/laravel/public;
location / {
if (!-e $request_filename) {
rewrite ^(.*)$ /index.php/$1 last;
break;
}
}
location ~ \.php($|/){
set $script $uri;
set $path_info "";
if ($uri ~ "^(.+?\.php)(/.+)$") {
set $script $1;
set $path_info $2;
}
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi.conf;
fastcgi_param SCRIPT_FILENAME $document_root$script;
fastcgi_param SCRIPT_NAME $script;
fastcgi_param PATH_INFO $path_info;
# include fastcgi_params;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
}
location ~ .*\.(js|jpg|JPG|jpeg|JPEG|css|bmp|gif|GIF|mp3|ico|wav|WAV|swf|SWF|png)$
{
expires 7d;
}
}
网友评论