##
这一篇是使用源码编译搭建lnmp+redis环境,并且使redis和mysql分布在不同的服务器上
## 安装前准备
#虚拟机版本号 centos7.6
uname -a
#Linux localhost.localdomain 3.10.0-957.27.2.el7.x86_64 #1 SMP Mon Jul 29 17:46:05 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux
yum -y install gcc gcc-c++ ncurses ncurses-devel bison ncurses-devel libxml2-devel autoconf
## 安装nginx
1.wget http://nginx.org/download/nginx-1.17.3.tar.gz
2.tar -zxf nginx-1.17.3 -C /usr/local
3.wget https://nchc.dl.sourceforge.net/project/pcre/pcre/8.43/pcre-8.43.tar.gz
4.wget http://www.zlib.net/zlib-1.2.11.tar.gz
5.wget https://www.openssl.org/source/openssl-1.0.2s.tar.gz
7.tar -zxf pcre-8.43.tar.gz -C /usr/local
8.tar -zxf zlib-1.2.11.tar.gz -C /usr/local
9.tar -zxf openssl-1.0.2s.tar.gz -C /usr/local
10.cd nginx-1.17.3
11../configure --prefix=/usr/local/nginx --with-http_ssl_module --with-pcre=/usr/local/pcre-8.43/ --with-zlib=/usr/local/zlib-1.2.11/ --with-openssl=/usr/local/openssl-1.0.2s/
12.make && make install
### 操作命令
1.nginx #启动nginx
2.nginx -t #测试配置文件是否正确
3.nginx -s reload #在不停止nginx的情况下,重新加载配置文件
4.nginx -s stop #关闭nginx
## 安装PHP
1.wget https://www.php.net/distributions/php-7.3.8.tar.gz
2.tar -zxf php-7.3.8.tar.gz -C /usr/local
3.cd php-7.3.8
4../configure --enable-fpm
5.make && make install
6.cp /usr/local/php-7.3.8/php.ini-developmen /usr/local/lib/php.ini
7.vim /usr/local/lib/php.ini
8.修改cgi.fix_pathinfo=0
9.vim /usr/local/etc/php-fpm.conf
10.NONE修改为/usr/local
### 修改nginx配置
server {
listen 80;
server_name localhost;
location / {
root html;
index index.php index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
include fastcgi_params;
}
if (!-e $request_filename) {
rewrite ^(.*)$ /index.php?s=/$1 last;
break;
}
}
启动php-fpm:/usr/local/bin/php-fpm
## 安装MySQL
### 事前准备
---安装cmake
1.wget https://github.com/Kitware/CMake/releases/download/v3.15.2/cmake-3.15.2.tar.gz
2.tar -zxf cmake-3.15.2.tar.gz -C /usr/local
3../configure
4.gmake && gmake install
---添加用户和创建安装目录
1.mkdir -p /application/mysql
2.mkdir -p /application/mysql/data
3.mkdir -p /application/mysql/tmp/
4.mkdir -p /application/mysql/binlog
5.mkdir -p /application/mysql/logs
6.groupadd mysql
7.useradd mysql -s /sbin/nologin -M -g mysql
### 开始安装
1.wget https://cdn.mysql.com/archives/mysql-5.7/mysql-5.7.26.tar.gz
2.wget http://nchc.dl.sourceforge.net/project/boost/boost/1.59.0/boost_1_59_0.tar.gz
3.tar -zxf mysql-5.7.26.tar.gz -C /usr/local
4.tar -zxf boost_1_59_0.tar.gz -C /usr/local/boost
5.cd mysql-5.7.26
6.cmake . -DCMAKE_INSTALL_PREFIX=/application/mysql -DMYSQL_DATADIR=/application/mysql/data -DMYSQL_UNIX_ADDR=/application/mysql/tmp/mysql.sock -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DEXTRA_CHARSETS=gbk,gb2312,utf8,ascii -DENABLED_LOCAL_INFILE=ON -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_FEDERATED_STORAGE_ENGINE=1 -DWITH_BLACKHOLE_STORAGE_ENGINE=1 -DWITHOUT_EXAMPLE_STORAGE_ENGINE=1 -DWITHOUT_PARTITION_STORAGE_ENGINE=1 -DWITH_ZLIB=bundled -DWITH_EMBEDDED_SERVER=1 -DWITH_DEBUG=0 -DWITH_BOOST=/usr/local/boost
7.make && make install
### 配置MySQL系统环境变量
1.echo 'export PATH=/application/mysql/bin:$PATH' >>/etc/profile
2.tail -1 /etc/profile
export PATH=/application/mysql/bin:$PATH
3.source /etc/profile
4.echo $PATH
/application/mysql/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
### 更改MySQL安装目录的属主和属组
chown -R mysql:mysql /application/mysql/
### 修改my.cnf文件的属主和属组
chown mysql:mysql /etc/my.cnf
### 修改my.cnf配置文件
1.vim /etc/my.cnf
[client]
port=3306
socket=/application/mysql/tmp/mysql.sock
default-character-set=utf8
[mysqld]
port=3306
user=mysql
basedir=/application/mysql
datadir=/application/mysql/data
pid-file=/application/mysql/mysqld.pid
socket=/application/mysql/tmp/mysql.sock
tmpdir=/application/mysql/tmp
character_set_server=utf8
server-id=1
max_connections=100
max_connect_errors=10
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_AUTO_VALUE_ON_ZERO,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,PIPES_AS_CONCAT,ANSI_QUOTES
log-bin=/application/mysql/binlog/mysql-bin
log-error=/application/mysql/logs/mysql_5_7_26.err
# Disabling symbolic-links is recommended to prevent assorted security risks
#isymbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd
#[mysqld_safe]
#log-error=/var/log/mariadb/mariadb.log
#pid-file=/var/run/mariadb/mariadb.pid
#
# include all files from the config directory
#
!includedir /etc/my.cnf.d
my.cnf文件常用选项详解
[client]
#默认连接端口
port = 3306
#用于本地连接的socket套接字
socket = /application/mysql/tmp/mysql.sock
#编码
default-character-set = utf8
[mysqld]
#服务端口号,默认3306
port = 3306
#mysql启动用户
user = mysql
#mysql安装根目录
basedir = /application/mysql
#mysql数据文件所在位置
datadir = /application/mysql/data
#pid文件所在目录
pid-file = /application/mysql/mysqld.pid
#为MySQL客户端程序和服务器之间的本地通讯指定一个套接字文件
socket = /application/mysql/tmp/mysql.sock
#MySQL存放临时文件的目录
tmpdir = /application/mysql/tmp
#数据库默认字符集,主流字符集支持一些特殊表情符号(特殊表情符占用4个字节)
character_set_server = utf8
#MySQL服务的唯一编号,每个mysql服务Id需唯一
server-id = 1
#最大连接数
max_connections = 100
#最大错误连接数
max_connect_errors = 10
#sql_mode,定义了mysql应该支持的sql语法,数据校验
#NO_ENGINE_SUBSTITUTION:如果需要的存储引擎被禁用或未编译,可以防止自动替换存储引擎
#STRICT_TRANS_TABLES:存储引擎启用严格模式,非法数据值被拒绝
#NO_AUTO_CREATE_USER:禁止GRANT创建密码为空的用户
#NO_AUTO_VALUE_ON_ZERO:默认情况下自增长列是从1开始的,如果你插入值为0的数据会报错,设置这个之后就可以正常插入为0的数据了
#NO_ZERO_IN_DATE:在严格模式,不接受月或日部分为0的日期
#NO_ZERO_DATE:在严格模式,不要将 ‘0000-00-00′做为合法日期
#ERROR_FOR_DIVISION_BY_ZERO:在严格模式,在INSERT或UPDATE过程中,如果被零除(或 MOD(X,0)),则产生错误(否则为警告)。如果未给出该模式,被零除时MySQL返回NULL
#PIPES_AS_CONCAT:将||视为字符串连接操作符(+)(同CONCAT()),而不视为OR
#ANSI_QUOTES:将‘”'视为识别符引号(‘`'引号字符),不要视为字符串的引号字符
sql_mode = NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_AUTO_VALUE_ON_ZERO,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,PIPES_AS_CONCAT,ANSI_QUOTES
#开启二进制日志功能,binlog数据位置
log-bin = /application/mysql/binlog/mysql-bin
#mysql生成的错误日志存放的路径
log-error = /application/mysql/logs/mysql_5_7_26.err
### 初始化数据库
1.cd /application/mysql/
2../bin/mysqld --initialize-insecure --user=mysql --basedir=/application/mysql --datadir=/application/mysql/data
提示:
#生成初始化密码参数(5.7版本才有),必须要添加,否则会初始化失败,会生成空密码
--initialize-insecure
#设定初始化用户
--user=mysql
#设定mysql工作目录
--basedir=/application/mysql
#设定数据文件目录
--datadir=/application/mysql/data
### 实例开启SSL连接
./bin/mysql_ssl_rsa_setup --initialize-insecure --user=mysql --basedir=/application/mysql --datadir=/application/mysql/data
### 修改data目录下的server-key.pem文件权限
1.cd /application/mysql/data/
2.chmod +r server-key.pem
3.ls -l server-key.pem
-rw-r--r-- 1 root root 1675 Jul 31 20:01 server-key.pem
提示:
如果不给文件server-key.pem其他用户和组可读权限的话,当启动数据库的时候,会报如下错误
2019-08-07T06:39:24.111526Z 0 [ERROR] SSL error: Unable to get private key from 'server-key.pem'
2019-08-07T06:39:24.111555Z 0 [Warning] Failed to set up SSL because of the following SSL library error: Unable to get private key
### 启动MySQL数据库
#拷贝启动脚本到/etc/init.d/目录下,并改名mysqld
1.cp support-files/mysql.server /etc/init.d/mysqld
2.ls -l /etc/init.d/mysqld
-rwxr-xr-x 1 root root 10588 Aug 1 18:33 /etc/init.d/mysqld
#重新加载系统服务,将MySQL数据库加入开机自启动
1./bin/systemctl daemon-reload
2./bin/systemctl enable mysqld.service
mysqld.service is not a native service, redirecting to /sbin/chkconfig.
Executing /sbin/chkconfig mysqld on
#启动MySQL数据库,并检查端口监听状态
1./etc/init.d/mysqld start
2. netstat -lntup
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 6549/sshd
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 7023/master
tcp6 0 0 :::3306 :::* LISTEN 10947/mysqld
tcp6 0 0 :::22 :::* LISTEN 6549/sshd
tcp6 0 0 ::1:25 :::* LISTEN 7023/master
### MySQL数据库基本优化(安全)
#删除全部用户,添加额外管理员,重新加载mysql授予表
1.mysql
2.select user,host from mysql.user;
+---------------+-----------+
| user | host |
+---------------+-----------+
| mysql.session | localhost |
| mysql.sys | localhost |
| root | localhost |
+---------------+-----------+
3.grant all privileges on *.* to name@'ip' identified by 'psd' with grant option;
4.flush privileges;
5.delete from mysql.user where user='mysql.session';
6.delete from mysql.user where user='mysql.sys';
7.delete from mysql.user where user='root';
### 操作命令
/etc/init.d/mysqld stop|start
### 授权远程用户连接MySQL数据的方法
1.grant all privileges on *.* to name@'ip'' identified by 'password'' with grant option;
2.flush privileges;
## 安装redis
1.wget http://download.redis.io/releases/redis-5.0.5.tar.gz
2.tar -zxf redis-5.0.5.tar.gz -C /usr/local
3.cd redis-5.0.5
4../configure --prefix=/usr/local/gcc --enable-languages=c,c++ --disable-multilib
5.make
### 操作命令
1./usr/local/redis.5.0.5/src/redis-server reids.conf #启动已配置文件为基础的redis服务
2./usr/local/redis5.0.5/src/redis-cli #进入命令行
## 安装php_redis和pdo_mysql扩展
### 安装redis扩展
1.wget https://pecl.php.net/get/redis-5.0.2.tgz
2.tar -zxf redis-5.0.2 -C /usr/local
3.cd redis-5.0.3
4./usr/local/bin/phpize
5../configure --with-php-config=/usr/local/bin/php-config
6.make
7.cp /usr/local/redis-5.0.2/modules/redis.so /usr/local/lib/php/extensions/no-debug-non-zts-20180731/
8.kill (php-fpm的进程号)
9./usr/local/bin/php-fpm
### 安装pdo_mysql扩展
1.cd /usr/local/php-7.3.8/ext/pdo_mysql
2./usr/local/bin/phpize
3../configure --with-php-config=/usr/local/bin/config --with-pdo-mysql=/application/mysql
4.make
5.cp /usr/local/php-7.3.8/ext/pdo_mysql/modules/pdo_mysql.so /usr/local/lib/php/extensions/no-debug-non-zts-20180731/
6.kill (php-fpm的进程号)
7./usr/local/bin/php-fpm
## 配置nginx实现路由重写
### nginx.conf配置
server {
listen 80;
server_name localhost;
location / {
root html;
index index.php index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
include fastcgi_params;
}
if (!-e $request_filename) {
rewrite ^(.)$ /index.php?s=/$1 last;
break;
}
}
## 提示
在编译安装的过程中,每完成一步都要测试是否成功在进行下一步;如果外部访问不到服务器内的数据,有可能是防火墙没有关闭,要首先查看防火墙。
## 测试方法
### nginx
1.使用 nginx -t 来检测配置是否成功
2.在外部访问ip,正常显示网页,则安装成功
### 编译安装时的测试
1.每完成一步都可以使用 echo $? 来查看编译情况,若为0,则编译成功