CentOS 7从零开始安装Nginx + PHP

了解概念

nginx fastcgi php-fpm的关系梳理 - talk_is_cheap - 博客园CGI(Common Gateway Interface)CGI全称是“公共网关接口”(Common Gateway Interface),HTTP服务器与你的或其它机器上的程序进行“交谈”的一种工具https://www.cnblogs.com/smallrookie/p/5634054.html

安装Nginx

## 更新yum
sudo yum update
sudo yum autoremove apache
sudo yum autoremove apache2
sudo yum autoremove apache-server

## nginx源码编译需要gcc支持
sudo yum -y install gcc
gcc -v

## 依赖包:nginx 的 http 模块使用 pcre 来解析正则表达式
## 依赖包:nginx 使用zlib对http包的内容进行gzip
## 依赖包:使用openssl开启https支持
sudo yum -y install pcre pcre-devel
sudo yum -y install zlib zlib-devel
sudo yum -y install openssl openssl-devel

## 安装wget工具
sudo yum -y install wget

## 接下来,要访问nginx官网 https://nginx.org/en/download.html
## 找到稳定版Stable version的	类似nginx-1.20.1的链接,复制好
## 例如 https://nginx.org/download/nginx-1.20.1.tar.gz
mkdir tmp
cd tmp
## 这里要修改为最新的下载链接
wget https://nginx.org/download/nginx-1.20.1.tar.gz
tar -zxvf nginx-1.20.1.tar.gz 
ls
cd nginx-1.20.1/
./configure
make
sudo make install

## 安装完毕,看看nginx放在哪
whereis  nginx


## 默认的启动、停止方式
启动) sudo /usr/local/nginx/sbin/nginx
停止) sudo /usr/local/nginx/sbin/nginx -s stop

启动后再检查) curl 'http://localhost'
## 即可看到Welcome to Nginx! 开启成功!


## 推荐以下更方便的管理方式 + Nginx自启动:
sudo /usr/local/nginx/sbin/nginx -s stop
sudo touch /etc/init.d/nginx
sudo vi /etc/init.d/nginx
===== 添加以下内容(共108行) ========================================
#!/bin/bash
#
# Startup script for Nginx - this script starts and stops the nginx daemon
#
# 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"

