Centos7 安装 NGINX PHP-FPM

以下操作均使用 root 账户进行。

  • 1.1 、安装 nginx repo

vi  /etc/yum.repos.d/nginx.repo

内容如下

[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=0
enabled=1

  • 1.2、导入Remi repository源(非必要)

rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-7.rpm

  • 1.3、更新

yum -y update

  • 2、安装nginx php-fpm

yum --enablerepo=remi,remi-php72 install nginx php-fpm php-common

  • 3、安装php模块

yum --enablerepo=remi,remi-php72 install php-opcache php-pecl-apcu php-cli php-pear php-pdo php-mysqlnd php-pgsql php-pecl-mongodb php-pecl-redis php-pecl-memcache php-pecl-memcached php-gd php-mbstring php-mcrypt php-xml

  •  4、启动nginx php-fpm

systemctl enable nginx
systemctl enable php-fpm
systemctl start nginx
systemctl start php-fpm

执行netstat -ntpl

[root@localhost ~]# netstat -ntpl
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 991/php-fpm: master
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 1212/nginx: master
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 993/sshd
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1078/master
tcp6 0 0 :::22 :::* LISTEN 993/sshd
tcp6 0 0 ::1:25 :::* LISTEN 1078/master

能看到9000 和 80 端口启动就成功了。

  • 5、配置nginx php-fpm

备份配置文件
cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.bak
cp /etc/nginx/nginx.conf.default /etc/nginx/nginx.conf

修改 /etc/nginx/conf.d/default.conf

server {
listen 80;
server_name localhost;

#charset koi8-r;
#access_log /var/log/nginx/host.access.log main;

location / {
root /usr/share/nginx/html;
index index.php index.html index.htm;
}

#error_page 404 /404.html;

# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}

# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}

# 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;
}

# deny access to .htaccess files, if Apache’s document root
# concurs with nginx’s one
#
#location ~ /\.ht {
# deny all;
#}
}

php-fpm 默认使用的账户是apache,我们需要给站点路径设置权限

chown -R apache /usr/share/nginx/html

chmod -R 777 /usr/share/nginx/html

变更配置需要重启服务

systemctl restart nginx

systemctl restart php-fpm

  • 6、防火墙配置

Centos7 默认开启了防火墙,要外部访问需要配置规则

firewall-cmd --permanent --zone=public --add-service=http

firewall-cmd --permanent --zone=public --add-service=https

firewall-cmd --reload

然后使用curl -l localhost访问或者http://ip可以看到页面输出信息.

 

Hits: 894