Centos7 编译安装 LNMP -- 可能是全网最全

声明

本文档请用于学习,生产环境谨慎使用

前言

网络上有很多关于Windows或者Linux下面安装LNMP的教程,非常多。但是我还是选择自己去一步一步实现,即使我已经按照网上的教程在不同系统中装了N多次。为什么?因为不懂。不懂为什么需要那些系统软件,不懂那些编译项为什么要加?有何作用?只会复制粘贴。

几点建议:

  • 编译安装只是环境搭建的开始,别想着把所有问题都在此阶段解决
  • 坚持按需编译,最小化安装,把问题暴露出来,去解决它
  • 对使用的每一个编译参数负责
  • 远离复制粘贴,你可能还有救
  • 完全不了解的软件尽可能避免编译安装

目标

我们把Laravel跑起来!

基本情况

软件文档列表

此处文档是基础软件文档,不是很全,一部分软件文档在内容中。

系统要求

# 查看CentOS版本
[root@bogon source]# cat /etc/redhat-release 
CentOS Linux release 7.6.1810 (Core) 
[root@bogon source]# cat /proc/version
Linux version 3.10.0-957.el7.x86_64 (mockbuild@kbuilder.bsys.centos.org) (gcc version 4.8.5 20150623 (Red Hat 4.8.5-36) (GCC) ) #1 SMP Thu Nov 8 23:39:32 UTC 2018

软件列表

系统内置 CURL 命令,所以我们选择CURL下载软件curl -O xxx,如果软件包出现解压问题,不是因为tar -zxf 解压不了这个包。而是说明这个包下载的不完整,或者说因为网络原因只是一个空包,解决办法是删除后重新下载。另外防止资源网站迁移,请使用curl -OL参数.
-L:跟踪重定向
国外资源下载太慢,请使用迅雷下载,然后通过scp上传到服务器,常用参数:

  • -r 传输的对象是目录
  • -p 指定端口,默认 22

使用场景:

  • scp -p <server port> <server user>@<server ip>:<server path>/<server file> <local path> 这是下载服务器资源,资源将被保存到 <local path>
  • scp -p <server port> <local path>/<local file> <server user>@<server ip>:<server path> 这是上传本地文件到服务器指定路径
mysql-8.0.17.tar.gz
nginx-1.16.1.tar.gz
php-7.3.9.tar.gz
redis-5.0.5.tar.gz
swoole-4.4.4.tgz

Software Collections

为什么先说这个,为了给大家提供一个不需要关注 lnmp 系统依赖,而只是编译安装 lnmp 的方法。

原理

我们一般装完软件,都会设置环境变量,以便我们能愉快方便的使用这款软件,如果我们不使用环境变量,我们就会到安装目录找到可执行文件,执行命令。

如果一款软件有多个版本,我们会把它们安装到不同的路径,这样我们就可以分别到不同版本的目录,启动它们。如果你对php进行过多版本的安装和使用那将会非常熟悉。

当然yum groupinstall Development tools 它们两作用一致,但是scl可以按需安装,按需加载是它不能比拟的。

这个工具干的也是同一件事情。它做两件事情,第一件事情装好不同版本的软件,第二件事情,在你使用时,将安装过程中生成的全局变量文件加载到系统中。尽管有三种不同的使用方式,原理基本一致。

大家可以看一下 enable(工具包安装完后生成的全局变量文件) 文件内容:

[root@bogon bld]# cat /opt/rh/devtoolset-8/enable 
# General environment variables
export PATH=/opt/rh/devtoolset-8/root/usr/bin${PATH:+:${PATH}}
export MANPATH=/opt/rh/devtoolset-8/root/usr/share/man:${MANPATH}
export INFOPATH=/opt/rh/devtoolset-8/root/usr/share/info${INFOPATH:+:${INFOPATH}}
export PCP_DIR=/opt/rh/devtoolset-8/root
# Some perl Ext::MakeMaker versions install things under /usr/lib/perl5
# even though the system otherwise would go to /usr/lib64/perl5.
export PERL5LIB=/opt/rh/devtoolset-8/root//usr/lib64/perl5/vendor_perl:/opt/rh/devtoolset-8/root/usr/lib/perl5:/opt/rh/devtoolset-8/root//usr/share/perl5/vendor_perl${PERL5LIB:+:${PERL5LIB}}
# bz847911 workaround:
# we need to evaluate rpm's installed run-time % { _libdir }, not rpmbuild time
# or else /etc/ld.so.conf.d files?
rpmlibdir=$(rpm --eval "%{_libdir}")
# bz1017604: On 64-bit hosts, we should include also the 32-bit library path.
if [ "$rpmlibdir" != "${rpmlibdir/lib64/}" ]; then
  rpmlibdir32=":/opt/rh/devtoolset-8/root${rpmlibdir/lib64/lib}"
fi
export LD_LIBRARY_PATH=/opt/rh/devtoolset-8/root$rpmlibdir$rpmlibdir32${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}
export LD_LIBRARY_PATH=/opt/rh/devtoolset-8/root$rpmlibdir$rpmlibdir32:/opt/rh/devtoolset-8/root$rpmlibdir/dyninst$rpmlibdir32/dyninst${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}
# duplicate python site.py logic for sitepackages
pythonvers=2.7
export PYTHONPATH=/opt/rh/devtoolset-8/root/usr/lib64/python$pythonvers/site-packages:/opt/rh/devtoolset-8/root/usr/lib/python$pythonvers/site-packages${PYTHONPATH:+:${PYTHONPATH}}
export PKG_CONFIG_PATH=/opt/rh/devtoolset-8/root/usr/lib64/pkgconfig${PKG_CONFIG_PATH:+:${PKG_CONFIG_PATH}}

简介

Software Collections give you the power to build, install, and use multiple versions of software on the same system, without affecting system-wide installed packages.

安装

yum install centos-release-scl

使用

  • 列出已安装的软件集合 scl --list
  • 启用软件集合: 四种方式(文档中三种,其它一种)

    • 命令形式 scl action software_collection_1 software_collection_2 'command --argument'
    • shell 形式 scl enable software_collection_1 software_collection_2 bash 。就是给你重新打开了一个子 shell,给你加载了他安装的软件的环境变量。
    • 直接使用 scl enable software_collection_1 'perl --version'
    • 永久性地将DTS添加到您的开发环境中

      使用首选文本编辑器,将以下行添加到以下结尾  ~/.bashrc:
      source scl_source enable devtoolset-8
      注销并重新登录后,您可以通过运行which g++ 或  验证DTS GCC是否在您的路径中  g++ --version

在本文章中使用说明

这款管理工具刚开始是不知道的,为了解决mysql编译时gcc版本太低才知道。只在mysql编译时使用了,在其它地方都没有使用。不是说这工具不好用,只是为了给你们多看看我们用yumscl时看不到的依赖问题,使用后你会发现这工具真香!当系统需要任何这个工具包中需要的依赖时,你都可以通过上述四种使用方式,来解决依赖。

推荐使用,尤其是安装你们不太了解的依赖软件时,比方说:gcc,后面有使用scl升级gcc的过程

安装

在安装开始之前,我们先创建三个用户: www web mysql和对应的用户组,分别给php-fpm nginx mysql使用
Linux中安装服务的时候,尽可能的为服务创建用户和用户组,方便管理这些服务,比方说:设置服务可以访问的目录权限,文件权限等

