安装nginx
如果机器上有Apache在运行 可以先把Apache关掉 或者准备让nginx监听其他端口
systemctl stop httpd
nginx下载地址
http://nginx.org/en/download.html
Mainline version 开发版
Stable version 稳定版
Legacy versions 历史版
鼠标移动到你要选择的版本超链接上点右键 复制链接地址
进入Linux终端
cd /usr/local
用wget命令跟上复制的链接地址
wget http://nginx.org/download/nginx-1.12.2.tar.gz
解压
tar -zxvf nginx-1.12.2.tar.gz
进入目录
cd nginx-1.12.2
配置
./configure
如果报错 报什么安装什么
报错: the HTTP rewrite module requires the PCRE library.
解决:yum -y install pcre-devel
报错:the HTTP gzip module requires the zlib library
解决:yum install -y zlib-devel
报错:the HTTP cache module requires md5 functions from OpenSSL library
解决:yum -y install openssl openssl-devel
编译安装
make && make install
安装完成后查找nginx路径
whereis nginx
打开配置文件
vim /usr/local/nginx/conf/nginx.conf
#这一段都是包在server{}之中 如要配置多个域名 则复制粘贴多个server{}代码块
server {
listen 80;
server_name www.fengyumeng.cn fengyumeng.cn;
root /var/www/admin;
location / {
#重写url隐藏index.php
if (!-e $request_filename) {
rewrite ^/index.php(.*)$ /index.php?s=$1 last;
rewrite ^(.*)$ /index.php?s=$1 last;
}
index index.html index.htm index.php;
}
#把下面这一段打开注释 并将其中的/scripts 修改为 $document_root
location ~ \.php$ {
root /var/www/admin;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
若要显示目录列表 可在外层的http代码块中添加这行配置
autoindex on;
配置php-fpm
vim /usr/local/php7/etc/php-fpm.d/www.conf
把里面的user group 两行 改为nobody 或者是系统中存在的用户
user = nobody
group = nobody
运行fpm 载入php.ini
/usr/local/php7/sbin/php-fpm -c /usr/local/php7/etc/php.ini
注意 如果修改了php.ini则每次需要杀掉php-fpm进程再重新启动php-fpm
ps -ef | grep php-fpm
kill -9 ***
nginx一般命令
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf //启动
/usr/local/nginx/sbin/nginx -s stop //强制停止
/usr/local/nginx/sbin/nginx -s quit //安全停止
/usr/local/nginx/sbin/nginx -s reload //平滑重启
ps -aux | grep nginx //查询进程
启动后 用浏览器输入服务器ip 回车 便可看到nginx默认欢迎页面
编写服务脚本
vim /etc/init.d/nginx
粘贴以下代码 注意路径
#!/bin/bash
# nginx Startup script for the Nginx HTTP Server
# it is v.0.0.2 version.
# 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=/usr/local/nginx/sbin/nginx
nginx_config=/usr/local/nginx/conf/nginx.conf
nginx_pid=/usr/local/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
设置权限
chmod 755 /etc/init.d/nginx
加入自启动
vi /etc/rc.local
增加一行 /usr/local/nginx/sbin/nginx
此时将nginx停止
/usr/local/nginx/sbin/nginx -s quit
换用服务命令启动
/etc/init.d/nginx start
#查询服务是否开机启动
systemctl is-enabled httpd
#将Apache移出开机启动项
systemctl disable httpd
#将nginx加入开机启动
chkconfig nginx on
#服务命令
systemctl status nginx
systemctl start nginx
systemctl stop nginx
systemctl reload nginx
systemctl restart nginx