CentOS 7 使用 YUM 搭建 LAMP/LNMP 环境

老牛浏览 343评论 0发表于

1. 目的

在 CentOS 7 上搭建 LAMP/LNMP 环境,使用 yum 命令安装,默认 PHP,MySQL 软件版本比较低。为了使用最新版本 PHP 及 MariaDB,我们指定第三方源后再使用 yum 安装。

2. 准备工作

2.1 安装系统

安装 CentOS 7.3 64 位系统,具体步骤略。

2.2 配置网络

编辑网络配置文件(文件名可能不同)

bash
vim /etc/sysconfig/network-scripts/ifcfg-ens33
TYPE="Ethernet"
BOOTPROTO=static        #启用静态IP地址 none-未指定 static-静态
ONBOOT="yes"            #开机自动启用网络连接
DNS1=114.114.114.114    #主DNS
DNS2=8.8.8.8            #备用DNS
IPADDR=172.16.7.3       #设置IP地址
PREFIX=24               #前缀是24,其实就是相当于子网掩码有24位
GATEWAY=172.16.7.1      #网关

重启网络,测试网络是否正常

bash
systemctl restart network
ping -c 5 www.baidu.com

2.3 配置防火墙

开启 80 端口,开启 3306 端口,重启防火墙

bash
firewall-cmd --zone=public --add-port=80/tcp --permanent
firewall-cmd --zone=public --add-port=443/tcp --permanent
firewall-cmd --zone=public --add-port=3306/tcp --permanent
firewall-cmd --reload

2.4 关闭 SELINUX

编辑 SELINUX 配置文件,修改添加以下内容

bash
vim /etc/selinux/config
vim
#SELINUX=enforcing        #修改:注释掉
#SELINUXTYPE=targeted     #修改:注释掉
SELINUX=disabled          #增加

临时关闭 SELINUX:

bash
setenforce 0

3. 搭建 LAMP 环境

3.1 安装 MariaDB

添加 MariaDB 官方最新源,MariaDB10.2:

bash
touch /etc/yum.repos.d/mariadb.repo

添加内容:

vim
[mariadb]
name = MariaDB
baseurl = http://yum.mariadb.org/10.2/centos7-amd64
gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck=1

然后,yum 安装 MariaDB10.2:

bash
yum install mariadb-server

添加开机自启动:

bash
systemctl enable mariadb

启动 MariaDB 数据库:

bash
systemctl start mariadb

接下来进行 MariaDB 的简单初始化配置:

bash
mysql_secure_installation
#Enter current password for root (enter for none): 数据库密码,第一次运行为空直接回车即可。
#Set root password? [Y/n] Y 设置新密码,然后设置新密码
#Remove anonymous users? [Y/n] Y 取消匿名用户访问
#Disallow root login remotely? [Y/n] Y 禁止root远程访问
#Remove test database and access to it? [Y/n] Y 删除test数据库
#Reload privilege tables now? [Y/n] Y 刷新权限列表

设置数据库字符集为 utf8:

打开文件 /etc/my.cnf

bash
vim /etc/my.cnf

[mysqld] 标签下添加

ini
init_connect='SET collation_connection = utf8_general_ci'
init_connect='SET NAMES utf8'
character-set-server=utf8
collation-server=utf8_general_ci

打开文件 /etc/my.cnf.d/client.cnf

bash
vim /etc/my.cnf.d/client.cnf

[client] 中添加

ini
default-character-set=utf8

打开文件 /etc/my.cnf.d/mysql-clients.cnf

bash
vim /etc/my.cnf.d/mysql-clients.cnf

[mysql] 中添加

ini
default-character-set=utf8

全部配置完成,重启 MariaDB 即可。

3.2 安装 Apache

使用 yum 安装 Apache Httpd2.4.6

bash
yum install httpd

隐藏 HTTP 响应头中的版本信息,文件路径 /etc/httpd/conf/httpd.conf

打开 httpd.conf,将 ServerTokens 的值修改为 Prod(若没有该属性,则新增一行)

隐藏服务器页脚信息:

打开 httpd.conf,将 ServerSignature 的值修改为 Off(若没有该属性,则新增一行)

添加开机自启动:

bash
systemctl enable httpd

启动 httpd 服务:

bash
systemctl start httpd

3.3 安装 PHP

添加 webtatic(第三方)yum 源:

bash
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 安装 PHP 7.1:

bash
yum -y install php71w php71w-devel

隐藏 HTTP 响应头中的 X-Powered-By:PHP/7.1.11