groupadd www
groupadd web
groupadd mysql
useradd -r -g mysql -s /bin/false mysql
useradd -r -g www   -s /bin/false www
useradd -r -g web   -s /bin/false web

参数解释:

  • -g 指定组
  • -s 设置 shell

    • /bin/false 指的是不能用这个用户访问系统资源,包括loginsftp,ftp等等
    • /usr/sbin/nologin 仅仅限制用户登陆
  • -r 系统账户:默认没有家目录,UID 不会大于1000 也就是 0-999。不加 -r就会创建用户账户,UID默认从1000开始,默认有家目录,可以通过 -M 禁止创建家目录

友情提示:下列安装的所有服务,在安装完之后先不要急着启动,先修改配置,然后启动。不然你如果启动后再修改,你可能还需要清理服务默认生成的文件。LNMP一般默认需要生成文件的配置项不是日志目录就是数据存储目录。

Nginx

我们先练个手按照安装文档上的示例来安装,命令如下:

./configure --sbin-path=/usr/local/nginx/nginx \
    --conf-path=/usr/local/nginx/nginx.conf \
    --pid-path=/usr/local/nginx/nginx.pid \
    --with-http_ssl_module \
    --with-pcre=../pcre-8.43 \
    --with-zlib=../zlib-1.2.11

pcrezlib 包需要你自己下载到本地.

PCRE 作用说明(来自官网)

sets the path to the sources of the PCRE library. The library distribution (version 4.4 — 8.43) needs to be downloaded from the PCRE site and extracted. The rest is done by nginx’s ./configure and make. The library is required for regular expressions support in the location directive and for the ngx_http_rewrite_module module.

ZLIB 作用说明(来自官网)

sets the path to the sources of the zlib library. The library distribution (version 1.1.3 — 1.2.11) needs to be downloaded from the zlib site and extracted. The rest is done by nginx’s ./configure and make. The library is required for the ngx_http_gzip_module module.

下载时注意nginx要求的版本

安装过程

# 解压并进入
tar -zxf nginx-1.16.1.tar.gz
tar -zxf pcre-8.43.tar.gz
tar -zxf zlib-1.2.11.tar.gz
cd nginx-1.16.1

# 执行编译
[root@bogon nginx-1.16.1]# ./configure --sbin-path=/usr/local/nginx/nginx \
    --conf-path=/usr/local/nginx/nginx.conf \
    --pid-path=/usr/local/nginx/nginx.pid \
    --with-http_ssl_module \
    --with-pcre=../pcre-8.43 \
    --with-zlib=../zlib-1.2.11

# 查看 NGINX_ERROR_1
checking for OS
'+' Linux 3.10.0-957.el7.x86_64 x86_64 # + 号有排版问题用双引号引起来
checking for C compiler ... not found
./configure: error: C compiler cc is not found

# 重新执行编译

# 查看 NGINX_ERROR_2
./configure: error: SSL modules require the OpenSSL library.
You can either do not enable the modules, or install the OpenSSL library
into the system, or build the OpenSSL library statically from the source
with nginx by using --with-openssl=<path> option.

# 重新执行编译

# 执行 make && make install
[root@bogon nginx-1.16.1]# make && make install
# 出现这个说明安装没问题: make[1]: Leaving directory `/root/source/nginx-1.16.1'

至此我们就Nginx编译安装完成,示例编译参数不是很符合我们生产环境,因为所有的东西都设置到一个目录下。我们在生产环境部署时,尽可能区分目录,方便维护,所以我们使用Nginx默认编译参数。
默认编译,配置一下安装目录即可(指定一下用户和用户组):

./configure --prefix=/usr/local/nginx --user=web --group=web --with-http_ssl_module --with-pcre=../pcre-8.43 --with-zlib=../zlib-1.2.11
make && make install
# 安装完文件目录
drwxr-xr-x. 2 root root 4096 Sep  2 23:04 conf
drwxr-xr-x. 2 root root   40 Sep  2 23:04 html
drwxr-xr-x. 2 root root    6 Sep  2 23:04 logs
drwxr-xr-x. 2 root root   19 Sep  2 23:04 sbin

更多编译参数我们可以通过 ./configure --help查看
因为一些原因,比如: 开启其他nginx功能,我们需要重新编译,步骤如下:

  • 然后直接进入到解压后的软件包目录,执行make clean
  • 重新 ./configure xxx
  • make && make install

nginx服务启动命令,请自行了解: /usr/local/nginx/sbin/nginx -h

遇到的问题

  • NGINX_ERROR_1
    ./configure: error: C compiler cc is not found
    yum install -y gcc-c++ 这个包支持编译CC++程序,防止代码中有C++代码
    维基百科:

  • NGINX_ERROR_2

    ./configure: error: SSL modules require the OpenSSL library.
    You can either do not enable the modules, or install the OpenSSL library
    into the system, or build the OpenSSL library statically from the source
    with nginx by using --with-openssl=<path> option.  

    Nginx开启了openssl但是这个需要系统安装openssl
    执行yum install -y openssl openssl-devel
    维基百科:

    devel 包主要是供开发用,至少包括以下2个东西:

    1. 头文件
    2. 链接库

    有的还含有开发文档或演示代码。
    glibglib-devel 为例:
    如果你安装基于 glib 开发的程序,只需要安装 glib 包就行了
    但是如果你要编译使用了 glib 的源代码(也就是我们常见的依赖XXX),则需要安装 glib-develXXX-devel
    一般我们不是用软件而只是安装依赖的话,我们尽量直接安装XXX-devel

测试

基于默认编译安装(feiyanshi )

  • 启动

    /usr/local/nginx/sbin/nginx

    默认读取编译时指定的Nginx配置文件,没指定配置文件路径默认读取安装目录下的conf/nginx.conf文件,如果想修改配置文件请使用 -c 参数: /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

    [root@bogon nginx]# /usr/local/nginx/nginx 
    [root@bogon nginx]# ps -ef | grep nginx
    root     32167     1  0 12:45 ?        00:00:00 nginx: master process /usr/local/nginx/nginx
    nobody   32168 32167  0 12:45 ?        00:00:00 nginx: worker process
    root     32170 11602  0 12:45 pts/0    00:00:00 grep --color=auto nginx
  • 通过服务器IP访问,出现访问拒绝问题,处理方式如下:
    查看防火墙状态:

    [root@bogon nginx]# firewall-cmd --state
    running

    关掉:systemctl stop firewalld.service(生产环境上注意,你应该选择开启 80 端口)
    再次访问!
    《Centos7 编译安装 LNMP -- 可能是全网最全》

PHP

安装过程

  • 一般除非特殊情况,我们都会开启php-fpm
  • 我们不能保证我们只需要一个版本的php,所以创建安装目录时,一般指定版本作为安装目录,比如: /usr/local/php/7.3/
  • 好多人可能觉得我项目里面使用了很多扩展,编译安装这么点,项目跑不起来怎么办,不要怕,缺就重新编译或者安装。坚持按需编译,按需安装
[root@bogon source]# tar -zxf php-7.3.9.tar.gz 
[root@bogon source]# cd php-7.3.9
[root@bogon php-7.3.9]# ./configure --prefix=/usr/local/php/7.3 --enable-fpm --with-fpm-user=www --with-fpm-group=www
[root@bogon php-7.3.9]# make && make install