[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx

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
   sleep 1
   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
===========================================================
## 以上内容来自 https://blog.csdn.net/weixin_34144848/article/details/92748920,感谢!

## 设置脚本权限
sudo chmod 755 /etc/init.d/nginx
sudo chmod +x /etc/init.d/nginx

## 设置系统服务
sudo chkconfig --add nginx
sudo chkconfig nginx on

## 方便的nginx管理命令
sudo service nginx start
sudo service nginx stop
sudo service nginx status

## 再次检查是否ok
sudo service nginx start
curl 'http://localhost'
## 即可看到Welcome to Nginx!


配置Nginx

cd /usr/local/nginx/conf/
sudo cp -p nginx.conf nginx.conf.origin-backup

## 配置nginx虚拟机列表
sudo vi nginx.conf

====== 在http{}里面为不同的网站添加不同的server ========
server {
  listen 80;
  server_name zzzzz.com;
  access_log  /var/log/nginx/access/zzzzz.com.log;
  location / {
    root /var/www/sites/zzzzz.com/;
    index index.php index.html index.htm;
  }
}
=====================================================

sudo mkdir -p /var/log/nginx/access/
sudo touch /var/log/nginx/access/zzzzz.com.log

## 重启nginx
sudo service nginx restart

## 修改HOSTS
sudo vi /etc/hosts
========= 把zzzzz.com追加到"127.0.0.1"那行 ============
127.0.0.1   localhost localhost.localdomain zzzzz.com
======================================================

## 测试内容
sudo mkdir -p /var/www/sites/zzzzz.com/
sudo vi /var/www/sites/zzzzz.com/index.html
## 随便插入点内容
## 访问浏览器,检查zzzzz.com是否可用

安装配置FastCGI

感谢 centos7下的基于nginx的fastcgi环境搭建及样例测试_chuanglan的专栏-CSDN博客_centos7 fastcgi1. 下载nginx安装包wgethttp://nginx.org/download/nginx-1.11.12.tar.gz2.解压,gunzipnginx-1.11.12.tar.gztar -xfnginx-1.11.12.tar.gz3.进入nginx-1.11.12文件夹cd nginx-1.11.124. 执行命令 ./configurehttps://blog.csdn.net/chuanglan/article/details/69063624

## 部署fastcgi
## 1. 安装fastcgi进程管理器spawn-cgi
wget http://good-good-study.rudon.cn/download/spawn-fcgi-1.6.3.tar.bz2
tar -jxf spawn-fcgi-1.6.3.tar.bz2
cd spawn-fcgi-1.6.3
./configure 
make
sudo make install
spawn-fcgi -v 
## 进行检测是否安装成功

## 2. 安装fcgi库
wget http://good-good-study.rudon.cn/download/fcgi-2.4.0.tar.gz
tar -xf fcgi-2.4.0.tar.gz
cd fcgi-2.4.0
./configure
make
sudo make install


## 查看9000端口(FastCGI、php-npm)
sudo netstat -anp | grep 9000

题外话:lighttpd.net上面找不到spawn-fcgi下载链接了

安装PHP

默认的yum -y install php会安装老版本的php5,需要用以下命令


## 参考 https://www.jianshu.com/p/1e23aba0a164

## 清空
php -v
sudo yum -y remove php*

## 设置php的源
## Remi 软件源:主要提供最新版的 PHP 软件包和其他一些 PHP 扩展工具包,它是针对 Fedora 和 RHEL 系分支变体 (包括:RHEL、CentOS、Oracle Linux 等等) 要安装 PHP,推荐使用 Remi 软件源。Remi 对 PHP 的支持和更新都很积极,可以在第一时间获得新版本的支持。
sudo yum install epel-release -y
sudo rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-7.rpm



## 查看有什么版本可用
yum list php*
yum list php* | grep php7

## 目标PHP 7.4
sudo yum --enablerepo=remi-php74 -y install php php-fpm


## ======= 自动安装的依赖 ========
## Dependencies Resolved
## php                    x86_64             7.4.25-1.el7.remi
## libsodium              x86_64             1.0.18-1.el7     
## php-cli                x86_64             7.4.25-1.el7.remi
## php-common             x86_64             7.4.25-1.el7.remi
## php-json               x86_64             7.4.25-1.el7.remi
## php-sodium             x86_64             7.4.25-1.el7.remi
## 

php -v
## PHP 7.4.25 (cli) (built: Oct 19 2021 15:18:10) ( NTS )
## Copyright (c) The PHP Group
## Zend Engine v3.4.0, Copyright (c) Zend Technologies

激活php-fpm(大坑)

[rudon@localhost ~]$ sudo systemctl status php-fpm.service
[sudo] password for rudon: 
● php-fpm.service - The PHP FastCGI Process Manager
   Loaded: loaded (/usr/lib/systemd/system/php-fpm.service; disabled; vendor preset: disabled)
   Active: inactive (dead)
[rudon@localhost ~]$ locate php-fpm
locate: can not stat () `/var/lib/mlocate/mlocate.db': No such file or directory
[rudon@localhost ~]$ 
[rudon@localhost ~]$ sudo updatedb
[rudon@localhost ~]$ locate php-fpm
/etc/php-fpm.conf
/etc/php-fpm.d
/etc/logrotate.d/php-fpm
/etc/php-fpm.d/www.conf
/etc/sysconfig/php-fpm
/etc/systemd/system/php-fpm.service.d
/usr/lib/systemd/system/php-fpm.service
/usr/sbin/php-fpm
/usr/share/doc/php-fpm-7.4.25
/usr/share/doc/php-fpm-7.4.25/README
/usr/share/doc/php-fpm-7.4.25/httpd-php.conf
/usr/share/doc/php-fpm-7.4.25/nginx-fpm.conf
/usr/share/doc/php-fpm-7.4.25/nginx-php.conf
/usr/share/doc/php-fpm-7.4.25/php-fpm.conf.default
/usr/share/doc/php-fpm-7.4.25/www.conf.default
/usr/share/licenses/php-fpm-7.4.25
/usr/share/licenses/php-fpm-7.4.25/fpm_LICENSE
/usr/share/man/man8/php-fpm.8.gz
/var/log/php-fpm
[rudon@localhost ~]$ 
[rudon@localhost ~]$ 
[rudon@localhost ~]$ sudo /usr/sbin/php-fpm
[04-Nov-2021 08:12:06] ERROR: Unable to create the PID file (/run/php-fpm/php-fpm.pid).: No such file or directory (2)
[04-Nov-2021 08:12:06] ERROR: FPM initialization failed
[rudon@localhost ~]$ 
[rudon@localhost ~]$ 
[rudon@localhost ~]$ sudo touch /run/php-fpm/php-fpm.pid
touch: cannot touch ‘/run/php-fpm/php-fpm.pid’: No such file or directory
[rudon@localhost ~]$ 
[rudon@localhost ~]$ ll /run/php-fpm/php-fpm.pid
ls: cannot access /run/php-fpm/php-fpm.pid: No such file or directory
[rudon@localhost ~]$ ll /run/php-fpm/
ls: cannot access /run/php-fpm/: No such file or directory
[rudon@localhost ~]$ ll /run/
total 56
drwxr-xr-x.  2 root           root            100 Nov  4 08:09 abrt
-rw-------.  1 root           root             11 Nov  4 08:09 alsactl.pid
.....
.....
[rudon@localhost ~]$ sudo mkdir -p /run/php-fpm/
[rudon@localhost ~]$ 
[rudon@localhost ~]$ sudo /usr/sbin/php-fpm
[rudon@localhost ~]$ 
[rudon@localhost ~]$ sudo systemctl status php-fpm.service
[sudo] password for rudon: 
● php-fpm.service - The PHP FastCGI Process Manager
   Loaded: loaded (/usr/lib/systemd/system/php-fpm.service; disabled; vendor preset: disabled)
   Active: inactive (dead)
[rudon@localhost ~]$ 
[rudon@localhost ~]$ 



没有报错就先这样吧。

配置PHP + Nginx

php -r 'phpinfo();' | grep 'php.ini'
## 查看php.ini的位置

sudo vi /etc/php.ini
## 该修改的修改,例如短标签的支持,内存大小等

## 开始修改nginx配置,使其支持php
cd /usr/local/nginx/conf/
sudo vi nginx.conf

## 每个站点的server{}配置,都要增加一段 location ~ \.php$ {}设置,如下:
================  例子 ================
server {
  listen 80;
  server_name zzzzz.com;
  access_log  /var/log/nginx/access/zzzzz.com.log;
  location / {
    root /var/www/sites/zzzzz.com/;
    index index.php index.html index.htm;
  }
  location ~ \.php$ {
    root           /var/www/sites/zzzzz.com/;
    fastcgi_pass   127.0.0.1:9000;
    fastcgi_index  index.php;
    fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
    include        fastcgi_params;
  }
}
=======================================

## 修改完毕,保存,重启nginx
sudo service nginx restart

## 访问 http://zzzzz.com/index.php(其内容为

特别鸣谢

https://segmentfault.com/a/1190000017789890icon-default.png?t=LA46https://segmentfault.com/a/1190000017789890

CentOS 7从零开始安装Nginx + PHP_第1张图片

 

你可能感兴趣的:(OS,运维,开发,nginx,centos,php,php-fpm,FastCGI)