centos 7 搭建lnmp环境搭建 yum 源安装

搭建centOS环境   采用yum 安装

为什么选择centos系统?CentOS是RedHat企业版的分支,也是企业操作系统的代表。服务器系统多采用CentOS

接下来我们一步一步搭建环境

一:yum 安装nginx

1.    添加NginxRepo 到CentOS中 

sudo rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm

2.安装nginx

sudo yum  -y  install nginx

3.添加nginx随系统启动

systemctl enable nginx.service

4.启动nginx

systemctl  start   nginx.service

查看nginx版本  nginx  -v                 我的版本 nginx version: nginx/1.12.2 

至此Web  Sever安装完毕!

二:yum 安装PHP7

centos php7安装方法  一:通过源码编译安装     二:使用第三方Repo源进行安装。

yum 源本身没有php7所以得添加Repo源

1.添加源执行以下两步

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

2.成功添加Repo源后,执行php7安装 我安装的是php7.2

sudo  yum  install  php72w  php72w-common  php72w-cli  php72w-fpm  php72w-mysql  php72w-opcache  php72w-mcrypt

通过yum简单方式安装php7代码 与 周边的必要模块。 执行下面的命令可以看到很长的列表,可以列出php7支持的所有模块。

sudo  yum  search  php72w-

3.php需要到gd模块 

sudo  yum install  php72w-gd

查看php版本  php -v

  php启动、停止、重启systemctl   start  php-fpm /  systemctl   restart   php-fpm  /  systemctl  stop php-fpm

4.通过nginx 与php-fpm让他们工作起来 首先编辑nginx的虚拟主机配置文件 文件位置

/etc/nginx/conf.d/default.conf

在配置文件中 加入

location  ~ .php$  {

       root          /usr/share/nginx/html;     //web根目录
        try_files $uri = 404;
        fastcgi_split_path_info ^(.+.php)(/.+)$;
        fastcgi_pass 127.0.0.1:9000;   //非常重要php-fpm运行时的端口
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME /usr/share/nginx/html$fastcgi_script_name;
        include fastcgi_params;

}

在/var/www  目录下创建index.php <?php  echo   phpinfo();    ?>

访问IP  看到php配置页  说明php与nginx 安装成功

三、安装数据库  如果你想要安装mariadb高版本的  请跳过直接看四

centOS 上yum源以不存在mysql  而是mariaDB版本5.多的

1直接yum -y  install   mariadb   mariadb-server

安装完成MariaDB,首先启动MariaDB

2. systemctl start mariadb

设置开机启动

3.systemctl enable mariadb

接下来进行MariaDB的相关简单配置

4. mysql_secure_installation

首先是设置密码,会提示先输入密码

5. Enter current password for root (enter for none):<–初次运行直接回车

四、安装mariadb高版本 

mariadb版本查看 参考官网版本

https://downloads.mariadb.org/mariadb/repositories/#mirror=tuna&distro=CentOS

添加MariaDB的yum源

1.创建MariaDB.repo

sudo vim  /etc/yum.repos.d/Mariadb.repo

第一种方法:(yum install -y MariaDB-server MariaDB-client galera)配置yum安装源和配置mariadb galera安装源yum源配置挂iso设置mariadb的yum源并安装(所有节点都要)修改yum源文件vi /etc/yum.repos.d/mariadb.repo[mariadb]name = MariaDBbaseurl = http://yum.mariadb.org/10.3.5/centos74-amd64gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDBgpgcheck=1enabled=0

2.将以下文件中的字段添加到MariaDB.repo文件中

# MariaDB10.1CentOS repository list - created2016-12-0103:36 UTC

# http://downloads.mariadb.org/mariadb/repositories/[mariadb]

name = MariaDB

baseurl = http://yum.mariadb.org/10.1/centos7-amd64gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDBgpgcheck=1

3.yum安装MariaDB

sudo yum -y install MariaDB-server MariaDB-client

4.启动MariaDB服务

systemctl start mysql.service

systemctl enable mysql.service

5.配置MariaDB服务

使用mysql_secure_installation配置MariaDB服务

mysql_secure_installation

具体设置

#由于一开始安装MariaDB数据库后, root用户默认密码为空, 所以只需要按Enter键

Enter current password for root (enter for none):

#是否设置root用户的新密码

Set root password? [Y/n] y

#录入新密码

New password:

#确认新密码

Re-enter new password:

#是否删除匿名用户,生产环境建议删除

Remove anonymous users? [Y/n] y

#是否禁止root远程登录,根据自己的需求选择

Disallow root login remotely? [Y/n] n

#是否删除test数据库

Remove test database and access to it? [Y/n] y

#是否重新加载权限表

Reload privilege tables now? [Y/n] y

5.配置MariaDB的字符集

  查看/etc/my.cnf文件内容,其中包含一句!includedir /etc/my.cnf.d 说明在该配置文件中引入/etc/my.cnf.d 目录下的配置文件。

  1)使用vi server.cnf命令编辑server.cnf文件,在[mysqld]标签下添加

init_connect='SET collation_connection = utf8_unicode_ci'

init_connect='SET NAMES utf8'

character-set-server=utf8

collation-server=utf8_unicode_ci

skip-character-set-client-handshake

如果/etc/my.cnf.d 目录下无server.cnf文件,则直接在/etc/my.cnf文件的[mysqld]标签下添加以上内容。

  2)用vi  client.cnf命令编辑/etc/my.cnf.d/client.cnf文件,在[client]标签下添加 

default-character-set=utf8

  3)用vi  mysql-clients.cnf命令编辑/etc/my.cnf.d/mysql-clients.cnf文件,在[mysql]标签下添加 

default-character-set=utf8

配置完成后 systemctl restart mariadb 重启服务。

进入到数据库查看字符设置。

show variableslike"%character%";

show variables like"%collation%";

6开启远程访问

1.防火墙添加3306端口

查看firewall状态

firewall-cmd --state

状态是not running,启动firewall

systemctl start firewalld

状态是running

开放3306端口

firewall-cmd --zone=public --add-port=3306/tcp --permanent

重新载入

firewall-cmd --reload

2.明明之前有设置root开启远程的,但是我这里访问不了,所以重新去MariaDB赋权限

#进入Mariadb

mysql -uroot -p

#选择数据库

use mysql;

#添加权限

Grant all on *.* to 'root'@'%' identified by 'root用户的密码' with grant option;

#重新载入

flush privileges;

原文地址:https://www.cnblogs.com/muscles/p/9554468.html