# PHP_ERROR_1
configure: error: libxml2 not found. Please check your libxml2 installation.

# 重新执行编译
# 出现 Thank you for using PHP. 说明编译没有问题继续...

make && make install

# 出现 Installing PDO headers:           /usr/local/php/7.3/include/php/ext/pdo/ 说明安装成功

遇到的问题

  • PHP_ERROR_1
    configure: error: libxml2 not found. Please check your libxml2 installation.
    直接安装,记得安装开发包
    yum install -y libxml2 libxml2-devel
    安装libxml2时,系统提示我已经存在,但php编译时还报错,说明编译安装时的依赖包需要安装devel版本

PHP-FPM 设置

我们依次解决如下问题:

  • 解决PHP_FPM 可执行文件找不到

    • 编译时加上 --sbindir=/usr/local/php/7.3/sbin 或者 --sbindir=/usr/local/php/7.3/bin(统一管理)
    • [root@bogon bin]# pwd
      /usr/local/php/7.3/bin
      [root@bogon bin]# cp /root/source/php-7.3.9/sapi/fpm/php-fpm ./
  • 解决PHP_FPM 只能通过 kill 命令关闭(建议大家了解一下进程管理),重启

    [root@bogon php-7.3.9]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
    [root@bogon php-7.3.9]# chmod 755 /etc/init.d/php-fpm
    [root@bogon etc]# /etc/init.d/php-fpm -h
    Usage: /etc/init.d/php-fpm {start|stop|force-quit|restart|reload|status|configtest}

配置

  • 首先设置php.ini
    如果在编译的时候没有加--with-config-file-path,那么就复制解压后的软件包中的php.ini-development || php.ini-productionphp默认配置文件所在路径,可通过 php --ini查看.
    关于这两个ini文件的区别,在配置文件中有说明,关键词:About this file
  • 设置php-fpm.conf
    在安装目录下/usr/local/php/7.3/etc/下有个php-fpm.conf.default复制一份,改名为php-fpm.conf
    cp php-fpm.conf.default php-fpm.conf
  • 设置www.conf
    在安装目录下/usr/local/php/7.3/etc/php-fpm.d/下有个www.conf.default复制一份,改名为www.conf
    cp www.conf.default www.conf

磁盘挂载

简单说一下,因为要存放代码
很多人喜欢把代码放到家目录,这样很不好,一个人用无所谓,但是服务器因为有很多人管理,所以,我们一般创建一个单独的文件进行存放,还有一点,这个文件最好挂载到一个单独的磁盘,方便说句备份,不要和系统文件打交道。

# 查看一下有无空闲磁盘
[root@bogon conf]# fdisk -l

Disk /dev/sda: 549.8 GB, 549755813888 bytes, 1073741824 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x0000e7dc

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     2099199     1048576   83  Linux
/dev/sda2         2099200  1073741823   535821312   8e  Linux LVM

Disk /dev/sdb: 549.8 GB, 549755813888 bytes, 1073741824 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes


Disk /dev/mapper/centos-root: 53.7 GB, 53687091200 bytes, 104857600 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes


Disk /dev/mapper/centos-swap: 4294 MB, 4294967296 bytes, 8388608 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes


Disk /dev/mapper/centos-home: 490.7 GB, 490695819264 bytes, 958390272 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes

可以看到我的/dev/sdb没有挂载使用,我们来挂载一下:
分区我就不说了,自己找教程搞进行了

  • 新建挂载点mkdir /data
  • mkfs.ext4 /dev/sdb 格式化
  • mount /dev/sdb /data 挂载
  • 查看UUID

    [root@bogon data]# blkid 
    /dev/mapper/centos-root: UUID="4ea36a4c-e3b5-4872-acbd-b874dc8a984f" TYPE="xfs" 
    /dev/sda2: UUID="6Rgi32-MsJD-akY4-4LwU-cILp-cxgM-resSuQ" TYPE="LVM2_member" 
    /dev/sda1: UUID="751824de-225b-44d7-8e1f-8349e189b665" TYPE="xfs" 
    /dev/mapper/centos-swap: UUID="70e13f7a-c6b5-4b79-af93-2bd5ecdb1d3c" TYPE="swap" 
    /dev/sr0: UUID="2018-11-25-23-54-16-00" LABEL="CentOS 7 x86_64" TYPE="iso9660" PTTYPE="dos" 
    /dev/sdb: UUID="86e8ad23-b083-4749-98d4-6baaf231af56" TYPE="ext4" 
    /dev/mapper/centos-home: UUID="317e45d6-c7c9-476a-84c3-eadca5133e21" TYPE="xfs"
  • 修改/etc/fstab,在最后增加如下内容,开机自动挂载

    # 具体的参数自己看一下手册
    UUID=86e8ad23-b083-4749-98d4-6baaf231af56       /data             ext4    defaults        0 0
  • 目录创建

    [root@bogon data]# tree
    .
    ├── logs
    │   ├── mysql
    │   ├── nginx
    │   └── php
    ├── lost+found
    └── website
        └── nginx-php
            └── index.php
  • index.php 文件中写入<?php phpinfo();

