Linux 配置 Nginx + PHP 7 環境


配置 Nginx

  • 系統更新
sudo yum -y install epel-release
sudo yum update -y
  • 使用 yum 安裝 nginx ,並設置開機啟動
yum install -y nginx
systemctl start nginx
systemctl enable nginx
  • 查看 nginx 版本
nginx -v
  • 查看 nginx 運行狀態
ps -ef | grep nginx

配置 PHP

  • 安裝 yum 源
rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm


  • 安裝 PHP 及組件
yum -y install php72w php72w-cli php72w-common php72w-devel php72w-embedded php72w-fpm php72w-gd php72w-mbstring php72w-mysqlnd php72w-opcache php72w-pdo php72w-xml php72w-ldap
  • 啟動 PHP 並設置為開機啟動
systemctl start php-fpm
systemctl enable php-fpm
  • 查看 PHP 版本及運行狀態
php -v
  • 查看 PHP 運行狀態
ps -ef | grep php-fpm

修改 Nginx 配置

vim /etc/nginx/conf.d/default.conf
找到第一個location中的這一行
    index  index.html index.htm;
    修改為:
    index  index.php index.html index.htm; #添加index.php
        
    把FastCGI server這行下面的location的注釋去掉,並修改成下面這樣子
     # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
     #
     location ~ \.php$ {
         root            /usr/share/nginx/html;  #網站根目錄
         fastcgi_pass   127.0.0.1:9000;
         fastcgi_index  index.php;
         fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
         include        fastcgi_params;
     }

service nginx restart   #重啟nginx
service php-fpm start   #開啟php-fpm
  • 在網站根目錄新建 index.php 文件
vim /usr/share/nginx/html/index.php
<?php
phpinfo();
?>
  • 在瀏覽器中輸入虛擬機ip,已經可以看到phpinfo的信息了


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM