这里有新鲜出炉的精品教程,程序狗速度看过来!
LNMP 一键安装包是一个用 Linux Shell 编写的可以为 CentOS/RadHat、Debian/Ubuntu VPS(VDS) 或独立主机安装 LNMP(Nginx、MySQL、PHP、phpMyAdmin) 生产环境的 Shell 程序。同时提供一些实用的辅助脚本如:可以帮助完成 Nginx、MySQL/MariaDB、PHP 的升级、常用缓存组件的安装、重置 MySQL root 密码、502 自动重启、日志切割等许多实用脚本。
我们常说的 LNMP 环境是指 Linux/nginx/MySQL/PHP 组合,LEMP 包是由 Linux、nginx、MariaDB/MySQL 和 PHP 组成的,那么看来 LEMP 和 LNMP 是一样的,而现在业内习惯性的称作 LEMP。这篇文章主要介绍了 CentOS 7.x 下的 LEMP 环境搭建详细教程, 需要的朋友可以参考下
最近由于项目需求,将服务器从 CentOS6 升级到 CentOS7,对应的 PHP 版本也升级到 PHP5.6。我们熟悉的有 LEMP 环境一键安装包,但是本文我们将单独安装各个组件模块,并搭建一个完整的 PHP 运行平台。
我们常说的 LNMP 环境是指 Linux/nginx/MySQL/PHP 组合,而 LEMP 是什么呢?其实 Nginx 的发音是 Engine-X = E,LEMP 包是由 Linux、nginx、MariaDB/MySQL 和 PHP 组成的,那么看来 LEMP 和 LNMP 是一样的,而现在业内习惯性的称作 LEMP。MariaDB 是一款社区支持驱动的 MySQL 数据库的分支,其功能更多性能更佳,所以我们在 CentOS7 下安装 MariaDB。CentOS7 我已经安装好了,现在只需安装 Nginx,MariaDB 和 PHP。
1、安装 Nginx
我们从 nginx 官方的 RPM 源来安装一个预构建的稳定版本的 nginx 包。
- $ sudo rpm--import http: //nginx.org/keys/nginx_signing.key
- $ sudo rpm - ivh http: //nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
- $ sudo yum install nginx
这样,nginx 就安装上了,安装完成后,nginx 不会自动启动。现在需要做的是让 nginx 自动启动,另外还要做些配置让其可以随着操作系统启动而启动。我们也需要在防火墙里打开 TCP/80 端口,以使得可以远程访问 nginx 的 web 服务。所有这些操作、设置都只需要输入如下命令就可实现。
- $ sudo systemctl start nginx $ sudo systemctl enable nginx $ sudo firewall - cmd--zone = public--add - port = 80 / tcp--permanent $ sudo firewall - cmd--reload
Nginx 已经启动了,现在来测试 nginx。nginx 在 CentOS7 下的默认文档要目录是 / usr/share/nginx/html。默认的 index.html 文件一定已经在这目录下了。让我们检测下是否可以访问到这个测试 web 页,输入 http://nginx 的 ip 地址 / 访问。
2、安装 MariaDB/MySQL
CentOS/RHEL 7 使用了 MariaDB 替代了默认的 MySQL。作为 MySQL 的简单替代品,MariaDB 保证了与 MySQL 的 API 和命令行用法方面最大的兼容性。下面是关于怎么在 CentOS7 上安装和配置 MaraDB/MySQL 的操作示例。
- $ sudo yum install mariadb - server $ sudo systemctl start mariadb $ sudo systemctl enable mariadb
在成功启动 MariaDB/MySQL 服务后,还要进行数据库的安全配置,如设置(非空)的 root 密码、删除匿名用户、锁定远程访问。执行如下代码:
- $ sudo mysql_secure_installation
根据提示设置 root 密码,以及删除匿名用户等操作。
3、安装 PHP
PHP 是 LEMP 包中一个重要的组件,它负责把存储在 MariaDB/MySQL 服务器的数据取出生成动态内容。为了 LEMP 需要,您至少需要安装上 PHP-FPM 和 PHP-MySQL 两个模块。PHP-FPM(FastCGI 进程管理器)实现的是 nginx 服务器和生成动态内容的 PHP 应用程序的访问接口。PHP-MySQL 模块使 PHP 程序能访问 MariaDB/MySQL 数据库。
首先检查当前安装的 PHP 包。
- yum list installed | grep php
如果有安装的 PHP 包,先删除他们。
给 yum 安装添加源包。
- rpm - Uvh https: //mirror.webtatic.com/yum/el7/epel-release.rpm
- rpm - Uvh https: //mirror.webtatic.com/yum/el7/webtatic-release.rpm
运行 yum install。
使用 yum 命令来定制 PHP 引擎,安装一些 PHP 扩展模块包。
- yum install php56w.x86_64 php56w - cli.x86_64 php56w - common.x86_64 php56w - gd.x86_64 php56w - ldap.x86_64 php56w - mbstring.x86_64 php56w - mcrypt.x86_64 php56w - mysql.x86_64 php56w - pdo.x86_64
然后安装 PHP FPM。
- yum install php56w - fpm
最后,启动 PHP-FPM
- $ sudo systemctl start php - fpm $ sudo systemctl enable php - fpm
4、配置 LEMP,让 Nginx 支持 PHP
首先,禁用随 PHP 包安装的 httpd 服务。
- $ sudo systemctl disable httpd
接下来,配置 nginx 虚拟主机,使得 nginx 可以通过 PHP-FPM 来处理 PHP 的任务。用文本编辑器打开 / etc/nginx/conf.d/default.conf,然后按如下所示修改。
- server {
- listen 80;
- server_name localhost;
- root / usr / share / nginx / html;
- index index.php index.html index.htm;#charset koi8 - r;#access_log /
- var / log / nginx / log / host.access.log main;
- location / {}#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 {
- }
- # 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$ {
- try_files $uri = 404;
- fastcgi_pass 127.0.0.1 : 9000;
- fastcgi_index index.php;
- fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
- include fastcgi_params;
- }
- }
然后,配置 PHP, 修改 / etc/php.ini。
- cgi.fix_pathinfo = 1 date.timezone = PRC
最后,测试 nginx 是否能处理 PHP 页面。在测试之前,请确保重启 nginx 和 PHP-FPM。
- $ sudo systemctl restart nginx $ sudo systemctl restart php - fpm
创建一个叫名叫 test.php 的文件,然后写入如下内容,并放入 / usr/share/nginx/html / 目录。
- <?php phpinfo(); ?>
打开浏览器,输入 http://nginx 的 IP 地址 / test.php 。看到以下界面则 LEMP 安装完成。
以上所述是小编给大家介绍的 CentOS 7.x 下的 LEMP 环境搭建详细教程,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 phperz 网站的支持!
来源: http://www.phperz.com/article/17/0724/301193.html