CentOS7 安装 PHP + NGINX

一、配置安装源

yum install epel-release

IUS包:
https://dl.iuscommunity.org/pub/ius/stable/CentOS/7/x86_64/
在里面找到:

ius-release-x.x-xx.ius.centos7.noarch.rpm


epel包:
https://dl.fedoraproject.org/pub/epel/7/x86_64/Packages/e/
在里面找到:

epel-release-x-xx.noarch.rpm

用wget下载:

yum install wget  //yum安装wget命令。
wget https://dl.iuscommunity.org/pub/ius/stable/CentOS/7/x86_64/ius-release-1.0-15.ius.centos7.noarch.rpm
wget https://dl.fedoraproject.org/pub/epel/7/x86_64/Packages/e/epel-release-7-11.noarch.rpm

用rpm安装:

rpm -ivh ius-release-1.0-15.ius.centos7.noarch.rpm epel-release-7-11.noarch.rpm

新增www用户并加入www组:

useradd www  //新增用户
passwd www  //设置密码
groupadd www //新增组
useradd -g www www  //将用户www加入www组

二、安装PHP
搜索php-fpm:

yum search php-fpm
[root@localhost ~]# yum search php-fpm
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: ftp-srv2.kddilabs.jp
 * epel: mirror.hoster.kz
 * extras: ftp-srv2.kddilabs.jp
 * ius: muug.ca
 * updates: ftp-srv2.kddilabs.jp
================= N/S matched: php-fpm =================
php-fpm.x86_64 : PHP FastCGI Process Manager
php71u-fpm-httpd.noarch : Apache HTTP Server configuration for PHP-FPM
php71u-fpm-nginx.noarch : Nginx configuration for PHP-FPM
php72u-fpm-httpd.noarch : Apache HTTP Server configuration for PHP-FPM
php72u-fpm-nginx.noarch : Nginx configuration for PHP-FPM //最新是72的。

  Name and summary matches only, use "search all" for everything.

安装php-fpm:

yum install php72u-fpm  //yum会自动解决软件之间的依赖。

查看php-fpm状态:

systemctl status php-fpm
[root@localhost ~]# systemctl status php-fpm
● php-fpm.service - The PHP FastCGI Process Manager
   Loaded: loaded (/usr/lib/systemd/system/php-fpm.service; disabled; vendor preset: disabled)
   Active: inactive (dead)

启动php-fpm:

systemctl start php-fpm  //启动
systemctl enable php-fpm  //开机启动。

安装php常用的扩展:

yum install php72u-cli php72u-common php72u-gd php72u-imap php72u-mbstring php72u-mysqlnd php72u-pdo php72u-process php72u-xml php72u-xmlrpc php72u-opcache php72u-json php72u-zip php72u-pspell php72u-pecl-apcu php72u-pecl-memcached php72u-ioncube-loader php72u-mcrypt php72u-pgsql -y

修改权限:

vi /etc/php-fpm.d/www.conf
user = www
group = www

重新加载php-fpm:

systemctl reload php-fpm

安装composer:
composer是php中管理依赖的工具。
进入https://getcomposer.org/download/查看安装命令

php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
php -r "if (hash_file('sha384', 'composer-setup.php') === '48e3236262b34d30969dca3c37281b3b4bbe3221bda826ac6a9a62d6444cdb0dcd0615698a5cbe587c3f0fe57a54d8f5') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
php composer-setup.php
php -r "unlink('composer-setup.php');"

这几条命令的意思就是:下载;校验;安装;删除安装包。
在国内使用composer如果没有翻墙的话,可以使用镜像。
https://pkg.phpcomposer.com

三、安装nginx
nginx官方教程:
http://nginx.org/en/linux_packages.html#RHEL-CentOS
安装必备组件:

yum install yum-utils

新建/etc/yum.repos.d/nginx.repo,加入以下内容:

vi /etc/yum.repos.d/nginx.repo
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key

[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=1
enabled=0
gpgkey=https://nginx.org/keys/nginx_signing.key

使用nginx库:

yum-config-manager --enable nginx-mainline

安装nginx:

yum install nginx

运行nginx:

systemctl start nginx
systemctl enable nginx

开启http与https端口:

firewall-cmd --permanent --add-service=http
firewall-cmd --permanent --add-service=https
systemctl reload firewalld

配置nginx:
我的网站放在/home/www/html:

cd /home/www/
mkdir html
chmod -R 777 html
chown -R www.www html
vi /etc/nginx/nginx.conf
user  www;
vi /etc/nginx/conf.d/default.conf
    listen       80;
    server_name  localhost;

    location / {
        root   /home/www/html;
        index  index.html index.htm index.php;
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    location ~ \.php$ {
        root           html;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  /home/www/html/$fastcgi_script_name;
        include        fastcgi_params;
    }
//红色部分我改成了网站的绝对路径,之前有次没改,出现了找不到文件错误!0。o.

关闭selinux:

vi /etc/selinux/config
SELINUX=disabled

重启生效。
不关闭会出现403 Forbidden。

phpinfo已经出来啦!
phpinfo.jpg
参考博文:https://www.jianshu.com/p/999949f8fbf3

标签: CentOS, nginx, php

评论已关闭