1. 程式人生 > >centos7 php7+nginx伺服器配置

centos7 php7+nginx伺服器配置

主機:centos7.5

  • 開啟終端:
yum update
yum install epel-release
  • 安裝nginx:
yum install nginx
systemctl start nginx
systemctl enable nginx
  • 安裝MariaDB 10:
    • 新增源:
vi /etc/yum.repos.d/MariaDB.repo
[mariadb]
name = MariaDB
baseurl = http://yum.mariadb.org/10.1/centos7-amd64
gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck=1
  • 安裝資料庫並設定密碼:
yum install MariaDB-server MariaDB-client
systemctl start mariadb  
mysql_secure_installation
systemctl enable mariadb
  • 安裝php
rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
yum install php70w

安裝php及其他拓展:

yum install php70w-xml php70w-soap php70w-xmlrpc php70w-mbstring php70w-json php70w-gd php70w-mcrypt php70w-mysql 
yum install php70w-intl php70w-tidy
yum install php70w-pecl-redis 
yum install php-pecl-mongodb
yum install php70w-fpm
yum install php70w-devel php70w-pear
yum install php70w-pecl-apcu php70w-opcache
  • 修改php.ini
vi /etc/php.ini
find cgi.fix_pathinfo=1 and then replace it with cgi.fix_pathinfo=0
vi /etc/php-fpm.d/www.conf
1. 
listen = /var/run/php-fpm/php-fpm.sock
2. 
listen.owner = nginx
listen.group = nginx
3. 
user = nginx
group = nginx
  • 開啟php-fpm:
systemctl start php-fpm
Enable it to boot
systemctl enable php-fpm
  • 配置nginx配置檔案
vi /etc/nginx/conf.d/default.conf
server {
    listen       80;
    server_name  localhost;

    # note that these lines are originally from the "location /" block
    root   /usr/share/nginx/html;
    index index.php index.html index.htm;

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

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

如果你發現存在permission denied等問題

chown nginx:nginx /var/run/php-fpm/php-fpm.sock
chown nginx:nginx -R /usr/share/nginx/html
firewall-cmd --permanent --zone=public --add-service=http
firewall-cmd --permanent --zone=public --add-service=https
firewall-cmd --reload