LNMP+Redis安装

1 系统部署环境要求

1.1 操作系统

CentOS7.6 x64及以上版本 演示版本:

1.2 支撑软件

PHP 7.2.21

NGINX 1.15.12

1.3 数据库

MYSQL 5.7

1.4 对外服务端口

80:WEB服务

2 支撑软件安装配置说明

2.1 Nginx

安装make:

yum -y install gcc automake autoconf libtool make

安装g++:

 yum install gcc gcc-c++

选定安装目录: 

/usr/local/src

安装pcre:

cd /usr/local/src

wget https://ftp.pcre.org/pub/pcre/pcre2-10.33.tar.gz

tar -zxvf pcre2-10.33.tar.gz

cd pcre2-10.33

./configure

make

make install

安装zlib库:

cd /usr/local/src

wget http://zlib.net/zlib-1.2.11.tar.gz

tar -zxvf zlib-1.2.11.tar.gz

cd zlib-1.2.11

./configure

make

make install

安装nginx:

cd /usr/local/src

wget http://nginx.org/download/nginx-1.15.12.tar.gz

tar -zxvf nginx-1.15.12.tar.gz

cd nginx-1.15.12

./configure

make

make install

如果报错:make:*** No rule to make target 'build', needed by 'default'。停止。

执行:

yum -y install openssl openssl-devel

然后重新

make

设置nginx为system服务:

vi /lib/systemd/system/nginx.service

[Unit]

Description=nginx

After=network.target


[Service]

Type=forking

ExecStart=/usr/local/nginx/sbin/nginx

ExecReload=/usr/local/nginx/sbin/nginx -s reload

ExecStop=/usr/local/nginx/sbin/nginx -s quit

PrivateTmp=true


[Install]

WantedBy=multi-user.target

[Unit]:服务的说明Description:描述服务After:描述服务类别[Service]服务运行参数的设置Type=forking是后台运行的形式ExecStart为服务的具体运行命令ExecReload为重启命令ExecStop为停止命令PrivateTmp=True表示给服务分配独立的临时空间

设置开机自启动:

systemctl enable nginx.service

启动命令:

systemctl start nginx.service

重启命令:

systemctl restart nginx.service

停止命令:

systemctl stop nginx.service

如果提示错误:Access denied执行命令:

systemctl daemon-reexec

2.1.1 Nginx使用方法

php-fpm配置文件地址:

/usr/local/php/etc/php-fpm.conf

nginx配置文件地址:

/usr/local/nginx/conf/nginx.conf

Nginx配置方法下面有详细说明。这里简单说明一下:

HTTP协议主配置在http{}里面

每个站点都分别在一个server{}里面,有监听的端口,一般使用80,可绑定多域名

可以把server{}写在外面的配置文档中,然后使用include包含进去即可,方便管理。

一般使用框架都有伪静态(rewrite)配置,详见Nginx详细配置。

2.2 PHP

2.2.1 PHP编译安装

参考文档:

http://php.net/manual/zh/install.unix.nginx.php

安装xml解析工具libxml2:

yum install -y libxml2

yum install -y libxml2-devel

下载解压PHP-2.21

wget https://www.php.net/distributions/php-7.2.21.tar.gz

tar -zxvf php-7.2.21.tar.gz

进入目录

cd php-7.2.21

配置安装变量

./configure --prefix=/usr/local/php --with-curl --with-freetype-dir --with-gd --with-gettext --with-iconv-dir --with-kerberos --with-libdir=lib64 --with-libxml-dir --with-mysqli --with-openssl --with-pcre-regex --with-pdo-mysql --with-pdo-sqlite --with-pear --with-png-dir --with-jpeg-dir --with-xmlrpc --with-xsl --with-zlib --with-bz2 --with-mhash --enable-fpm --enable-bcmath --enable-libxml --enable-inline-optimization --enable-mbregex --enable-mbstring --enable-opcache --enable-pcntl --enable-shmop --enable-soap --enable-sockets --enable-sysvsem --enable-sysvshm --enable-xml --enable-zip

编译源码

make

make install

复制配置文件:

cp /usr/local/php-7.2.21/php.ini-production  /usr/local/php/lib/php.ini

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

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

配置php.ini:

vi /usr/local/php/php.ini  

