1.下载nginx源码包
wget http://nginx.org/download/nginx-1.20.2.tar.gz
#查看文件的大小
ls -alh | grep nginx
2.构建nginx编译环境
#进入nginx源码包中
./configure --prefix=/usr/local/nginx --with-pcre=[pcre的目录地址]
3.编译安装
make && make install
4.进入nginx的sbin目录,准备开启服务
cd /usr/local/nginx/sbin
#关闭刚刚开启的httpd服务器
#查看httpd服务进程
ps aux | grep httpd
#然后将httpd的服务都kill 掉,-9暴力杀掉
kill -9 进程号
5.开启nginx服务
./nginx
6.测试,输入虚拟机ip
#出现Welcome to nginx 表示安装成功
7.修改相关文件的后缀
/usr/local/php7/etc/php-fpm.conf.default 文件改成 php-fpm.conf
/usr/local/php7/etc/php-fpm.d/www.conf.default 文件改成 www.conf
7.开启php-fpm服务
cd /usr/local/php7/sbin
./php-fpm
8.查看进程
#存在php-fpm进程,开启成功
ps aux | grep php-fpm
9.测试访问php文件
#建立test.php文件
/usr/local/nginx/html/test.php
#访问php文件发现是下载php文件
9.修改nginx 的配置文件
vim /usr/local/nginx/conf/nginx.conf
#然后放开 localtion 相关的php注释
#重启nginx,cd到nginx的sbin目录
./nginx -s reload
#再次访问,报错 File not found.
#继续修改nginx.conf
fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
改成
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
#重启nginx,问题解决
10.添加nginx服务
在/etc/init.d/nginx下插入以下代码
#!/bin/sh
# nginx - this script starts and stops the nginx daemin
#
# chkconfig: - 85 15
# description: Nginx is an HTTP(S) server, HTTP(S) reverse \
# proxy and IMAP/POP3 proxy server
# processname: nginx
# config: /usr/local/nginx/conf/nginx.conf
# pidfile: /usr/local/nginx/logs/nginx.pid
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0
nginx="/usr/local/nginx/sbin/nginx"
prog=$(basename $nginx)
NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"
lockfile=/var/lock/subsys/nginx
start() {
[ -x $nginx ] || exit 5
[ -f $NGINX_CONF_FILE ] || exit 6
echo -n $"Starting $prog: "
daemon $nginx -c $NGINX_CONF_FILE
retval=$?
echo
[ $retval -eq 0 ] && touch $lockfile
return $retval
}
stop() {
echo -n $"Stopping $prog: "
killproc $prog -QUIT
retval=$?
echo
[ $retval -eq 0 ] && rm -f $lockfile
return $retval
}
restart() {
configtest || return $?
stop
start
}
reload() {
configtest || return $?
echo -n $"Reloading $prog: "
killproc $nginx -HUP
RETVAL=$?
echo
}
force_reload() {
restart
}
configtest() {
$nginx -t -c $NGINX_CONF_FILE
}
rh_status() {
status $prog
}
rh_status_q() {
rh_status >/dev/null 2>&1
}
case "$1" in
start)
rh_status_q && exit 0
$1
;;
stop)
rh_status_q || exit 0
$1
;;
restart|configtest)
$1
;;
reload)
rh_status_q || exit 7
$1
;;
force-reload)
force_reload
;;
status)
rh_status
;;
condrestart|try-restart)
rh_status_q || exit 0
;;
*)
echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
exit 2
esac
cd /etc/init.d
#增加权限
chmod 755 /etc/init.d/nginx
#加入到linux系统服务
chkconfig --add nginx
#查看系统服务列表会发现有nginx服务
chkconfig --list
#成功开启nginx服务
service nginx start
#php-fpm配置文件位置
/usr/local/php7/etc/
# 重启php-fpm主进程
kill -USR2 +php-fpm主进程号