CentOS 7搭建LNMP环境

2018-12-14  本文已影响0人  在牛魔角上狂码

安装MySQL

首先添加所要安装的MySQL版本yum存储库

vim /etc/yum.repos.d/mysql-community.repo 

//在文件中加入
[mysql80-community]
name=MySQL 8.0 Community Server
baseurl=http://repo.mysql.com/yum/mysql-8.0-community/el/7/$basearch/
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql

注:8.0为版本号,7为centos 7

检查验证yum存储库是否添加成功

yum repolist | grep mysql

//显示
mysql80-community/x86_64  MySQL 8.0 Community Server                          49

安装mysql

yum install mysql-community-server

//显示
已加载插件:fastestmirror
Loading mirror speeds from cached hostfile
正在解决依赖关系
--> 正在检查事务
---> 软件包 mysql-community-server.x86_64.0.8.0.13-1.el7 将被 安装
--> 正在处理依赖关系 mysql-community-common(x86-64) = 8.0.13-1.el7,它被软件包 mysql-community-server-8.0.13-1.el7.x86_64 需要
.
.
.
为依赖而安装:
 libaio                       x86_64  0.3.109-13.el7   base                24 k
 mysql-community-client       x86_64  8.0.13-1.el7     mysql80-community   26 M
 mysql-community-common       x86_64  8.0.13-1.el7     mysql80-community  554 k

事务概要
================================================================================
安装  3 软件包 (+3 依赖软件包)

总下载量:412 M
Is this ok [y/d/N]: y
Downloading packages:
(1/6): libaio-0.3.109-13.el7.x86_64.rpm                    |  24 kB   00:00     
warning: /var/cache/yum/x86_64/7/mysql80-community/packages/mysql-community-common-8.0.13-1.el7.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
mysql-community-common-8.0.13-1.el7.x86_64.rpm 的公钥尚未安装
.
.
.
已安装:
  mysql-community-libs.x86_64 0:8.0.13-1.el7                                    
  mysql-community-libs-compat.x86_64 0:8.0.13-1.el7                             
  mysql-community-server.x86_64 0:8.0.13-1.el7                                  

作为依赖被安装:
  libaio.x86_64 0:0.3.109-13.el7                                                
  mysql-community-client.x86_64 0:8.0.13-1.el7                                  
  mysql-community-common.x86_64 0:8.0.13-1.el7                                  

替代:
  mariadb-libs.x86_64 1:5.5.60-1.el7_5                                          

完毕

如果出现

.
.
.
警告:/var/cache/yum/x86_64/7/mysql80-community/packages/mysql-community-libs-8.0.13-1.el7.x86_64.rpm: 头V3 DSA/SHA1 Signature, 密钥 ID 5072e1f5: NOKEY
从 file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql 检索密钥


获取 GPG 密钥失败:[Errno 14] curl#37 - "Couldn't open file /etc/pki/rpm-gpg/RPM-GPG-KEY-mysql"

gpgcheck=1设置为gpgcheck=0,安装完毕后再设置回gpgcheck=1

启动MySQL

systemctl start mysqld

查看MySQL状态

systemctl status mysqld

//MySQL启动前的状态显示
● mysqld.service - MySQL Server
   Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
   Active: inactive (dead)
     Docs: man:mysqld(8)
           http://dev.mysql.com/doc/refman/en/using-systemd.html

//MySQL启动后的状态显示
● mysqld.service - MySQL Server
   Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
   Active: active (running) since 四 2018-12-13 23:11:25 CST; 16s ago
     Docs: man:mysqld(8)
           http://dev.mysql.com/doc/refman/en/using-systemd.html
  Process: 18533 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
 Main PID: 18605 (mysqld)
   Status: "SERVER_OPERATING"
   CGroup: /system.slice/mysqld.service
           └─18605 /usr/sbin/mysqld

12月 13 23:11:17 iZwz956snfyrvah6yq8sa4Z systemd[1]: Starting MySQL Server...
12月 13 23:11:25 iZwz956snfyrvah6yq8sa4Z systemd[1]: Started MySQL Server.

在服务器初始启动时,如果服务器的数据目录为空,则会发生以下情况:

mysql
//显示
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)

登录MySQL,修改系统生成的临时MySQL密码

查看MySQL初始化后的临时密码,临时密码存储在错误日志文件中

grep 'temporary password' /var/log/mysqld.log

//显示
2018-12-13T15:11:21.342091Z 5 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: 密码