“cgi.fix_pathinfo=0”  cgi.fix_pathinfo前面的;分号去掉

启动php-fpm服务:

/usr/local/php/sbin/php-fpm

启动完毕之后,php-fpm服务默认使用9000端口,使用 netstat -tln | grep 9000 可以查看端口使用情况:

编辑nginx配置文件/usr/local/nginx/conf/nginx.conf,主要修改nginx的server {}配置块中的内容,修改location块,追加index.php让nginx服务器默认支持index.php为首页:

然后配置.php请求被传送到后端的php-fpm模块,默认情况下php配置块是被注释的,此时去掉注释并修改为以下内容:

 location ~* \.php$ {

        root           html;

        fastcgi_pass   127.0.0.1:9000;

        fastcgi_index  index.php;

        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;

        include        fastcgi_params;

}

这里面很多都是默认的,root是配置php程序放置的根目录,主要修改的就是fastcgi_param中的/scripts为$document_root 修改完这些保存并退出,然后重启nginx:

systemctl  restart nginx

接下来编辑一个测试的php程序,在nginx下的html目录下创建test.php文件,打印一下php配置:

然后打开浏览器输入对应的地址(例):192.168.1.1/test.php 进行访问,看到输出页面,说明nginx和php都配置成功了:

加php-fpm管理器到systemctl中:

vim /usr/lib/systemd/system/php-fpm.service

[Unit]

Description=The PHP FastCGI Process Manager

After=syslog.target network.target


[Service]

Type=simple

PIDFile=/run/php-fpm.pid

ExecStart=/usr/local/php/sbin/php-fpm --nodaemonize --fpm-config /usr/local/php/etc/php-fpm.conf

ExecReload=/bin/kill -USR2 $MAINPID

ExecStop=/bin/kill -SIGINT $MAINPID


[Install]

WantedBy=multi-user.target

systemctl指令

systemctl enable *.service#开机运行服务

systemctl disable *.service#取消开机运行

systemctl start *.service#启动服务

systemctl stop *.service#停止服务

systemctl restart *.service#重启服务

systemctl reload *.service#重新加载服务配置文件

systemctl status *.service#查询服务运行状态

systemctl --failed#显示启动失败的服务

添加环境变量:

echo "export PATH=\$PATH:/usr/local/php/bin" >> /etc/profile

刷新环境变量:

source /etc/profile

查看php版本:

php -v

安装composer:

php -r "copy('https://install.phpcomposer.com/installer', 'composer-setup.php');"

php composer-setup.php

移动composer.phar,这样 composer 就可以进行全局调用:

mv composer.phar /usr/local/bin/composer

切换为国内镜像(非root用户下执行):

composer config -g repo.packagist composer https://packagist.phpcomposer.com

2.2.2 PHP安装Redis扩展:

下载phpredis扩展包:

wget https://github.com/phpredis/phpredis/archive/5.0.2.tar.gz

mv 5.0.2.tar.gz phpredis-5.0.2.tar.gz

解压:

tar -zxvf phpredis-5.0.2.tar.gz phpredis-5.0.2

进入安装目录:

cd phpredis-5.0.2

用phpize生成configure配置文件:

/usr/local/php/bin/phpize

./configure --with-php-config=/usr/local/php/bin/php-config

make

make install

成功结果如下:

配置php支持:

vi /usr/local/php/lib/php.ini

最后一行添加:

extension="redis.so"

然后打开浏览器输入对应的地址(例):192.168.1.1/test.php 进行访问,看到输出页面

2.3 Redis

2.3.1 Redis安装:

cd /usr/local/src

yum install gcc-c++

wget http://download.redis.io/releases/redis-5.0.4.tar.gz

tar -zxvf redis-5.0.4.tar.gz

cd redis-5.0.4

make 

如果出现cc: error: ../deps/hiredis/libhiredis.a: No such file or directory 

执行

make MALLOC=libc

make PREFIX=/usr/local/redis install

cp redis.conf /usr/local/redis

cd /usr/local/redis/

vim redis.conf

更改内容:

    1.绑定端口,port 6379 默认是6379 需要安全组开放端口

    2.绑定IP,bind 192.168.2.39

    3.指定持久化方式,appendonly yes

    4.requirepass redis123 根据需求是否设置密码

