详解CentOS 7.0源码包搭建LNMP 实际环境搭建

2019-10-13 13:12:24刘景俊

1、重命名:/etc/nginx/nginx.conf

mv /etc/nginx/nginx.conf /etc/nginx/nginx.conf.back

2、新建/etc/nginx/nginx.conf

cat > /etc/nginx/nginx.conf << EOF
user nginx;
worker_processes 1;
 
error_log /var/log/nginx/error.log warn;
pid  /var/run/nginx.pid;
 
 
events {
 worker_connections 1024;
}
 
 
http {
 include  /etc/nginx/mime.types;
 default_type application/octet-stream;
 
 log_format main '$remote_addr - $remote_user [$time_local] "$request" '
      '$status $body_bytes_sent "$http_referer" '
      '"$http_user_agent" "$http_x_forwarded_for"';
 
 access_log /var/log/nginx/access.log main;
 
 sendfile  on;
 #tcp_nopush  on;
 
 keepalive_timeout 65;
 
 #gzip on;
 
 include /etc/nginx/conf.d/*.conf;
}
EOF

3、创建/etc/nginx/conf.d

mkdir -p /etc/nginx/conf.d

4、创建支持php-fpm web nginx配置

cat > /etc/nginx/conf.d/default.conf << EOF
server {
  listen  80;
  server_name localhost;
 
  #charset koi8-r;
 
  #access_log logs/host.access.log main;
 
  location / {
   root /usr/local/nginx/html;
   index index.php index.html index.htm;
  }
 
  #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 {
   root 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$ {
   root   /usr/local/nginx/html;
   fastcgi_pass 127.0.0.1:9000;
   fastcgi_index index.php;
   fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
   include  fastcgi_params;
  }
  location ~* ^.+.(jpg|jpeg|gif|png|bmp)$ {
   access_log off;
   root  opencart;
   expires  30d;
      break;
  }
}
EOF

5、创建nginx启动脚本

vim /etc/init.d/nginx

# chkconfig: 2345 10 90
# description: Start and Stop nginx
 
PATH=/usr/local/bin:/sbin:/usr/bin:/bin
 
EXEC=/usr/sbin/nginx
PIDFILE=/var/run/nginx.pid
CONF="/etc/nginx/nginx.conf"
AUTH="1234"
 
case "$1" in
  start)
    if [ -f $PIDFILE ]
    then
      echo "$PIDFILE exists, process is already running or crashed."
    else
      echo "Starting nginx server..."
      $EXEC -c $CONF &
    fi
    if [ "$?"="0" ]
    then
      echo "nginx is running..."
    fi
    ;;
  stop)
    if [ ! -f $PIDFILE ]
    then
      echo "$PIDFILE exists, process is not running."
    else
      PID=$(cat $PIDFILE)
      echo "Stopping..."
      kill -9 $PID
      PID=$(pidof nginx)
      kill -9 $PID
      rm -rf /var/run/nginx.pid
      sleep 2
      while [ -x $PIDFILE ]
      do
        echo "Waiting for nginx to shutdown..."
        sleep 1
      done
      echo "nginx stopped"
    fi
    ;;
  reload)
 
   $EXEC -s reload
    ;;
  restart|force-reload)
    ${0} stop
    ${0} start
    ;;
  *)
    echo "Usage: /etc/init.d/nginx {start|stop|restart|force-reload|reload}" >&2
    exit 1
esac