LNMP环境安装Nginx
Nginx服务器安装和配置1、安装pcre软件包
# rpm -qa | grep pcre
# yum -y install pcre
2、创建nginx用户
# useradd -s /sbin/nologin nginx
# passwd nginx
Changing password for user nginx.
New password:
BAD PASSWORD: it is too short
BAD PASSWORD: is too simple
Retype new password:
passwd: all authentication tokens updated successfully.
3、nginx编译参数
--user 指定启动程序所属用户--group 指定组--prefix 指定安装路径--sbin-path 设置nginx二进制文件的路径名--conf-path 指定配置文件路径--error-log-path 错误日志文件路径--http-log-path 指定访问日志文件路径--http-client-body-temp-path 设置存储HTTP客户端请求主体的临时文件路径--http-proxy-temp-path 设置存储HTTP代理临时文件的路径--http-fastcgi-temp-path 设置存储HTTP fastcgi的临时文件的路径--pid-path 设置nginx.pid文件路径--lock-path 设置nginx.lock文件路径--with-openssl 启用SSL--with-pcre 启用正则表达式--with-http_stub_status_module 安装可以监控nginx状态的模块--with-http_ssl_module 启用SSL支持--with-http_gzip_static_module 启用gzip压缩
4、安装nginx
# ./configure \
> --user=nginx \
> --group=nginx \
> --prefix=/usr/local/nginx \
> --sbin-path=/usr/sbin/nginx \
> --conf-path=/etc/nginx/nginx.conf \
> --error-log-path=/var/log/nginx/error.log \
> --http-log-path=/var/log/nginx/access.log \
> --http-client-body-temp-path=/tmp/nginx/client_body \
> --http-proxy-temp-path=/tmp/nginx/proxy \
> --http-fastcgi-temp-path=/tmp/nginx/fastcgi \
> --pid-path=/var/run/nginx.pid \
> --lock-path=/var/lock/subsys/nginx \
> --with-http_stub_status_module \
> --with-http_ssl_module \
> --with-http_gzip_static_module \
> --with-pcre \
> --with-http_realip_module \
> --with-http_sub_module
# make && make install
# ls /usr/local/nginx/
html
检查配置
# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: mkdir() "/tmp/nginx/client_body" failed (2: No such file or directory)
nginx: configuration file /etc/nginx/nginx.conf test failed
# mkdir /tmp/nginx/client_body -p
# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
启动nginx
# nginx
# netstat -tlnp |grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 4554/nginx
5、查看nginx版本及编译参数
# nginx -V
nginx version: nginx/1.9.6
built by gcc 4.4.7 20120313 (Red Hat 4.4.7-16) (GCC)
built with OpenSSL 1.0.1e-fips 11 Feb 2013
TLS SNI support enabled
configure arguments: --user=nginx --group=nginx --prefix=/usr/local/nginx --sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --http-client-body-temp-path=/tmp/nginx/client_body --http-proxy-temp-path=/tmp/nginx/proxy --http-fastcgi-temp-path=/tmp/nginx/fastcgi --pid-path=/var/run/nginx.pid --lock-path=/var/lock/subsys/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --with-pcre --with-http_realip_module --with-http_sub_module
6、控制Nginx服务器
启动:nginx停止:nginx -s stop退出:nginx -s quit重新打开:nginx -s reopen重新加载配置:nginx -s reload
7、配置nginx启动脚本
# vim /etc/init.d/nginx
#!/bin/bash
# chkconfig: - 30 21
# description: http service.
# Source Function Library
. /etc/init.d/functions
# Nginx Settings
NGINX_SBIN="/usr/sbin/nginx"
NGINX_CONF="/etc/nginx/nginx.conf"
NGINX_PID="/var/run/nginx.pid"
RETVAL=0
prog="Nginx"
#Source networking configuration
. /etc/sysconfig/network
# Check networking is up
[ ${NETWORKING} = "no" ] && exit 0
[ -x $NGINX_SBIN ] || exit 0
start() {
echo -n $"Starting $prog: "
touch /var/lock/subsys/nginx
daemon $NGINX_SBIN -c $NGINX_CONF
RETVAL=$?
echo
return $RETVAL
}
stop() {
echo -n $"Stopping $prog: "
killproc -p $NGINX_PID $NGINX_SBIN -TERM
rm -rf /var/lock/subsys/nginx /var/run/nginx.pid
RETVAL=$?
echo
return $RETVAL
}
reload(){
echo -n $"Reloading $prog: "
killproc -p $NGINX_PID $NGINX_SBIN -HUP
RETVAL=$?
echo
return $RETVAL
}
restart(){
stop
start
}
configtest(){
$NGINX_SBIN -c $NGINX_CONF -t
return 0
}
case "$1" in
start)
start
;;
stop)
stop
;;
reload)
reload
;;
restart)
restart
;;
configtest)
configtest
;;
*)
echo $"Usage: $0 {start|stop|reload|restart|configtest}"
RETVAL=1
esac
exit $RETVAL
设置可执行权限
# chmod 755 /etc/init.d/nginx
添加开机启动
# chkconfig --add nginx
# chkconfig nginx on
开启
# service nginx start
Starting Nginx:
停止
# service nginx stop
Stopping Nginx:
重启
# service nginx restart
Stopping Nginx:
Starting Nginx:
重新加载配置
# service nginx reload
Reloading Nginx:
8、配置nginx服务器php解析
# vim /etc/nginx/nginx.conf
打开php配置:
65 location ~ \.php$ {
66 root html;
67 fastcgi_pass 127.0.0.1:9000;
68 fastcgi_indexindex.php;
69 fastcgi_paramSCRIPT_FILENAME/usr/local/nginx/html$fastcgi_script_name;
70 include fastcgi_params;
71 }
usernginx;
worker_processes8;
error_log/var/log/nginx/error.log;
pid /var/run/nginx.pid;
events {
worker_connections204800;
}
log_formatmain'$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.logmain;
sendfile on;
tcp_nopush on;
charset utf-8;
php-fpm启动报错
Starting php-fpm ERROR: unable to bind listening socket for address '127.0.0.1:9000': Address already in use (98)
ERROR: FPM initialization failed
failed
# /usr/local/php/sbin/php-fpm -t
NOTICE: configuration file /usr/local/php/etc/php-fpm.conf test is successful
# killall php-fpm
# service php-fpm start
Starting php-fpmdone
重新加载配置
# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
# nginx -s reload
测试php解析
# vim /usr/local/nginx/html/index.php
<?php
phpinfo();
?>
页:
[1]