/etc/php.ini 中,将 expose_php 值修改为 Off

接下来,可以安装 PHP 常用的扩展(可选):

bash
yum -y install php71w-opcache
yum -y install php71w-gd
yum -y install php71w-imap
yum -y install php71w-mbstring
yum -y install php71w-mcrypt
yum -y install php71w-mysqlnd
yum -y install php71w-pdo
yum -y install php71w-pdo_dblib
yum -y install php71w-pear.noarch
yum -y install php71w-pecl-apcu
yum -y install php71w-pecl-apcu-devel
yum -y install php71w-pecl-igbinary
yum -y install php71w-pecl-igbinary-devel
yum -y install php71w-pecl-imagick
yum -y install php71w-pecl-imagick-devel
yum -y install php71w-pecl-memcached
yum -y install php71w-pecl-mongodb
yum -y install php71w-pecl-redis
yum -y install php71w-pecl-xdebug
yum -y install php71w-pgsql
yum -y install php71w-phpdbg
yum -y install php71w-process
yum -y install php71w-pspell
yum -y install php71w-recode
yum -y install php71w-snmp
yum -y install php71w-soap
yum -y install php71w-tidy
yum -y install php71w-xml
yum -y install php71w-xmlrpc

安装完扩展,重启 Apache 即可。

4. 搭建 LNMP 环境

LAMP 环境下,PHP 是以 Apache 模块方式运行的,而 LNMP 中,PHP 一般以 PHP-FPM 模式运行。

4.1 安装 MariaDB

同 Apache 下安装

4.2 安装 Nginx

添加 Nginx 官方最新源:

bash
touch /etc/yum.repos.d/nginx.repo

添加以下内容:

vim
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=0
enabled=1

然后,yum 安装 Nginx 1.12.2:

bash
yum install nginx

隐藏 HTTP 响应头中的版本信息,文件路径 /etc/nginx/nginx.conf

打开 nginx.conf,在 http 里面增加一行 server_tokens off;

添加开机自启动:

bash
systemctl enable nginx

启动 Nginx 服务:

bash
systemctl start nginx

4.3 安装 PHP

添加 webtatic(第三方)yum 源:

bash
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 安装 PHP 7.1:

bash
yum -y install php71w-fpm

配置 Nginx 支持 PHP。

修改默认虚拟主机配置文件:

bash
vim /etc/nginx/conf.d/default.conf

取消 FastCGI server 部分 location 的注释,注意 fastcgi_param 行的参数,改为项目绝对路径。

vim
# 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_param SCRIPT_FILENAME /usr/share/nginx/html/$fastcgi_script_name;
  include fastcgi_params;
}

设置 Nginx,PHP-FPM 开机自启动:

bash
systemctl enable nginx
systemctl enable php-fpm

重启 Nginx,启动 PHP-FPM:

bash
systemctl restart nginx
systemctl start php-fpm

隐藏 HTTP 响应头中的 X-Powered-By:PHP/7.1.11

/etc/php.ini 中,将 expose_php 值修改为 Off

接下来,可以安装 PHP 常用的扩展(可选):

bash
yum -y install php71w-opcache
yum -y install php71w-gd
yum -y install php71w-imap
yum -y install php71w-mbstring
yum -y install php71w-mcrypt
yum -y install php71w-mysqlnd
yum -y install php71w-pdo
yum -y install php71w-pdo_dblib
yum -y install php71w-pear.noarch
yum -y install php71w-pecl-apcu
yum -y install php71w-pecl-apcu-devel
yum -y install php71w-pecl-igbinary
yum -y install php71w-pecl-igbinary-devel
yum -y install php71w-pecl-imagick
yum -y install php71w-pecl-imagick-devel
yum -y install php71w-pecl-memcached
yum -y install php71w-pecl-mongodb
yum -y install php71w-pecl-redis
yum -y install php71w-pecl-xdebug
yum -y install php71w-pgsql
yum -y install php71w-phpdbg
yum -y install php71w-process
yum -y install php71w-pspell
yum -y install php71w-recode
yum -y install php71w-snmp
yum -y install php71w-soap
yum -y install php71w-tidy
yum -y install php71w-xml
yum -y install php71w-xmlrpc

安装完扩展,重启 PHP-FPM 即可。

5. 负载均衡

将 Nginx 当负载均衡放在前面,后面放 Apache 服务器,静态文件直接由 Nginx 处理,动态文件交给 Apache 处理,再返回给 Nginx。Apache 服务器可以放多台,组成集群。

点赞
收藏
暂无评论,快来发表评论吧~