linux教程:Ubuntu安装Nginx+PHP7+MySQL5.6

技术文章 litterfat 6年前 (2018-12-14) 5148次浏览 已收录 扫描二维码

整理得比较全的 linux教程:Ubuntu安装 Nginx+PHP7+MySQL5.6

安装 Nginx

1、首先添加 nginx_signing.key(必须,否则出错)

wget http://nginx.org/keys/nginx_signing.key
sudo apt-key add nginx_signing.key

2、添加[Nginx](http://nginx.org/)官方提供的源

echo "deb http://nginx.org/packages/ubuntu/ trusty nginx" >> /etc/apt/sources.list
echo "deb-src http://nginx.org/packages/ubuntu/ trusty nginx" >> /etc/apt/sources.list

3、更新源并安装 Nginx

sudo apt-get update
sudo apt-get install nginx

4、安装 Nginx 完成后可查看版本号,输入

$ /usr/sbin/nginx -v

安装 PHP7

1、添加 PPA

sudo apt-get install python-software-properties software-properties-common
sudo add-apt-repository ppa:ondrej/php
sudo apt-get update

2、安装 PHP7 以及所需的一些扩展

sudo apt-get install php7.0-fpm php7.0-mysql php7.0-common php7.0-curl php7.0-cli php7.0-mcrypt php7.0-mbstring php7.0-dom

3、配置 PHP7
打开 php.ini 配置文件:

$ sudo vim /etc/php/7.0/fpm/php.ini

找到 cgi.fix_pathinfo 选项,去掉注释;,然后将值设置为 0:
cgi.fix_pathinfo = 0;
display_errors = On

Nginx 配置 php 支持(注意替换路径)

安装 MySQL

安装 mysql

sudo apt-get install mysql-server-5.6 mysql-client-5.6

途中会提示设置 MySQL 的密码,安装好后:

mysql -uroot -p

然后输入刚刚设置的密码,能成功进入即成功安装。

解决 Nginx php-fpm 配置有误引起的 502 错误

在 Ubuntu+Nginx+PHP 环境下部署好以后,访问网站报错 502,在后台 nginx error_log 里看到以下报错信息

2017/07/29 10:59:15 [error] 5622#0: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 183.14.134.39, server: xx.xx.xx.xx, request: “GET /index.php HTTP/1.1”, upstream: “fastcgi://127.0.0.1:9001”, host: “xx.xx.xx.xx”

通常这个报错是表示 php-fpm 这个服务未启动,由于默认是配置的 9000 端口,执行 netstat -anp|grep 9000 确实没有看到相关进程。

但执行命令查询 php-fpm 是 running 状态

# /etc/init.d/php7.0-fpm status
● php7.0-fpm.service – The PHP 7.0 FastCGI Process Manager
Loaded: loaded (/lib/systemd/system/php7.0-fpm.service; enabled; vendor preset: enabled)
Active: active (running) since Sat 2017-07-29 11:52:47 CST; 3h 43min ago
Process: 7191 ExecStartPre=/usr/lib/php/php7.0-fpm-checkconf (code=exited, status=0/SUCCESS)
Main PID: 7200 (php-fpm7.0)
Status: “Processes active: 0, idle: 2, Requests: 54, slow: 0, Traffic: 0req/sec”
CGroup: /system.slice/php7.0-fpm.service
├─7200 php-fpm: master process (/etc/php/7.0/fpm/php-fpm.conf)
├─7203 php-fpm: pool www
└─7204 php-fpm: pool www

Jul 29 11:52:47 iZwz96f0gkw4blayus6g2yZ systemd[1]: Starting The PHP 7.0 FastCGI Process Manager…
Jul 29 11:52:47 iZwz96f0gkw4blayus6g2yZ systemd[1]: Started The PHP 7.0 FastCGI Process Manager.

查看/etc/php/7.0/fpm/pool.d/www.conf 和/etc/php/7.0/fpm/php-fpm.conf 发现以下参数:

listen = /run/php/php7.0-fpm.sock

查阅资料后才知道,原来 php-fpm 支持网络端口监听和 socket 两种方式,但后者效率更高。

针对该问题的解决方案是,修改 nginx/conf/vhosts 下的 conf 文件,

将 fastcgi_pass 127.0.0.1:9000;修改为 fastcgi_pass unix:/run/php/php7.0-fpm.sock;

重启 Nginx 服务后,WEB 访问依然报错 502,继续定位分析。

在 nginx error_log 日志中出现了以下新的报错内容:

2017/07/29 11:24:47 [crit] 6114#0: *1 connect() to unix:/run/php/php7.0-fpm.sock failed (13: Permission denied) while connecting to upstream, client: 183.14.134.xx, server: 112.74.89.xx, request: “GET / HTTP/1.1”, upstream: “fastcgi://unix:/run/php/php7.0-fpm.sock:”, host: “112.74.89.xx”

然后找到/etc/php/7.0/fpm/pool.d/www.conf 文件,做以下处理:

注释掉:


;listen.owner = www-data
;listen.group = www-data

将 mode 值修改为 0666


listen.mode = 0666

最后,
执行/etc/init.d/php7.0-fpm restart 重启 php-fpm 服务,
执行/etc/init.d/nginx restart 重启 Nginx 服务,问题解决!!!


喜欢 (2)
Searching for the best XXX images on the web? Look no further than our premier destination for all things erotic. the highest quality content on the web, with crystal clear video and audio that will make you feel like you're right there in the action https://eroticpictures.org/