LNMP架构——nginx+mysql5.7+php7

    科技2025-01-09  12

    实验安装环境:关闭防火墙、核心防护、已配好yum本地源

    目录

    Nginx编译安装一、安装依赖性环境包二、创建运行用户、组三、编译安装四、优化路径五、检查配置文件六、启动、停止nginx七、添加nginx系统服务 Mysql5.7编译安装一、安装Mysql环境依赖包二、创建运行用户三、编译安装四、数据库目录权限调整五、建立调整配置文件六、设置环境变量 PHP7编译安装一、安装环境依赖包二、编译安装三、配置及优化FPM模块四、nginx支持PHP功能五、重启验证

    Nginx编译安装

    一、安装依赖性环境包

    [root@host ~]# yum -y install \ > gcc \ > gcc-c++ \ > make \ > pcre-devel \ > expat-devel \ > perl \ > zlib-devel

    二、创建运行用户、组

    [root@host ~]# useradd -M -s /sbin/nologin nginx 创建一个名为nginx的程序用户,-M不让它创建家目录,-s指定shell环境/sbin/nologin不允许登录shell环境

    三、编译安装

    把安装包放到/opt目录下解压

    [root@host ~]# cd /opt [root@host opt]# tar xzvf nginx-1.15.9.tar.gz

    进入nginx文件里,配置参数,编译,安装

    [root@host opt]# cd nginx-1.15.9 [root@host nginx-1.15.9]# ./configure \ --prefix=/usr/local/nginx \ --user=nginx \ --group=nginx \ --with-http_stub_status_module [root@host nginx-1.15.9]# make -j3 [root@host nginx-1.15.9]# make install

    四、优化路径

    为了使 Nginx 服务器的运行更加方便, 可以为主程序 nginx 创建链接文件, 以便管理员 直接执行 “nginx” 命令就可以调用Nginx的主程序

    [root@host nginx-1.15.9]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/ 把命令放到/usr/local/sbin下面,这个目录是$PATH下的变量 [root@host nginx-1.15.9]# ls -l /usr/local/sbin/nginx lrwxrwxrwx 1 root root 27 Sep 16 15:31 /usr/local/sbin/nginx -> /usr/local/nginx/sbin/nginx

    五、检查配置文件

    首先检查配置文件是否正确,只有出现is ok 和 successful,配置文件才正确

    [root@host nginx-1.15.9]# nginx -t nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

    六、启动、停止nginx

    直接运行Nginx即可启动Nginx服务器,这种方式将使用默认的配置文件 [root@host nginx-1.15.9]# nginx [root@host nginx-1.15.9]# yum -y install lynx [root@host nginx-1.15.9]# lynx 127.0.0.1

    本地测试出现测试页,nginx服务开启成功 主程序Nginx支持标准的进程信号,通过kill或killall命令发送HUP信号表示重载配置, QUIT信号表示退出进程, KILL信号表示杀死进程。例如,若使用killall 命令,重载配置、 停止服务的操作分别如下所示(通过"-s"选项指定信号种类)

    七、添加nginx系统服务

    为了使Nginx服务的启动、停止、重载等操作更加方便,可以编写基于CentOs 7.6的 Nginx服务控制文件使用systemctl工具来进行管理. 添加系统服务,到/lib/systemd/system/目录下添加service文件

    [root@host nginx-1.15.9]# vi /lib/systemd/system/nginx.service [Unit] Description=nginx After=network.target [Service] Type=forking PIDFile=/usr/local/nginx/logs/nginx.pid ExecStart=/usr/local/nginx/sbin/nginx ExecReload=/usr/bin/kill -s HUP $MAINPID ExecStop=/usr/bin/kill -s QUIT $MAINPID PrivateTmp=true [Install] WantedBy=multi-user.target [root@host nginx-1.15.9]# chmod 754 /lib/systemd/system/nginx.service //更改权限 [root@host nginx-1.15.9]# systemctl enable nginx.service //开机自启动 Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service. [root@host nginx-1.15.9]# netstat -anutp | grep nginx //先看一下,没有开 [root@host nginx-1.15.9]# systemctl start nginx //打开 [root@host nginx-1.15.9]# netstat -anutp | grep nginx //验证一下,开了 tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 23229/nginx: master

    ##配置参数解释## [Unit] Description=nginx ###描述 After=network.target ####描述服务类别 [Service] Type=forking ###后台运行形式 PIDFile=/usr/local/nginx/logs/nginx.pid ###PID文件位置 ExecStart=/usr/local/nginx/sbin/nginx ###启动服务 ExecReload=/usr/bin/kill -s HUP $MAINPID ###根据PID重载配置 ExecStop=/usr/bin/kill -s QUIT $MAINPID ###根据PID终止进程 PrivateTmp=true [Install] WantedBy=multi-user.target

    Mysql5.7编译安装

    一、安装Mysql环境依赖包

    [root@localhost ~]#yum -y install \ gcc \ gcc-c++ \ make \ ncurses \ ncurses-devel \ bison \ cmake

    二、创建运行用户

    [root@localhost ~] useradd -s /sbin/nologin mysql

    三、编译安装

    获取mysql-boost-5.7.20.tar.gz安装包到opt目录下

    [root@localhost ~]cd /opt [root@localhost opt]# tar xzvf mysql-boost-5.7.20.tar.gz [root@localhost opt]#cd /opt/mysql-5.7.20/ ######配置参数模块 [root@localhost mysql-5.7.20]# cmake \ -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \ -DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock \ -DSYSCONFDIR=/etc \ -DSYSTEMD_PID_DIR=/usr/local/mysql \ -DDEFAULT_CHARSET=utf8 \ -DDEFAULT_COLLATION=utf8_general_ci \ -DWITH_INNOBASE_STORAGE_ENGINE=1 \ -DWITH_ARCHIVE_STORAGE_ENGINE=1 \ -DWITH_BLACKHOLE_STORAGE_ENGINE=1 \ -DWITH_PERFSCHEMA_STORAGE_ENGINE=1 \ -DMYSQL_DATADIR=/usr/local/mysql/data \ -DWITH_BOOST=boost \ -DWITH_SYSTEMD=1 [root@localhost mysql-5.7.20]#make -j3 [root@localhost mysql-5.7.20]#make install 名称解释-DCMAKE_INSTALL_PREFIX=/usr/local/mysql指定工作目录-DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock通讯文件路径-DSYSCONFDIR=/etc配置文件位置-DSYSTEMD_PID_DIR=/usr/local/mysqlPID进程号-DDEFAULT_CHARSET=utf8默认字符集,能够识别简体中文-DDEFAULT_COLLATION=utf8_general_ci默认字符集校对规则,utf8的扩展-DWITH_INNOBASE_STORAGE_ENGINE=1INNOBASE存储引擎-DWITH_ARCHIVE_STORAGE_ENGINE=1ARCHIVE存储引擎-DWITH_BLACKHOLE_STORAGE_ENGINE=1BLACKHOLE存储引擎-DWITH_PERFSCHEMA_STORAGE_ENGINE=1PERFSCHEMA存储引擎-DMYSQL_DATADIR=/usr/local/mysql/data数据目录-DWITH_BOOST=boost支持C++库-DWITH_SYSTEMD=1ID号

    四、数据库目录权限调整

    chown -R mysql:mysql /usr/local/mysql/

    五、建立调整配置文件

    vi /etc/my.cnf [client] port = 3306 default-character-set=utf8 socket = /usr/local/mysql/mysql.sock [mysql] port = 3306 default-character-set=utf8 socket = /usr/local/mysql/mysql.sock [mysqld] user = mysql basedir = /usr/local/mysql datadir = /usr/local/mysql/data port = 3306 character_set_server=utf8 pid-file = /usr/local/mysql/mysqld.pid socket = /usr/local/mysql/mysql.sock server-id = 1 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 chown mysql:mysql /etc/my.cnf

    六、设置环境变量

    echo 'PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH' >> /etc/profile echo 'export PATH' >> /etc/profile source /etc/profile cd /usr/local/mysql/ bin/mysqld \ --initialize-insecure \ //初始化安全 --user=mysql \ --basedir=/usr/local/mysql \ --datadir=/usr/local/mysql/data cp usr/lib/systemd/system/mysqld.service /usr/lib/systemd/system/ systemctl enable mysqld systemctl start mysqld systemctl status mysqld netstat -anpt | grep 3306 mysqladmin -u root -p password "abc123" //刚开始没密码是空的直接回车,然后输入密码abc123,这是新密码,此时新密码已经设立完成,因为是明文,所以会有warning不安全,abc123,这是在root账户下运行的 mysql -u root -p //这个命令敲下,提示要输入密码,这个就是刚才设置的密码abc123

    PHP7编译安装

    一、安装环境依赖包

    yum -y install \ libjpeg \ libjpeg-devel \ libpng libpng-devel \ freetype freetype-devel \ libxml2 \ libxml2-devel \ zlib zlib-devel \ curl curl-devel \ openssl openssl-devel

    二、编译安装

    cd /opt yum -y install bzip2 tar xjvf php-7.1.10.tar.bz2 cd php-7.1.10 ./configure \ --prefix=/usr/local/php \ --with-mysql-sock=/usr/local/mysq//mysql.sock \ --with-mysqli \ --with-zlib \ --with-curl \ --with-gd \ --with-jpeg-dir \ --with-png-dir \ --with-freetype-dir \ --with-openssl \ --enable-fpm \ --enable-mbstring \ --enable-xml \ --enable-session \ --enable-ftp \ --enable-pdo \ --enable-tokenizer \ --enable-zip make && make install cp php.ini-development /usr/local/php/lib/php.ini vi /usr/local/php/lib/php.ini mysqli.default_socket =/usr/local/mysql/mysql.sock date.timezone = Asia/Shanghai /usr/local/php/bin/php -m //验证安装的模块

    三、配置及优化FPM模块

    php三个配置文件

    名称解释php.ini核心配置文件php-fpm.conf进程服务配置文件www.conf扩展配置文件 cd /usr/local/php/etc/ cp php-fpm.conf.default php-fpm.conf cd /usr/local/php/etc/php-fpm.d/ cp www.conf.default www.conf cd /usr/local/php/etc/ vi php-fpm.conf pid = run/php-fpm.pid //修改这里 /usr/local/php/sbin/php-fpm -c /usr/local/php/etc/php.ini netstat -anpt | grep 9000 ln -s /usr/local/php/bin/* /usr/local/bin/ ps aux | grep -c "php-fpm" //结果

    四、nginx支持PHP功能

    vi /usr/local/nginx/conf/nginx.conf //在合适的位置 location ~\.php$ { root html; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name; ###注意目录名称 include fastcgi_params; } vi /usr/local/nginx/html/index.php <?php phpinfo(); ?>

    五、重启验证

    systemctl restart nginx http://20.0.0.24/index.php

    Processed: 0.014, SQL: 8