热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

linuxlocationroot访问文件夹404_Linux下yum安装PHP7.2+Nginx+Mysql

一.安装NGINX1.先查看是否安装了第三方软件库:yumlistinstalled|grepepel-release或rpm-qepel-release如果已经安装
904bd7fa890539bbbb23cce1d4e424e5.png

一.安装NGINX

1.先查看是否安装了第三方软件库:

yum list installed | grep epel-release

rpm -q epel-release

如果已经安装了则跳过这一步

yum install -y epel-release

2.安装nginx

yum install -y nginx

3.设置自启动以及启动nginx

systemctl enable nginx
systemctl start nginx

4. 查询nginx进程

ps aux|grep nginx

5.浏览器访问「成功」

8500484a99e8d2d608f3be22987c321d.png

备注: nginx配置文件:/etc/nginx nginx根目录:/usr/share/nginx/html 6.修改网站根目录 修改nginx配置

vim /etc/nginx/nginx.conf
server下修改:
root /var/www/html;

为让web应用能读写HTML根目录下的目录和文件,需将其拥有者改为 nginx 用户:

chown -R nginx:nginx html/

重启nginx

nginx -t
systemctl reload nginx

注意:如果其他主机需要访问的话且有iptables防火墙的话,需要开放80和8080端口

vim /etc/sysconfig/iptables
-A INPUT -p tcp -mstate --state NEW -m tcp --dport 22 -j ACCEPT
下面添加:
-A INPUT -p tcp -m state --state NEW -m tcp --dport 80 -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 8080-j ACCEPT

重启iptables防火墙

systemctl restart iptables


二.安装Mysql

1.安装

yum install -y mysql

2.安装mysql-server

wget http://dev.mysql.com/get/mysql-community-release-el7-5.noarch.rpm
rpm -ivh mysql-community-release-el7-5.noarch.rpm
yum install -y mysql-community-server mysql-devel

3. 设置自启动以及启动mysql

systemctl restart mysqld
systemctl enable mysqld

4. 修改密码

mysql -uroot -p
set password for 'root'@'localhost' =password('123456');

5.允许远程连接(非必要,而且最好是创建新的用户进行远程连接,不要使用root)

GRANT ALL PRIVILEGES ON . TO 'root'@'%'' IDENTIFIED BY '123456@'' WITH GRANT OPTION;
FLUSH PRIVILEGES;

6.重启mysql

systemctl restart mysqld

注意:如果要远程连接的话且有iptables防火墙的话,需要开放3306端口 vi /etc/sysconfig/iptables 增加一条:

-A INPUT -p tcp -m state --state NEW -m tcp --dport 3306 -j ACCEPT

7. 重启iptables防火墙

systemctl restart iptables


三.安装PHP7.2

1.配置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
yum install -y http://rpms.remirepo.net/enterprise/remi-release-7.rpm

2.安装

yum -y install php72w php72w-cli php72w-common php72w-devel php72w-embedded php72w-fpm php72w-gd php72w-mbstring php72w-pecl-redis php72w-mysqlnd php72w-opcache php72w-pdo php72w-xml php72w-json

3.修改php配置

vim /etc/php.ini:
date.timezone=PRCvim /etc/php-fpm.d/www.conf:
listen = /var/run/php-fpm/php-fpm.sock #本来是127.0.0.1:9000
listen.owner = nginx
listen.group = nginx
user = nginx
group = nginx

4.设置自启动以及启动php

systemctl enable php-fpm
systemctl start php-fpm

注意:PHP-FPM 启动之后,会生成 socket 文件 /var/run/php-fpm/php-fpm.sock作为守护进程运行 FastCGI 服务。 5.nginx配置

vim /etc/nginx/nginx.conf
server {listen 80;listen [::]:80 default_server;server_name localhost;root /var/www/html;index index.html index.php index.htminclude /etc/nginx/default.d/*.conf;location / {}location ~ .php$ {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;}error_page 404 /404.html;location = /40x.html {}error_page 500 502 503 504 /50x.html;location = /50x.html {}
}

6.重启nginx

systemctl restart nginx

7.测试

vi /var/www/html/info.php

8.浏览器访问 可能遇到问题1 访问php文件时报错:403 forbidden 查看nginx的error日志,显示

[error] 6830#0: *5 "/var/www/html/index.php" is forbidden (13: Permission denied)

两个可能原因:

  • 权限问题
  • SELinux状态

权限问题解决方式:

chmod -R 777 /var/www
chmod -R 777 /var/www/html

SELinux状态解决方式: 查看当前selinux的状态

/usr/sbin/sestatus

如果是enabled的话需要关闭SELinux vi /etc/selinux/config 将SELINUX=enforcing 修改为 SELINUX=disabled 状态。 重启生效

reboot

可能遇到问题2 访问PHP文件时变成下载 原因: 这是因为nginx没有设置好碰到php文件时,要传递到后方的php解释器 看看你的nginx.conf配置,里面有没有这样的设置:

location ~ .*.php$ {fastcgi_pass 127.0.0.1:9000;
}

上面的意思,就是说,碰到.php结尾的文件,传递给后方127.0.0.1的9000端口上。 当然啦,你的php-fpm解析器也需要正常运行,并监听好9000端口,才能最终生效并有效处理php脚本。



推荐阅读
author-avatar
WINGZERO哥哥
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有