CentOS 7 下 安装 LEMP 服务(nginx、MariaDB/MySQL 和 php)
LEMP 组合包是一款日益流行的网站服务组合软件包,在许多生产环境中的核心网站服务上起着强有力的作用。正如其名称所暗示的, LEMP 包是由 Linux、nginx、MariaDB/MySQL 和 PHP 组成的。在传统的 LAMP 包中使用的 Apache HTTP 协议服务器性能低下而且难于大规模集群,相比来说 nginx 的高性能及轻量级等特性,正是其的替代方案。 MariaDB 是一款社区支持驱动的 MySQL 数据库的分支,其功能更多性能更佳。PHP,服务端编程语言,具体是由 PHP FastCGI 的增强版 PHP-FPM 组件来处理,生成网页动态内容。
(LCTT 译注:为何采用 LEMP 而不是 LNMP 的缩写?据 https://lemp.io/ 的解释:Nginx 的发音是 Engine-X,重要的发音而不是首字母,而且 LEMP 实际上是可读的,而 LNMP 看起来只是字母表。)
第一步: Nginx
让我们在 CentOS 上安装 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 RPM 包之前,如果您没有导入 nginx 的官方 GPG 密钥的话,会出一如下所示的警告:
warning: /var/tmp/rpm-tmp.KttVHD: Header V4 RSA/SHA1 Signature, key ID 7bd9bf62: NOKEY
启动 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 的默认文档要目录是 /usr/share/nginx/html。默认的 index.html 文件一定已经在这目录下了。让我们检测下是否可以访问到这个测试 web 页,输入 http://nginx的ip地址/ 访问。
如果能看到nginx启动页面,说明nginx已经正常。
第二步: MariaDB/MySQL
下一步就是安装 LEMP 包的数据库组件。CentOS/RHEL 6 或早期的版本中提供的是 MySQL 的服务器/客户端安装包,但 CentOS/RHEL 7 已使用了 MariaDB 替代了默认的 MySQL。作为 MySQL 的简单替代品,MariaDB 保证了与 MySQL 的 API 和命令行用法方面最大的兼容性。下面是关于怎么在 CentOS 上安装和配置 MaraDB/MySQL 的操作示例。
$ sudo yum install mariadb-server $ sudo systemctl start mariadb $ sudo systemctl enable mariadb
在成功启动 MariaDB/MySQL 服务后,执行在 MariaDB/MySQL 服务包中的脚本。这一次的运行会为为数据库服务器进行一些安全强化措施,如设置(非空)的 root 密码、删除匿名用户、锁定远程访问。
$ sudo mysql_secure_installation
(一直选择‘y’就行了,然后再进行数据库配置,添加用户,添加表,增加远程登录)
第三步: PHP
PHP 是 LEMP 包中一个重要的组件,它负责把存储在 MariaDB/MySQL 服务器的数据取出生成动态内容。为了 LEMP 需要,您至少需要安装上 PHP-FPM 和 PHP-MySQL 两个模块。PHP-FPM(FastCGI 进程管理器)实现的是 nginx 服务器和生成动态内容的 PHP 应用程序的访问接口。PHP-MySQL 模块使 PHP 程序能访问 MariaDB/MySQL 数据库。$ sudo yum php php-fpm php-mysql
在安装 PHP 时,得注意两个地方:
在 CentOS 6 系统中,安装 REMI仓库中最新的 php-mysql 模块时,MySQL 的服务端包和客户端包会被当做一部分依赖包而自动的更新。
在 CentOS 6 和 CentOS 7 中,在安装 PHP 包的同时会把 Apache web 服务器(即 httpd)当做它的依赖包一起安装。这会跟 nginx web 服务器起冲突。这个问题会在下一节来讨论。
取决于您的使用情况,可以使用 yum 命令来定制您的 PHP 引擎,也许会想安装下面的任意一个扩展 PHP 模块包。
php-cli: PHP 的命令行界面。从命令行里测试 PHP 时非常有用。php-gd: PHP 的图像处理支持。
php-bcmath: PHP 的数学支持。
php-mcrypt: PHP 的加密算法支持 (例如 DES、Blowfish、CBC、 CFB、ECB ciphers 等)。
php-xml: PHP 的 XML 解析和处理支持。
php-dba: PHP 的数据抽象层支持。
php-pecl-apc: PHP 加速器/缓存支持。
安装时,要查看可用的 PHP 模块的完整列表的话,可以运行:
$ sudo yum search php-
启动 PHP-FPM
您需要启动 PHP-FPM ,然后把它放到自动启动服务列表。$ sudo systemctl start php-fpm $ sudo systemctl enable php-fpm
第四步: 配置 LEMP 组合包
使 Httpd 不可用
首先,让我们把早先随 PHP 包安装的 httpd 服务给禁用掉。$ sudo systemctl disable httpd
配置 Nginx
接下来,让我们配置 nginx 虚拟主机,使得 nginx 可以通过 PHP-FPM 来处理 PHP 的任务。用文本编辑器打开 /etc/nginx/conf.d/default.conf ,然后按如下所示修改。server { listen 80; #server_name localhost; #charset koi8-r; #access_log /var/log/nginx/host.access.log main; #access_log /var/www/logs/host.access.log; location / { root /var/www/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 { } # 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 html; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_split_path_info ^(.+\.php)(.*)$; fastcgi_param PATH_INFO $fastcgi_path_info; fastcgi_param SCRIPT_FILENAME /var/www/html/weifen/trunk$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; #} }
server { listen 8014;#(自己配置的端口) #server_name localhost; location / { root /var/www/html;#(自己设置的主目录) index index.php index.html index.htm; if (!-e $request_filename) {#(ngxin做伪静态) rewrite ^(.*)$ /index.php?s=$1 last; break; } } error_page 500 502 503 504 /50x.html; location = /50x.html { } location ~ \.php$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_split_path_info ^(.+\.php)(.*)$; fastcgi_param PATH_INFO $fastcgi_path_info; fastcgi_param SCRIPT_FILENAME /var/www/html/weifen/trunk$fastcgi_script_name; include fastcgi_params; } }
配置 PHP
接下来,让我们对 PHP 的配置文件 /etc/php.ini 做自定义设置。更具体的就是在 /etc/php.ini 文件中增加以下两行。cgi.fix_pathinfo=0date.timezone = “PRC”
为了安全起见,我们希望的是 PHP 解释器只是处理指定文件路径的文件任务,而不是预测搜索一些并不存在的文件任务。上面的第一行起的就是这个作用。(LCTT 译注:原文用的时区是“America/New York”,根据国内情况,应该用 PRC或 Asia 下的中国城市。)
第二行定义的是 PHP 中日期/时间相关函数使用相关的默认时区。使用本指南,找出您所在的时区,并设置相应 date.timezone 的值。
测试 PHP
最后,让我们来测试下 nginx 是否能处理 PHP 页面。在测试之前,请确保重启 nginx 和 PHP-FPM。
$ sudo systemctl restart nginx $ sudo systemctl restart php-fpm
创建一个叫名叫 test.php 的文件,然后写入如下内容,并放入 /var/www/html/ 目录。
打开浏览器,输入 http://nginx的IP地址/test.php 。能看到php的信息就说明成功了。
如果配置了端口,但是访问不了,可以看一下下面的防火墙配置
CentOS7默认的防火墙不是iptables,而是firewalle.
安装iptable iptable-service
#先检查是否安装了iptables service iptables status #安装iptables yum install -y iptables #升级iptables yum update iptables #安装iptables-services yum install iptables-services
禁用/停止自带的firewalld服务
#停止firewalld服务 systemctl stop firewalld #禁用firewalld服务 systemctl mask firewalld
设置现有规则
#如果要添加内网ip信任(接受其所有TCP请求) iptables -A INPUT -p tcp -s 45.96.174.68 -j ACCEPT #过滤所有非以上规则的请求 iptables -P INPUT DROP #要封停一个IP,使用下面这条命令: iptables -I INPUT -s ***.***.***.*** -j DROP #要解封一个IP,使用下面这条命令: iptables -D INPUT -s ***.***.***.*** -j DROP
#保存上述规则 service iptables save
#注册iptables服务 #相当于以前的chkconfig iptables on systemctl enable iptables.service #开启服务 systemctl start iptables.service #查看状态 systemctl status iptables.service
解决vsftpd在iptables开启后,无法使用被动模式的问题
1.首先在/etc/sysconfig/iptables-config中修改或者添加以下内容
#添加以下内容,注意顺序不能调换
IPTABLES_MODULES="ip_conntrack_ftp"
IPTABLES_MODULES="ip_nat_ftp"
然后再重新设置iptables
注意:Linux在安装好之后通常 SELinux都是出于默认开启的状态,开启的情况下会导致一些服务的安装不成功。
在不需要的情况下完全可以关闭掉,下面是在centos 7.0里面如何查看,
关闭selinux。
永久关闭,可以修改配置文件/etc/selinux/config,将其中SELINUX设置为disabled。[root@localhost ~]# cat /etc/selinux/config # This file controls the state of SELinux on the system. # SELINUX= can take one of these three values: # enforcing - SELinux security policy is enforced. # permissive - SELinux prints warnings instead of enforcing. # disabled - No SELinux policy is loaded. #SELINUX=enforcing SELINUX=disabled # SELINUXTYPE= can take one of three two values: # targeted - Targeted processes are protected, # minimum - Modification of targeted policy. Only selected processes are protected. # mls - Multi Level Security protection. SELINUXTYPE=targeted [root@rdo ~]# sestatus SELinux status: disabled
数据库配置:
#配置文件vim /etc/my.cnf
# [mysqld]里面添加了一句 # Recommended in standard MySQL setup sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
# [mysqld_safe]里面添加了一句 character_set_server=utf8
MySQL登录 mysql -u root -p
创建库
create database weifen;
在mysql 命令行中运行以下代码 增加MySQL用户(只可以对数据增删改查)
grant select,insert,update,delete on weifen.* to weifen@"%" Identified by "hl#ahf123cjsh*";
// 增加MySQL用户(拥有该库所有权限)
grant all privileges on weifen.* to 'weifen'@'%' IDENTIFIED BY 'wei123456.A';
好了,到此你的服务器就可以用了!
参考:
http://blog.csdn.net/l1028386804/article/details/50779761
http://blog.csdn.net/leshami/article/details/51396811