配置redis通过systemctl启动:

vim /lib/systemd/system/redis.service

[Unit]

Description=Redis persistent key-value database

After=network.target


[Service]

ExecStart=/usr/local/redis/bin/redis-server /usr/local/redis/redis.conf --daemonize no

ExecStop=/usr/local/redis/bin/redis-cli -h 127.0.0.1 -p 6379 shutdown

[Install]

WantedBy=multi-user.target

systemctl指令

systemctl enable *.service#开机运行服务

systemctl disable *.service#取消开机运行

systemctl start *.service#启动服务

systemctl stop *.service#停止服务

systemctl restart *.service#重启服务

systemctl reload *.service#重新加载服务配置文件

systemctl status *.service#查询服务运行状态

systemctl --failed#显示启动失败的服务

添加环境变量:

echo "export PATH=\$PATH:/usr/local/redis/bin" >> /etc/profile

刷新环境变量:

source /etc/profile

查看是否可以连接redis客户端:

redis-cli 

2.4 Mysql

2.4.1 Mysql安装:

进入安装目录:

cd /usr/local/src

mysql5.7.22编译需要依赖boost包:

wget http://sourceforge.net/projects/boost/files/boost/1.59.0/boost_1_59_0.tar.gz

安装mysql编译依赖包:

yum -y install gcc gcc-c++ ncurses ncurses-devel cmake

下载mysql5.7.22源码:

wget http://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.22.tar.gz

解压boost和mysql包:

tar -zxvf boost_1_59_0.tar.gz

tar -zxvf mysql-5.7.22.tar.gz

cd mysql-5.7.22

cmake编译:

cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/var/lib/mysql -DDOWNLOAD_BOOST=1 -DWITH_BOOST=/usr/local/src/boost_1_59_0 -DSYSCONFDIR=/etc -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_PARTITION_STORAGE_ENGINE=1 -DWITH_FEDERATED_STORAGE_ENGINE=1 -DWITH_BLACKHOLE_STORAGE_ENGINE=1 -DWITH_MYISAM_STORAGE_ENGINE=1 -DENABLE_DTRACE=0 -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_EMBEDDED_SERVER=1

make编译:

make && make install

创建mysql用户,为nologin用户:

useradd -s /sbin/nologin mysql

创建数据目录:

mkdir -p /var/lib/mysql

配置my.cnf:

vim /etc/my.cnf

[client]

port=3306

default-character-set=utf8

socket=/var/lib/mysql/mysql.sock #设置默认scok链接路径


[mysqld]

basedir=/usr/local/mysql #安装路径

port=3306

datadir=/var/lib/mysql #数据路径

socket=/var/lib/mysql/mysql.sock #开始服务时sock存放位置

sql_mode=ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER

加载mysql配置文件:

cd /usr/local/mysql/bin

./mysqld --defaults-file=/etc/my.cnf --user=mysql --initialize-insecure

systemctl添加mysql服务:

vi /usr/lib/systemd/system/mysql.service

[Unit]

Description=MySQL SERVER

SourcePath=/usr/local/mysql/support-files/mysql.server

Before=shutdown.target


[Service]

User=mysql

Type=forking

ExecStart=/usr/local/mysql/support-files/mysql.server start

ExecStop=/usr/local/mysql/support-files/mysql.server stop


[Install]

WantedBy=multi-user.target

systemctl指令

systemctl enable *.service#开机运行服务

systemctl disable *.service#取消开机运行

systemctl start *.service#启动服务

systemctl stop *.service#停止服务

systemctl restart *.service#重启服务

systemctl reload *.service#重新加载服务配置文件

systemctl status *.service#查询服务运行状态

systemctl --failed#显示启动失败的服务

添加环境变量:

echo "export PATH=\$PATH:/usr/local/mysql/bin:/usr/local/mysql/lib" >> /etc/profile

刷新环境变量:

source /etc/profile

设置mysql密码:

echo "set password=password('123456');"| mysql -S /var/lib/mysql/mysql.sock

测试登录:

mysql -uroot -p123456

远程访问:

use mysql;

update user set host=’%’ where user=’root’;

flush privileges;

你可能感兴趣的:(LNMP+Redis安装)