CentOS 7安装LNMP(Linux+Nginx+MySQL+PHP-FPM)
1、安装MySQL5
与CentOS 6不同,CentOS 7服务器必须通过社区仓库来安装MySQL。如果像CentOS 6中用:yum install mysql
,默认就会安装 MariaDB 数据库。(说明:MariaDB数据库管理系统是MySQL的一个分支,主要由开源社区在维护,采用GPL授权许可。开发这个分支的原因之一是:甲骨文公司收购了MySQL后,有将MySQL闭源的潜在风险,因此社区采用分支的方式来避开这个风险,目前来看没有太大必要。)
MySQL的社区仓库:https://dev.mysql.com/downloads/repo/yum/,安装步骤:
wget http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm
rpm -ivh mysql-community-release-el7-5.noarch.rpm
yum update yum install mysql-server
在询问的时候键入y
确定安装。然后启动MySQL,并设置开机启动:
systemctl start mysqld # 启动 MySQL
systemctl enable mysqld # 开机启动ySMySQL
MySQL默认会绑定到地址 localhost(127.0.0.1),默认用户名是root
,密码为空。使用下面的命令修改密码:
mysql_secure_installation
2、安装Nginx
如果系统上有安装了Apache,先卸载。
service httpd stop
systemctl disable httpd
yum remove httpd
再安装 Nginx:
yum install epel-release
yum install nginx
启动 Nginx,并设置为开机启动:
systemctl start nginx
systemctl enable nginx
在浏览器中输入服务器的域名或IP地址(如:http://127.0.0.1 ),会看到如下的内容,说明安装成功:
Nginx 默认的HTML根目录是:/usr/share/nginx/html,可以修改其下的 index.html 的内容再看看效果。
为让web应用能读写HTML根目录下的目录和文件,需将其拥有者改为 nginx 用户:
chown -R nginx:nginx /usr/share/nginx/html
3、安装PHP5-FPM
PHP5 通过 PHP-FPM(FastCGI进程管理器)可以很好地与 Nginx 协同工作,PHP-FPM 针对不同规模的网站功能和性能都非常优良,尤其是高并发大型网站。
安装步骤如下:
yum install php php-mysql php-fpm
然后是配置。打开文件 /etc/php.ini,设置cgi.fix_pathinfo=0
(要先删除前面的;
注释符),如下:
[...] cgi.fix_pathinfo=0 [...]
再配置PHP-FPM。打开文件 /etc/php-fpm.d/www.conf,
-
将
127.0.0.1:9000
改为php-fpm.sock
文件 -
取消
listen.owner
和listen.group
前面的注释 -
将
user
和group
的值由apache
改为nginx
如下:
[...]
listen = /var/run/php-fpm/php-fpm.sock
[...]
listen.owner = nobody
listen.group = nobody
[...]
user = nginx group = nginx
[...]
启动 PHP-FPM,并设置为开机启动:
systemctl start php-fpm
systemctl enable php-fpm
PHP-FPM 启动之后,会生成 socket 文件 /var/run/php-fpm/php-fpm.sock
作为守护进程运行 FastCGI 服务。接下来配置 Nginx 的时候会用到这个 socket 文件。
4、配置Nginx
Nginx 的配置文件是:/etc/nginx/nginx.conf,使用 vi
打开:
vi /etc/nginx/nginx.conf
配置项非常简单,如果需要了解详细内容,
可看:https://www.nginx.com/resources/wiki/start/topics/examples/full/。下面介绍基本的配置。
首先,根据情况调整worker_processes
和keepalive_timeout
(可选):
[...] worker_processes 4; [...] keepalive_timeout 2; [...]
虚拟主机定义在 server{}
容器中,修改为如下内容:
[...]
server { listen 80; listen [::]:80 default_server;
server_name _;
root /usr/share/nginx/html; index index.php index.html index.htm;
location / { # First attempt to serve request as file, then # as directory, then fall back to displaying a 404. try_files $uri $uri/ =404;
}
error_page 404 /404.html;
location = 40x.html {
root /usr/share/nginx/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_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_index index.php;
include fastcgi_params;
}
}
[...]
关于配置的一些说明:
-
首先,我们打开两个
listen
,让 Nginx 同时侦听 IPv4 和 IPv6 的80
端口。 -
server_name _;
绑定所有的 vhost(可以指定主机名,如 www.example.com)。 -
脚本根目录 root 不变,依然是
/usr/share/nginx/html
。 -
索引首页文件 index 添加上
index.php
。 -
其中针对 PHP 很重要的部分在
location ~\.php$ {}
中。为防止零日攻击(详见:http://wiki.nginx.org/Pitfalls#Passing_Uncontrolled_Requests_to_PHP andhttp://forum.nginx.org/read.php?2,88845,page=3),该部分开头设置了try_files $uri =404;
。
保存配置文件后,检查配置文件的语法,再重新加载 Nginx:
nginx -t # 检查配置文件语法 systemctl reload nginx # 重新加载nginx
在 /usr/share/nginx/html 目录下创建文件 index.php,内容为:
<?php phpinfo(); ?>
再次访问 http://127.0.0.1,如下:
可以看到,PHP5 已经工作,根据 Server API 行看出,是通过 FPM/FastCGI 方式工作的,往下拉会看到 PHP5 加载的其他模块,包括MySQL、cURL、sqlite模块的支持。
5、PHP-FPM使用TCP连接
默认情况下,PHP-FPM 通过 /var/run/php-fpm/php-fpm.sock 文件侦听 socket。当然,也可以设置 PHP-FPM 使用 TCP 连接。打开文件 /etc/php-fpm.d/www.conf,设置 listen
值如下:
[...] ;listen = /var/run/php-fpm/php-fpm.sock listen = 127.0.0.1:9000 [...]
这样 PHP-FPM 会侦听地址 127.0.0.1
(localhost)和端口9000
,确保这个端口没有被其他程序占用。然后重新加载 PHP-FPM:
systemctl reload php-fpm
接下来,编辑 /etc/nginx/nginx.conf 文件,修改如下一行:
[...] #fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock; fastcgi_pass 127.0.0.1:9000;
[...]
然后,重载 nginx 就可以了:
sytemctl reload nginx
这样,整个LNMP环境就搭好了。
参考地址:
- How To Install Linux, Nginx, MySQL, PHP (LEMP) stack On CentOS 7:https://www.digitalocean.com/community/tutorials/how-to-install-linux-nginx-mysql-php-lemp-stack-on-centos-7
- Installing Nginx With PHP5 (And PHP-FPM) And MySQL Support (LEMP) On Ubuntu 14.04 LTS:https://www.howtoforge.com/installing-nginx-with-php5-fpm-and-mysql-on-ubuntu-14.04-lts-lemp
- How to Install MySQL on CentOS 7:https://www.linode.com/docs/databases/mysql/how-to-install-mysql-on-centos-7
- HOW TO INSTALL AND CONFIGURE NGINX ON CENTOS 7:https://www.godaddy.com/garage/tech/config/how-to-install-and-configure-nginx-on-centos-7/
版权声明:除非注明,本文由( chao )原创,转载请保留文章出处