重置初始化密码

ALTER USER 'root'@'localhost' IDENTIFIED BY '新密码';

注:新密码要求:密码包含至少一个大写字母,一个小写字母,一个数字和一个特殊字符,并且密码总长度至少为8个字符。

重置密码后,退出并使用新密码重新登录

mysql -uroot -p
Enter password: 输入密码
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 11
Server version: 8.0.13 MySQL Community Server - GPL

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> 

安装Nginx

添加Nginx的yum存储库

vim /etc/yum.repos.d/nginx.repo

//在文件中加入
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=0
enabled=1

检查验证yum存储库是否添加

yum repolist | grep nginx
//显示
nginx/x86_64              nginx repo                                         136

安装Nginx

yum install nginx

//显示
已加载插件:fastestmirror
Loading mirror speeds from cached hostfile
正在解决依赖关系
--> 正在检查事务
---> 软件包 nginx.x86_64.1.1.14.2-1.el7_4.ngx 将被 安装
--> 解决依赖关系完成
.
.
.
安装  1 软件包

总下载量:754 k
安装大小:2.6 M
Is this ok [y/d/N]: y
Downloading packages:
nginx-1.14.2-1.el7_4.ngx.x 47% [=======-        ] 9.5 kB/s | 360 kB   00:41 ETA 
.
.
.
----------------------------------------------------------------------
  验证中      : 1:nginx-1.14.2-1.el7_4.ngx.x86_64                           1/1 

已安装:
  nginx.x86_64 1:1.14.2-1.el7_4.ngx                                             

完毕!

启动Nginx

systemctl start nginx

IP访问


image.png

安装PHP

php官网下载:http://php.net/downloads.php

wget http://cn2.php.net/distributions/php-7.2.13.tar.gz

tar -zxvf php-7.2.13.tar.gz

cd php-7.2.13

./configure --enable-fpm --with-mysql

make

make install

常用扩展

--prefix=/usr/local/php \
--with-config-file-path=/usr/local/php/etc \
--with-zlib-dir \
--with-freetype-dir \
--enable-mbstring \
--with-libxml-dir=/usr \
--enable-xmlreader \
--enable-xmlwriter \
--enable-soap \
--enable-calendar \
--with-curl \
--with-zlib \
--with-gd \
--with-pdo-sqlite \
--with-pdo-mysql \
--with-mysqli \
--with-mysql-sock \
--enable-mysqlnd \
--disable-rpath \
--enable-inline-optimization \
--with-zlib \
--enable-sockets \
--enable-sysvsem \
--enable-sysvshm \
--enable-pcntl \
--enable-mbregex \
--enable-exif \
--enable-bcmath \
--with-mhash \
--enable-zip \
--with-pcre-regex \
--with-jpeg-dir=/usr \
--with-png-dir=/usr \
--with-openssl \
--enable-ftp \
--with-kerberos \
--with-gettext \
--with-xmlrpc \
--with-xsl \
--enable-fpm \
--with-fpm-user=www-data \
--with-fpm-group=www-data

创建配置文件,并将其复制到正确的位置

cp php.ini-development /usr/local/php/php.ini

cp /usr/local/etc/php-fpm.d/www.conf.default /usr/local/etc/php-fpm.conf

cp sapi/fpm/php-fpm /usr/local/bin

修改php.ini配置文件,设置cgi.fix_pathinfo=0

vim /usr/local/php/php.ini

//修改
.
.
.
cgi.fix_pathinfo=0
.
.
.

启动 php-fpm 服务:

/usr/local/bin/php-fpm

配置 Nginx 使其支持 PHP 应用:

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

//修改
.
.
.
location / {
        root   /usr/share/nginx/html;
        index  index.php index.html index.htm;
    }
.
.
.
location ~* \.php$ {
        root /usr/share/nginx/html;
        fastcgi_index   index.php;
        fastcgi_pass    127.0.0.1:9000;
        include         fastcgi_params;
        fastcgi_param   SCRIPT_FILENAME    $document_root$fastcgi_script_name;
        fastcgi_param   SCRIPT_NAME        $fastcgi_script_name;
    }
.
.
.

重启Nginx

systemctl restart nginx

测试

echo "<?php echo phpinfo(); ?> >> /usr/share/nginx/html/index.php

浏览器访问:XX/index.php

image.png

MySQL官方yum安装文档

Nginx官方yum安装文档

PHP官方安装文档

上一篇下一篇

猜你喜欢

热点阅读