测试

  • 安装测试

    [root@bogon php-7.3.9]# /usr/local/php/7.3/bin/php -v
    PHP 7.3.9 (cli) (built: Sep  2 2019 23:31:10) ( NTS )
    Copyright (c) 1997-2018 The PHP Group
    Zend Engine v3.3.9, Copyright (c) 1998-2018 Zend Technologies
  • 启动php-fpm测试

    [root@bogon php-fpm.d]# /etc/init.d/php-fpm start
    Starting php-fpm  done
    [root@bogon php-fpm.d]# ps -ef | grep php-fpm
    root      3540     1  0 03:20 ?        00:00:00 php-fpm: master process (/usr/local/php/7.3/etc/php-fpm.conf)
    www       3541  3540  0 03:20 ?        00:00:00 php-fpm: pool www
    www       3542  3540  0 03:20 ?        00:00:00 php-fpm: pool www
    root      3567 11602  0 03:21 pts/0    00:00:00 grep --color=auto php-fpm
  • nginx && php-fpm测试
    配置一个server,配置文件使用laradock中的laravel.conf.example

    server {
       listen 80;
       listen [::]:80;
    
       # For https
       # listen 443 ssl;
       # listen [::]:443 ssl ipv6only=on;
       # ssl_certificate /etc/nginx/ssl/default.crt;
       # ssl_certificate_key /etc/nginx/ssl/default.key;
    
       server_name nginx_php.test;
       root /data/website/nginx-php;
       index index.php index.html index.htm;
    
       location / {
           try_files $uri $uri/ /index.php$is_args$args;
       }
    
       location ~ \.php$ {
           try_files $uri /index.php =404;
           fastcgi_pass php-upstream;
           fastcgi_index index.php;
           fastcgi_buffers 16 16k;
           fastcgi_buffer_size 32k;
           fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
           #fixes timeouts
           fastcgi_read_timeout 600;
           include fastcgi_params;
       }
    
       location ~ /\.ht {
           deny all;
       }
    
       location /.well-known/acme-challenge/ {
           root /var/www/letsencrypt/;
           log_not_found off;
       }
       
       # 改一下日至存放目录,顺便改一下 nginx.conf 中的Nginx日志存放目录
       error_log /data/logs/nginx/nginx_php_error.log;
       access_log /data/logs/nginx/nginx_php_access.log;
    }

    接下来:

    • 创建一个upstream.conf文件,用以统一管理php-fpm链接,负载均衡

      [root@bogon conf]# pwd
      /usr/local/nginx/conf
      [root@bogon conf]# cat upstream.conf
      upstream php-upstream {
          server 127.0.0.1:9000;
      }
    • 创建vhost目录,并修改nginx.conf,在http {}模块中最下面增加如下配置

      vim /usr/local/nginx/nginx.conf;
      include upstream.conf;
      include vhost/*.conf;
    • vhost文件夹中创建nginx_php_test.conf文件,上面的nginx配置粘贴进去
    • 在本地绑定hosts ip server_name,比如:192.168.2.41 nginx_php.test
    • 访问nginx_php.test
      《Centos7 编译安装 LNMP -- 可能是全网最全》
    • 查看日志

      [root@bogon nginx]# tree
      .
      ├── access.log
      ├── error.log
      ├── nginx_php_access.log
      └── nginx_php_error.log
      
      '0' directories, 4 files # 格式问题 0 引号引起来
      [root@bogon nginx]# pwd
      /data/logs/nginx

至此,测试完毕

Redis

redis 安装比较简单,直接按照下载页面的说明安装即可

[root@bogon redis-5.0.5]# make test
cd src && make test
make[1]: Entering directory `/root/source/redis-5.0.5/src'
    CC Makefile.dep
make[1]: Leaving directory `/root/source/redis-5.0.5/src'
make[1]: Entering directory `/root/source/redis-5.0.5/src'
You need tcl 8.5 or newer in order to run the Redis test
make[1]: *** [test] Error 1
make[1]: Leaving directory `/root/source/redis-5.0.5/src'
make: *** [test] Error 2

yum install -y tcl

想了解TCL的,请点击此链接
注意点

  • 请仔细阅读一遍安装包中的 README.md 文件,文档中有很多安装的注意事项,和教程。
  • redis 编译时如果想指定安装目录,请执行 make PREFIX=/some/other/directory install
  • redis 有一个安装脚本,文档解释的也很清楚

    Make install will just install binaries in your system, but will not configure

    init scripts and configuration files in the appropriate place. This is not

    needed if you want just to play a bit with Redis, but if you are installing

    it the proper way for a production system, we have a script doing this

    for Ubuntu and Debian systems:

      % cd utils
      % ./install_server.sh
    

    The script will ask you a few questions and will setup everything you need

    to run Redis properly as a background daemon that will start again on

    system reboots.c

    啥作用呢,我替你们提前执行了一下(网上一堆 新建 etc 复制 redis.conf 到 etc 安装教程,不想说啥):

    [root@bogon redis-5.0.5]# cd utils/
    [root@bogon utils]# ./install_server.sh 
    Welcome to the redis service installer
    This script will help you easily set up a running redis server
    
    Please select the redis port for this instance: [6379] 
    Selecting default: 6379 # 指定端口,默认 6379
    Please select the redis config file name [/etc/redis/6379.conf] 
    Selected default - /etc/redis/6379.conf # 指定配置文件,默认就是这个
    Please select the redis log file name [/var/log/redis_6379.log] /data/logs/redis/redis_6379.log # 日志文件
    Please select the data directory for this instance [/var/lib/redis/6379] /data/redis/7379 # 数据持久化
    Please select the redis executable path [/usr/local/bin/redis-server] /usr/local/redis/bin/redis-server
    Selected config:
    Port           : 6379
    Config file    : /etc/redis/6379.conf
    Log file       : /data/logs/redis/redis_6379.log
    Data dir       : /data/redis/7379
    Executable     : /usr/local/redis/bin/redis-server
    Cli Executable : /usr/local/bin/redis-cli
    Is this ok? Then press ENTER to go on or Ctrl-C to abort.
    Copied /tmp/6379.conf => /etc/init.d/redis_6379 # 帮你创建了服务器启动脚本
    Installing service...
    Successfully added to chkconfig!
    Successfully added to runlevels 345!
    Starting Redis server... # 帮你启动了 redis 服务
    Installation successful!
  • redis 服务的启动和停止

    [root@bogon utils]# /etc/init.d/redis_6379 -h
    Please use start, stop, restart or status as first argument

Mysql

下载地址
《Centos7 编译安装 LNMP -- 可能是全网最全》
为啥要选这个?因为我们要从源码编译安装。Centos又不在列表中,所以选择通用。最后筛选出来两个包,一个包包含了我们不知道的东西,一个纯净版,下载纯净版。
Mysql 源安装官网安装教程,建议大家读一遍,因为在百度的时候,发现一个无奈的事情:

The InnoDB, MyISAM, MERGE, MEMORY, and CSV engines are mandatory (always compiled into the server) and need not be installed explicitly.

文档中明确说,这几个数据库引擎是强制安装的,网上一大堆设置cmake参数开启这四个引擎的安装教程。如果你需要关闭默认引擎或者安装新引擎,我觉得可以设置参数。

依赖安装

就是Mysql官网的先决条件,在这里简单说一下。

  • 推荐使用 GNU make 对应的命令是 gmake 编译。感兴趣的可以了解一下 cmake make gmake他们的历史,不同,使用场景(这里有个坑,下文有说明)。
  • 需要cmake维基百科
    官方下载地址 官方编译安装教程
    注意:CMake has bootstrapped. Now run gmake. 推荐使用的是 gmake
  • 需要boost维基百科

    • 第一种方式:自行下载解压,cmake 时设置cmake参数 -DWITH_BOOST=<指定解压后目录>
    • 第二种方式:cmake 时设置cmake参数-DDOWNLOAD_BOOST=1 -DWITH_BOOST=<指定自动下载后存储路径>
  • 需要ncurses, 维基百科。系统中已经存在这个库,但是没有开发版,也就是ncurses-devel,命令:yum install -y ncurses-devel
  • mysql编译安装参数文档

安装过程

tar -zxf mysql-8.0.17.tar.gz
cd mysql-8.0.17
mkdir bld # 建一个编译文件存储目录(刚开始不信邪,确实挺多文件的)
cd bld

# 这里我们可以再加一个编译参数 MYSQLX_UNIX_ADDR & MYSQL_UNIX_ADDR
# 因为 mysql 默认的 socket file 位置在 /tmp/ 目录下,但是 mysql 配置文件中却是在 /var/lib/mysql/ 目录下为了保持一致。当然你也可以修改配置文件。
# 关于 mysqlx 是个什么鬼,大家在编译参数页面搜索一下 -DWITH_MYSQLX 关键词,下面有说明。
cmake .. -DDOWNLOAD_BOOST=1 -DWITH_BOOST=<指定自动下载后存储路径> -DMYSQLX_UNIX_ADDR=file_name -DMYSQL_UNIX_ADDR=file_name

make && make install # 强烈建议使用 -j 参数,不然很慢

# 来自官方文档
# Postinstallation setup
shell> cd /usr/local/mysql
shell> mkdir mysql-files
shell> chown mysql:mysql mysql-files
shell> chmod 750 mysql-files
shell> bin/mysqld --initialize --user=mysql
shell> bin/mysql_ssl_rsa_setup
shell> bin/mysqld_safe --user=mysql &
# Next command is optional
shell> cp support-files/mysql.server /etc/init.d/mysql.server

遇到的问题

cmake 没装 依赖安装中已解释

[root@bogon bld]# cmake ..
-- Running cmake version 2.8.12.2
CMake Warning at CMakeLists.txt:43 (MESSAGE):
  Please use cmake3 rather than cmake on this platform


-- Please install cmake3 (yum install cmake3)
CMake Error at CMakeLists.txt:73 (CMAKE_MINIMUM_REQUIRED):
  CMake 3.5.1 or higher is required.  You are running version 2.8.12.2


-- Configuring incomplete, errors occurred!

ncurses 没装 依赖安装中已解释

-- Could NOT find Curses (missing: CURSES_LIBRARY CURSES_INCLUDE_PATH)

GCC 版本太低

CMake Error at cmake/os/Linux.cmake:59 (MESSAGE):
  GCC 5.3 or newer is required (-dumpversion says 4.8.5)
Call Stack (most recent call first):
  CMakeLists.txt:424 (INCLUDE)

-- Configuring incomplete, errors occurred!
See also "/root/source/mysql-8.0.17/bld/CMakeFiles/CMakeOutput.log".

思考:

  • GCC版本太高的话,之前使用低版本GCC编译的软件是否会存在问题?有兴趣的可以测试一下
  • GCC了解的很少人,编译安装我觉得有很多隐患,所以不建议编译安装。有兴趣的可以研究一下这个 Software Collections

再次提醒,编译自己不熟悉的软件有风险,请谨慎

  • SCL(Software Collections) 升级 gcc
    gcc在这个开发者软件工具集中,如果你不想为了装个gcc而把整个工具集都装上,请使用 yum search devtoolset-8 然后将列出来的包安装上即可,例如:

    yum search devtoolset-8
      
    devtoolset-8-gcc.x86_64 : GCC version 8
    devtoolset-8-gcc-c++.x86_64 : C++ support for GCC version 8
      
    yum install devtoolset-8-gcc-c++ devtoolset-8-gcc

    这里有一篇文章,讲了什么是软件集合,以及怎么使用

    # 下面这个命令就别敲了,人家都明说了,ON RHEL,网上又是一堆互相抄袭,不知道看一下文档会不会死
    yum-config-manager --enable rhel-server-rhscl-7-rpms
      
    # 要在系统上启用对软件集的支持,以便启用和构建软件集,您需要安装软件包scl-utils和scl-utils-build
    yum install scl-utils scl-utils-build
    yum install devtoolset-7
    scl enable devtoolset-7 bash
      
    # 测试
    [root@bogon source]# gcc -v
    Using built-in specs.
    COLLECT_GCC=gcc
    COLLECT_LTO_WRAPPER=/opt/rh/devtoolset-7/root/usr/libexec/gcc/x86_64-redhat-linux/7/lto-wrapper
    Target: x86_64-redhat-linux
    Configured with: ../configure --enable-bootstrap --enable-languages=c,c++,fortran,lto --prefix=/opt/rh/devtoolset-7/root/usr --mandir=/opt/rh/devtoolset-7/root/usr/share/man --infodir=/opt/rh/devtoolset-7/root/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-shared --enable-threads=posix --enable-checking=release --enable-multilib --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-linker-build-id --with-gcc-major-version-only --enable-plugin --with-linker-hash-style=gnu --enable-initfini-array --with-default-libstdcxx-abi=gcc4-compatible --with-isl=/builddir/build/BUILD/gcc-7.3.1-20180303/obj-x86_64-redhat-linux/isl-install --enable-libmpx --enable-gnu-indirect-function --with-tune=generic --with-arch_32=i686 --build=x86_64-redhat-linux
    Thread model: posix
    gcc version 7.3.1 20180303 (Red Hat 7.3.1-5) (GCC)
      
    # 执行 Mysql 的编译安装
  • 编译安装 官方文档,文档中有五个步骤大家仔细看

    • 下载,解压,进入软件目录,我们下载最新版本9.2(生产环境请注意)。
    • 执行./contrib/download_prerequisites下载依赖包,就是官方文档中提到的几个依赖库:

      MPFR Library version 2.4.2 (or later)

      MPC Library version 0.8.1 (or later)

      isl Library version 0.15 or later.

      zstd Library.

    • 创建编译目录mkdir gcc-build && cd gcc-build
    • ../configure --prefix=/usr/local/gcc/9.2.0 --enable-checking=release --enable-languages=c,c++ --disable-multilib
      –enable-checking=release 增加一些检查,也可以–disable-checking生成的编译器在编译过程中不做额外检查
      –enable-languages=c,c++ 设置gcc支持的编程语言
      –disable-multilib 取消多目标库编译(取消32位库编译)
      prefix参数建议加上,不然你都不知道编译安装完的路径
    • make && make install 建议增加 -j 参数 不然真的很慢
    • 升级gcc动态库(生产环境请备份)动态连接器维基百科 ldconfig详解

      # 备份,没有对应的二进制可执行文件就跳过
      mv /usr/bin/gcc /usr/bin/gcc.old
      mv /usr/bin/g++ /usr/bin/g++.old
      mv /usr/bin/c++ /usr/bin/c++.old
      mv /usr/bin/cpp /usr/bin/cpp.old
      mv /usr/bin/gcov /usr/bin/gcov.old
      # 将新的二进制文件链接过去
      ln -sf /usr/local/gcc/9.2.0/bin/* /usr/bin/
      
      ldconfig
      # 将新的动态库路径写入到 ld.so.conf
      [root@localhost gcc-build-9.2.0]# echo /usr/local/gcc/9.2.0/lib64 >> /etc/ld.so.conf
      # 执行
      [root@localhost gcc-build-9.2.0]# ldconfig 
      ldconfig: /usr/local/gcc/9.2.0/lib64/libstdc++.so.6.0.27-gdb.py is not an ELF file - it has the wrong magic bytes at the start.
      # 删除 libstdc++.so.6.0.27-gdb.py
      [root@localhost gcc-build-9.2.0]# rm -rf /usr/local/gcc/9.2.0/lib64/libstdc++.so.6.0.27-gdb.py 
      [root@localhost gcc-build-9.2.0]# ldconfig
      
      [root@localhost gcc-build-9.2.0]# cp /usr/local/gcc/9.2.0/lib64/libstdc++.so.6.0.27 /lib64/
      [root@localhost gcc-build-9.2.0]# cd /lib64/
      [root@localhost lib64]# ln -sf libstdc++.so.6.0.27 libstdc++.so.6
    • 可能的报错

      # 1
      [root@bogon gcc-9.2.0]# ./contrib/download_prerequisites
      gmp-6.1.0.tar.bz2: OK
      mpfr-3.1.4.tar.bz2: OK
      mpc-1.0.3.tar.gz: OK
      isl-0.18.tar.bz2: OK
      tar (child): lbzip2: Cannot exec: No such file or directory
      tar (child): Error is not recoverable: exiting now
      tar: Child returned status 2
      tar: Error is not recoverable: exiting now
      error: Cannot extract package from gmp-6.1.0.tar.bz2
      # 解决办法:`yum install -y bzip2`
      
      # 2
      -- Running cmake version 3.15.2
      -- Could NOT find Git (missing: GIT_EXECUTABLE) 
      -- MySQL 8.0.17
      -- The C compiler identification is unknown
      CMake Error at CMakeLists.txt:310 (PROJECT):
        The CMAKE_C_COMPILER:
      
          /usr/bin/cc
      
        is not a full path to an existing compiler tool.
      
        Tell CMake where to find the compiler by setting either the environment
        variable "CC" or the CMake cache entry CMAKE_C_COMPILER to the full path to
        the compiler, or to the compiler name if it is in the PATH.
      # 解决办法
      [root@bogon bld]# whereis gcc
      gcc: /usr/lib/gcc /usr/local/bin/gcc /usr/local/lib/gcc /usr/libexec/gcc
      [root@bogon bld]# ln -s /usr/local/bin/gcc /usr/bin/cc
      
      # 3
      configure: error: no acceptable C compiler found in $PATH
      # 解决办法
      [root@localhost gcc-build-9.2.0]# yum install -y gcc-c++
    • 报找不到 gcc 命令时请退出终端重新登陆或者直接通过执行 source ~/.bashrc 两者原理一致
    • make install 错误:
      查了官方论坛有这个问题的描述,但是没有解决方法:

      /root/source/mysql-8.0.17/storage/innobase/buf/buf0buf.cc: In function ‘void buf_pool_create(buf_pool_t*, ulint, ulint, std::mutex*, dberr_t&)’:
      /root/source/mysql-8.0.17/storage/innobase/buf/buf0buf.cc:1220:44: error: ‘SYS_gettid’ was not declared in this scope
       1220 |   setpriority(PRIO_PROCESS, (pid_t)syscall(SYS_gettid), -20);
            |                                            ^~~~~~~~~~
      [ 95%] Building CXX object storage/innobase/CMakeFiles/innobase.dir/data/data0type.cc.o
      [ 95%] Building CXX object storage/innobase/CMakeFiles/innobase.dir/dict/dict0boot.cc.o
      [ 95%] Building CXX object storage/innobase/CMakeFiles/innobase.dir/dict/dict0crea.cc.o
      make[2]: *** [storage/innobase/CMakeFiles/innobase.dir/buf/buf0buf.cc.o] Error 1
      make[2]: *** Waiting for unfinished jobs....
      make[1]: *** [storage/innobase/CMakeFiles/innobase.dir/all] Error 2
      make: *** [all] Error 2

      经过测试发现不是版本的问题,这个问题产生的原因是没有更新动态链接库。

    • 登陆错误 Segmentation fault
      这个错误是这样产生的:
      官方提示我们用 GUN make

      A good make program. Although some platforms come with their own make implementations, it is highly recommended that you use GNU make 3.75 or higher. It may already be available on your system as gmake. GNU make is available from
      http://www.gnu.org/software/m…

      然后查找资料发现,在centos7GNU make 就是gmake,所以我用 gmake进行了编译,最后在执行mysql -uroot -p时报错了。后来用 make 编译就没这个错了
      但是我看了 gmake 和make的版本信息,一毛一样,令人困惑:

      [root@bogon mysql]# make -v
      GNU Make 4.2.1
      Built for x86_64-redhat-linux-gnu
      Copyright (C) 1988-2016 Free Software Foundation, Inc.
      License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
      This is free software: you are free to change and redistribute it.
      There is NO WARRANTY, to the extent permitted by law.
      [root@bogon mysql]# gmake -v
      GNU Make 4.2.1
      Built for x86_64-redhat-linux-gnu
      Copyright (C) 1988-2016 Free Software Foundation, Inc.
      License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
      This is free software: you are free to change and redistribute it.
      There is NO WARRANTY, to the extent permitted by law.

配置

  • 根据需求修改 /etc/my.cnf。强调一点,只要是mysql服务可能用到的文件或者目录,请设置用户用户组为mysql,不然可能会导致mysql执行失败

    chown mysql:mysql mysql-files
    chmod 750 mysql-files
  • 数据目录初始化bin/mysqld --initialize --user=mysql
    建议大家仔细读一下文档, 要了解:初始化过程中mysql做了哪些工作?默认的帐号密码是什么?初始化都有那些参数?为什么初始化失败了?
    友情提示:初始化后的密码在日志中,具体文档

    2019-09-06T03:29:36.109917Z 0 [Warning] [MY-011070] [Server] 'Disabling symbolic links using --skip-symbolic-links (or equivalent) is the default. Consider not using this option as it' is deprecated and will be removed in a future release.
    2019-09-06T03:29:36.110151Z 0 [System] [MY-013169] [Server] /usr/local/mysql/bin/mysqld (mysqld 8.0.17) initializing of server in progress as process 11886
    2019-09-06T03:29:38.652784Z 5 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: MppJU;a1Yh;t # password
    2019-09-06T03:29:40.142765Z 0 [System] [MY-013170] [Server] /usr/local/mysql/bin/mysqld (mysqld 8.0.17) initializing of server has completed
  • 启动

    cp support-files/mysql.server /etc/init.d/mysql.server
    /etc/init.d/mysql.server start
  • 登陆数据库

    [root@bogon mysql]# /usr/local/mysql/bin/mysql -uroot -p
    Enter password: 
    Welcome to the MySQL monitor.  Commands end with ; or \g.
    Your MySQL connection id is 8
    Server version: 8.0.17
      
    Copyright (c) 2000, 2019, 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> exit
    Bye
      
  • 修改默认密码

    ALTER USER 'root'@'localhost' IDENTIFIED BY 'root-password';

测试

[root@bogon mysql]# ps -ef | grep mysql
root     12396     1  0 Sep05 pts/0    00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/data/mysql --pid-file=/data/mysql/bogon.pid

到此,mysql安装完毕

Laravel跑起来

维基百科及其他资料

zlib
mbstring
autoconf

安装composer

# 要嫌绝对路径烦人,你们可以把`php`可执行文件链接到`/usr/local/bin`,或者自己把`php`可执行文件路径添加到环境变量
# 下面的报错可能都是 XX 功能没开启,XX扩展没装。我就不解释了。
# 命令中没有 cd 命令,bash 名称后的路径变化
[root@bogon website]# /usr/local/php/7.3/bin/php -r "copy('https://install.phpcomposer.com/installer', 'composer-setup.php');"

Warning: copy(): Unable to find the wrapper "https" - did you forget to enable it when you configured PHP? in Command line code on line 1

Warning: copy(): Unable to find the wrapper "https" - did you forget to enable it when you configured PHP? in Command line code on line 1

Warning: copy(https://install.phpcomposer.com/installer): failed to open stream: No such file or directory in Command line code on line 1

[root@bogon php-7.3.9]# make clean
find . -name \*.gcno -o -name \*.gcda | xargs rm -f
find . -name \*.lo -o -name \*.o | xargs rm -f
find . -name \*.la -o -name \*.a | xargs rm -f
find . -name \*.so | xargs rm -f
find . -name .libs -a -type d|xargs rm -rf
rm -f libphp7.la sapi/cli/php sapi/cgi/php-cgi  sapi/fpm/php-fpm  modules/* libs/*
# openssl Nginx 编译的时候已经讲了,这里就不多说了
[root@bogon php-7.3.9]# ./configure --prefix=/usr/local/php/7.3 --enable-fpm --with-fpm-user=www --with-fpm-group=www --sbindir=/usr/local/php/7.3/bin --with-config-file-path=/usr/local/php/7.3/etc --with-openssl
[root@bogon php-7.3.9]# make && make install
[root@bogon website]# /usr/local/php/7.3/bin/php -r "copy('https://install.phpcomposer.com/installer', 'composer-setup.php');"
[root@bogon website]# /usr/local/php/7.3/bin/php composer-setup.php
Downloading...

Composer (version 1.9.0) successfully installed to: /data/website/composer.phar
Use it: php composer.phar

Some settings on your machine may cause stability issues with Composer.
If you encounter issues, try to change the following:

The zlib extension is not loaded, this can slow down Composer a lot.
If possible, install it or recompile php with --with-zlib

A php.ini file does not exist. You will have to create one.
If you can not modify the ini file, you can also run `php -d option=value` to modify ini values on the fly. You can use -d multiple times.
# 缺少 zlib,编译项上增加 --with-zlib。zlib 上文有维基百科链接
[root@bogon php-7.3.9]# make clean
[root@bogon php-7.3.9]# ./configure --prefix=/usr/local/php/7.3 --enable-fpm --with-fpm-user=www --with-fpm-group=www --sbindir=/usr/local/php/7.3/bin --with-config-file-path=/usr/local/php/7.3/etc --with-openssl --with-zlib
[root@bogon php-7.3.9]# make && make install
[root@bogon website]# /usr/local/php/7.3/bin/php composer-setup.php
All settings correct for using Composer
Downloading...

Composer (version 1.9.0) successfully installed to: /data/website/composer.phar
Use it: php composer.phar

[root@bogon website]# /usr/local/php/7.3/bin/php -r "unlink('composer-setup.php');"
# 全局安装,意思就是添加到环境变量下,就可以全系统使用
[root@bogon website]# mv composer.phar /usr/local/bin/composer
[root@bogon website]# composer -v
Do not run Composer as root/super user! See https://getcomposer.org/root for details
   ______
  / ____/___  ____ ___  ____  ____  ________  _____
 / /   / __ \/ __ `__ \/ __ \/ __ \/ ___/ _ \/ ___/
/ /___/ /_/ / / / / / / /_/ / /_/ (__  )  __/ /
\____/\____/_/ /_/ /_/ .___/\____/____/\___/_/
                    /_/
Composer version 1.9.0 2019-08-02 20:55:32

安装 Laravel

[root@bogon website]# composer config -g repo.packagist composer https://packagist.phpcomposer.com
[root@bogon website]# composer create-project --prefer-dist laravel/laravel blog
Installing laravel/laravel (v6.0.1)
  - Installing laravel/laravel (v6.0.1): Loading from cache
Created project in blog
> @php -r "file_exists('.env') || copy('.env.example', '.env');"
Loading composer repositories with package information
Updating dependencies (including require-dev)
Your requirements could not be resolved to an installable set of packages.

  Problem 1
    - laravel/framework v6.0.1 requires ext-mbstring * -> the requested PHP extension mbstring is missing from your system.
    - laravel/framework v6.0.0 requires ext-mbstring * -> the requested PHP extension mbstring is missing from your system.
    - laravel/framework 6.x-dev requires ext-mbstring * -> the requested PHP extension mbstring is missing from your system.
    - Installation request for laravel/framework ^6.0 -> satisfiable by laravel/framework[6.x-dev, v6.0.0, v6.0.1].

# 缺少 mbstring扩展
[root@bogon php-7.3.9]# ./configure --prefix=/usr/local/php/7.3 --enable-fpm --with-fpm-user=www --with-fpm-group=www --sbindir=/usr/local/php/7.3/bin --with-config-file-path=/usr/local/php/7.3/etc --with-openssl --with-zlib --enable-mbstring
[root@bogon php-7.3.9]# make clean
[root@bogon php-7.3.9]# make && make install
[root@bogon website]# rm -rf blog/
[root@bogon website]# composer create-project --prefer-dist laravel/laravel blog
# Laravel 6.0 以下的版本在这个阶段可能会报 curl 找不到的问题,不急,往下看
# 在 nginx vhost目录下创建一个 server(也就是增加一个XX.conf文件),修改 server_name self_laravel.test
#  注意命名:一般格式,<企业名|项目名><_><框架名称><_><开发环境>.conf,比方说:self_laravel_test.conf 这样方便管理。随后的日志,数据库名称等也希望以同样的命名方式命名。server_name 我建议按照 <企业名|项目名><_><框架名称><.><开发环境> 命名。
# 链接 mysql, 创建一个数据库 CREATE DATABASE self_laravel_test CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci 这个数据库的命名呢,我建议还是按照 nginx conf 文件的命名来,已有项目就不要求了。
# 修改 .env 文件
# laravel 6.0 以后没有 make:auth 命令,请执行 composer require laravel/ui && php artisan ui vue --auth 这两个命令
# php artisan migrate 执行数据迁移
[root@bogon blog]# php artisan migrate

Illuminate\Database\QueryException  : could not find driver (SQL: select * from information_schema.tables where table_schema = alone_laravel_test and table_name = migrations and table_type = 'BASE TABLE')

pdo-mysql 驱动没装:
在这里我简要说明一下 php 数据库驱动:
pdophp数据库抽象层,让你可以使用一套代码连接不同的数据库,也可以理解为,有了pdo你就可以通过pdo函数连接pdo支持的数据库,但是你还是需要有项目所需的数据库驱动。比方说mysql,你需要安装mysql的驱动,你才能通过pdo函数连接mysql。当然,你可以不是用pdo,而是通过mysql驱动提供的方法连接mysqlphp默认安装了pdopdo-sqlite所以我们按需安装。
相关文档在这里:数据库扩展
在该项目中,我们需要安装mysql的扩展和pdo-mysql扩展,php关于mysql扩展也有很多,我们选择php官方编写的mysql扩展mysqlndpdo-mysqlpdomysql的抽象。详细信息请多查看文档。

[root@bogon php-7.3.9]# ./configure --prefix=/usr/local/php/7.3 --enable-fpm --with-fpm-user=www --with-fpm-group=www --sbindir=/usr/local/php/7.3/bin --with-config-file-path=/usr/local/php/7.3/etc --with-openssl --with-zlib --enable-mysqlnd --with-pdo-mysql=mysqlnd
[root@bogon php-7.3.9]# make clean
[root@bogon php-7.3.9]# make && make install

[root@bogon php-7.3.9]# php -m | grep pdo
pdo_mysql
pdo_sqlite

[root@bogon blog]# php artisan migrate
Illuminate\Database\QueryException  : SQLSTATE[HY000] [2054] The server requested authentication method unknown to the client (SQL: select * from information_schema.tables where table_schema = alone_laravel_test and table_name = migrations and table_type = 'BASE TABLE')

关于这个错误,Changes in MySQL 8.0.4 (2018-01-23, Release Candidate) 文章中有明确的说明。
简单来说就是身份验证插件更新了,但是 mysqlnd 这个扩展还没有更新。
所以我们选择设置mysql的身份验证插件为老插件(全局方式),当然我们也可以在创建新用户时,指定身份验证插件IDENTIFIED WITH mysql_native_password(局部),当然也可以使用其他支持新身份验证的mysql驱动,它是谁呢?mysqli
mysqlimysql的增强版(增强了啥,自己google一下)。它们两个是mysql官方给php写的api(这是文档:MySQL PHP API ),上面用的mysqlndphp官方用C写的api。至于mysqlndmysqli还有啥区别,自己研究吧。
我们选用第一种方式:

vi /etc/my.cnf
# 在[mysqld]标签下,增加如下内容:
default_authentication_plugin=mysql_native_password

[root@bogon blog]# /etc/init.d/mysql.server restart
Shutting down MySQL. SUCCESS! 
Starting MySQL.. SUCCESS!

# 是不是还报错?为啥?因为 root 账户的 身份验证插件默认是 caching_sha2_password
mysql> select plugin from user where user = 'root';
+-----------------------+
| plugin                |
+-----------------------+
| caching_sha2_password |
+-----------------------+
1 row in set (0.00 sec)

mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH 'mysql_native_password';
Query OK, 0 rows affected (0.01 sec)

mysql> select plugin from user where user = 'root';
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.

mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'root';
Query OK, 0 rows affected (0.00 sec)

mysql> select plugin from user where user = 'root';
+-----------------------+
| plugin                |
+-----------------------+
| mysql_native_password |
+-----------------------+
1 row in set (0.00 sec)

mysql> exit
Bye

[root@bogon blog]# php artisan migrate
Migration table created successfully.
Migrating: 2014_10_12_000000_create_users_table
Migrated:  2014_10_12_000000_create_users_table (0.12 seconds)
Migrating: 2014_10_12_100000_create_password_resets_table
Migrated:  2014_10_12_100000_create_password_resets_table (0.02 seconds)
Migrating: 2019_08_19_000000_create_failed_jobs_table
Migrated:  2019_08_19_000000_create_failed_jobs_table (0.01 seconds)

curl 扩展安装(这次没碰见curl扩展问题,但也是经常使用的扩展):

# 首先,安装 curl-devel
yum install -y curl-devel
# 找到 libcurl 位置
whereis curl
# 然后增加 --with-curl
./configure --prefix=/usr/local/php/7.3 --enable-fpm --with-fpm-user=www --with-fpm-group=www --sbindir=/usr/local/php/7.3/bin --with-config-file-path=/usr/local/php/7.3/etc --with-openssl --with-zlib --enable-mysqlnd --with-pdo-mysql=mysqlnd --with-curl=/usr/include/curl
make clean
make && make install
php -m | grep curl

根据不同的业务,会安装不同的扩展,官方扩展 php 给出了编译参数,常用扩展 php软件包中有个ext目录,全部在里面,省去了下载的步骤,和一些非官方的扩展安装方式一致。非官方扩展无法通过编译参数安装,例如: swoole。接下来我们看看这类扩展如何安装:
swoole 扩展安装( 官方文档 ):

[root@bogon source]# tar -zxf swoole-4.4.5.tgz 
[root@bogon source]# cd swoole-4.4.5

[root@bogon swoole-4.4.5]# /usr/local/php/7.3/bin/phpize 
Configuring for:
PHP Api Version:         20180731
Zend Module Api No:      20180731
Zend Extension Api No:   320180731
Cannot find autoconf. Please check your autoconf installation and the
$PHP_AUTOCONF environment variable. Then, rerun this script.
# 上文已经给出维基百科,这里我们用 yum 安装一下,感兴趣的 自己去编译安装

[root@bogon php-7.3.9]# yum install -y autoconf

[root@bogon swoole-4.4.5]# /usr/local/php/7.3/bin/phpize 
Configuring for:
PHP Api Version:         20180731
Zend Module Api No:      20180731
Zend Extension Api No:   320180731

[root@bogon swoole-4.4.5]# ./configure --with-php-config=/usr/local/php/7.3/bin/php-config
[root@bogon swoole-4.4.5]# make && make install

# 将 extension=swoole.so 添加到 php.ini 最后一行,最好给个标签 [swoole]
[root@bogon php-7.3.9]# vi /usr/local/php/7.3/etc/php.ini
[root@bogon php-7.3.9]# php -m | grep swoole
swoole
[root@bogon php-7.3.9]# php -i | grep swoole
swoole
Author => Swoole Team <team@swoole.com>
swoole.display_errors => On => On
swoole.enable_coroutine => On => On
swoole.enable_library => On => On
swoole.enable_preemptive_scheduler => Off => Off
swoole.unixsock_buffer_size => 8388608 => 8388608
swoole.use_shortname => On => On
OLDPWD => /root/source/swoole-4.4.5
$_SERVER['OLDPWD'] => /root/source/swoole-4.4.5

检查服务启动情况

确保我们的服务已经全部启动: php, mysql, nginx, redis

[root@bogon utils]# ps -ef | grep nginx && ps -ef | grep php && ps -ef | grep redis && ps -ef | grep mysql
root      5546  2417  0 03:17 pts/1    00:00:00 grep --color=auto nginx
root     15038     1  0 02:06 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
web      15039 15038  0 02:06 ?        00:00:00 nginx: worker process
root      5548  2417  0 03:17 pts/1    00:00:00 grep --color=auto php
root     27051     1  0 02:15 ?        00:00:00 php-fpm: master process (/usr/local/php/7.3/etc/php-fpm.conf)
www      27052 27051  0 02:15 ?        00:00:00 php-fpm: pool www
www      27053 27051  0 02:15 ?        00:00:00 php-fpm: pool www
root      5508     1  0 02:59 ?        00:00:01 /usr/local/redis/bin/redis-server 127.0.0.1:6379
root      5550  2417  0 03:17 pts/1    00:00:00 grep --color=auto redis
root      5552  2417  0 03:17 pts/1    00:00:00 grep --color=auto mysql
root     20299     1  0 Sep06 ?        00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/data/mysql --pid-file=/data/mysql/bogon.pid
mysql    20466 20299  0 Sep06 ?        00:19:53 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/data/mysql --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=/data/logs/mariadb/mariadb.log --pid-file=/data/mysql/bogon.pid --socket=/tmp/mysql.sock

访问网站

有点丑,哈哈。应该是没执行npm 相关命令。
《Centos7 编译安装 LNMP -- 可能是全网最全》

总结

一直很想写这么一篇文章或者说一直想把复制粘贴的命令弄懂。历时五天,全部实践了一遍,大致了解了一遍,我觉得很好。写这篇文章不是为了让你们复制粘贴,而是顺着这一条线,好好研究一翻。文中给出了很多文档链接,希望大家在之后的工作中,学会查阅文档,不要一味的百度,google
最后,还有一些东西没写,还有很多东西不是很清楚,比方说:各种优化参数,各种编译参数,各种依赖等等。还有一些话来不及说,还有一些担心,还有很多说得不对的地方,不可能十全十美,希望大家见谅,欢迎提出意见和建议。

祝君:不要放弃一颗追求真相的心。

不喜勿喷,转载请标明出处。

    原文作者:amorZhu
    原文地址: https://segmentfault.com/a/1190000020337993
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