Advertisement

LNMP生产环境部署

阅读量:

一、编译安装mysql

1.1清理安装环境

yum erase mariadb mariadb-server mariadb-libs mariadb-devel -y

userdel -r mysql(删除原来的MySQL用户)

rm -rf /etc/my*

rm -rf /var/lib/mysql

1.2创建MySQL用户

useradd mysql -M -s /bin/nologin

(-M 不创建用户的家目录)

useradd -r mysql -s /bin/nologin

  • 使用-r选项可以实现创建一个系统账户的操作。
  • 这个-r选项也被视为一种特殊的账号设置。
  • 通过useradd -r命令执行该操作时,
    系统将会自动生成较高权限的用户名和组别,
    并且不会为其建立个人文件夹。

1.3从官网下载tar包

wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-boost-5.7.27.tar.gz

1.4安装编译工具

使用yum进行批量安装ncurses软件包及其相关开发版本工具箱:ncurses软件包、nucurses开发版本软件包以及openssl开发版本软件包;同时安装bison工具箱;接着依次安装gcc编译器系列工具箱和gcc-c++编译器版本;完成构建过程;最后安装glibc以及automake工具箱

cmake:

yum -y install cmake

1.5解压

tar xzvf mysql-bootst-5.7.27.tar.gz -C /usr/local/

1.6编译安装

cd /sur/local/mysql-5.7.27/

cmkae . \

-DWITH_BOOST=boost/boost_1_59_0/ \

-DCMAKE_INSTALL_PREFIC=/usr/local/mysql \ 安装目录

-DSYSCONFDIR=/etc \ 配置文件存放(默认可以不安装配置文件)

MySQL 数据目录 配置为 /usr/local/mysql/data;错误日志记录文件 也会存储于此

-DINSTALL_MANDIR=/usr/share/man \ 帮助文档

-DMYSQL_TCP_PORT=3306 \ 默认端口

-DMYSQL_UNTIX_ADDR=/tmp/mysql.sock \ 表示文件路径用于网络通信,并供客户端连接服务器使用。

-DDEFAULT_CHARSTE=utf8 \ 默认字符集。字符集的支持,可以调

-DEXTRA_CHARSETS=all \ 扩展的字符集支持所有的

-DDEFAULT_COLLATION=utf8_general_ci \ 支持的

-DWITH_READLINE=1 \ 上下翻历史命令

-DWITH_SYSTEM=system \ 通过私钥及认证证书实现公钥登录的方式能够实现加密。该方法适用于长时间保持连接的状态,并其缺点在于操作速度较慢

-DWITH_EMBEDDED_SERVER=1 \ 嵌入式数据库

-DWITH_INNOBASE_STORAGE_ENGINE=1 默认的存储引擎,支持外键

make && make install (如果安装出错,想重新安装:

不用重新解压,只需要删除安装目录中的缓存文件CMakeCache.txt)

1.7初始化

cd /usr/local/mysql

chown -R mysql.mysql .

./bin/mysql --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data(切记最后一位密码既可用于登录也可用于修改)

echo 'export PATH=/usr/local/mysql/bin:$PATH' >>/etc/profile

source /etc/profile

echo $PATH

使用vim编辑器打开/etc/my.cnf这个配置文件(如果发现该文件中有记录,则让该文件中的所有内容被注释;然后输入以下命令)

[client]

port = 3306 默认连接端口

socket = /tmp/mysql.sock 用于本地连接socket套数字

default-character-set = utf8 编码

[mysqld]

port = 3306 服务端口号。默认3306

user = mysql mysql启动用户

basedir =/usr/local/mysql mysql安装根目录

datadir = /usr/local/mysql/data mysql数据文件所在位置

socket = /tmp/mysql.sock 为mysql客户程序和服务器之间的本地通讯指定一个套接字文件(用于进行本地数据传输)

character_set_server = utf8数据库默认字符集

1.8启动mysql

cd /usr/local/mysql

./bin/mysql_safe --user=mysql &(启动之后再按一下回车!即可后台运行)

1.9登录mysql

/usr/local/mysql/bin/mysql -uroot -p'初始密码'

mysql>exit(quit/\q)退出

1.10systemctl启动方式

拷贝脚本到/etc/init.d/目录下,并改名mysql

cd /usr/local/mysql

cp support-files/mysql.srver //etc/init.d/mysql

重新加载服务

systemctl daemon-reload

启动mysql数据库,并检查端口监听状态

停止mysql:systemctl stop mysql

启动mysql:systemctl start mysql

二、编译安装 Nginx

1、安装编译 Nginx 依赖包

使用yum批量安装多个开发工具。其中包括gcc系列工具(如gcc和gcc-c++)、make以及其他如zlib、pcre及其相关的开发版本。

2、官网下载 Nginx 安装包

wget https://nginx.org/download/nginx-1.16.0.tar.gz

3、创建 Nginx 运行用户

useradd -s /sbin/nologin -M nginx

4、解压配置 Nginx 编译

tar zxvf nginx-1.22.1.tar.gz -C /usr/local/

cd /usr/local/nginx-1.22.1/

./configure
--users=nginx
--groups=nginx
--prefix=/usr/local/ nginx
--config_path=/etc/ nginx/ngx.conf
--system binaries path=/usr/sbin/ nginx
--error log path=/var/log/ nginx/error.log
--access log path=/var/log/ nginx/access.log
--process id path=/usr/local/ nginx/run zeroes PID file

5、Nginx 编译安装

make && make install

6、测试 Nginx 是否安装成功

nginx -V

7、启动 Nginx 服务

/usr/sbin/nginx

8、验证 Nginx 服务是否启动成功

netstat -lntp | grep nginx

9、系统添加 Nginx 服务

以 systemd 形式添加

1、创建 nginx.service 文件

[vim]
Unit
SetDescription(nginx)
SetAfter(network.target)

[Service]
ServiceType=forking
StartUpScript=/usr/bin/nginx
ReloadScript=/usr/bin/nginx -s reload
StopScript=/usr/bin/nginx -s quit
PrivateTmp=true

[Install]
WantedBy=multi-user.target

单元格:关于服务的内容说明
Describes the service
After describes the service category
The configuration parameters of the Service are set
The running form of a service is forked
The specific command to start a service is ExecStart
The reload command for restarting a service is ExecReload
The stop command for terminating a service is ExecStop
The boolean value PrivateTmp=True allocates independent temporary space for services.
Notice that all commands to start, restart, and stop a [Service] must use absolute paths.
Operation level settings related to installing services can be configured as multi-user, with system operation level set to 3.

2、以 systemctl 方式启动 Nginx

pkill nginx

systemctl daemon-reload

systemctl start nginx

3、查看 Nginx 服务状态

ps -ef | grep nginx

4、验证 Nginx 服务是否成功启动

netstat -ntlp | grep nginx

5、配置 Nginx 服务自动启动

systemctl enable nginx

三、编译安装 Php

1、安装编译环境依赖包

yum -y install gcc gcc-c++ glibc automake autoconf libtool make

2、安装编译 php 依赖库

在root目录使用yum执行安装操作:libxslt-devel、libjpeg、libjpeg-devel等开发版本以及标准库如libpng、freetype等

3、编译安装 Php

1、下载 Php 源码包

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

download the libzip-1.3.2.tar.gz file from the official website of libzip.org.
decompress and extract the source code archive using tar -xf.
change directory to libzip-1.3.2 directory with cd.
run the build configuration script by executing ./configure.
compile the source code by typing make.
install the built software with make install.

2、配置 Php 编译

tar xzvf php-7.3.6.tar.gz -C /usr/local/

cd /usr/local/php-7.3.6/

./configure
−−prefix=/usr/local/php7
−−with-config-file-path=/usr/local/php7
−−with-config-file-scan-dir=/usr/local/php7/php.d
启用mysqlnd −−with-mysqlnd
启用mysqli −−with-mysqli
启用pdo-mysql −−with-pdo-mysql
启用fpm −−with-fpm
设置fpm用户为nginx −−with-fpm-user=nginx
设置fpm组为nginx −−with-fpm-group=nginx
启用gd −−with-gd
启用iconv −−with-iconv
启用xml功能 −−enable-xml
启用共享库功能 −−enable-shmop
启动系统服务功能 −−enable-sysvsem
启用内联优化功能 −¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬ enable-inline-optimization ¬→ 启用内联优化 ¬→ enable-inline-optimization ¬→ 启用多字符正则表达式 enable-mbregex ¬→ 启用多字符字符串处理 enable-mbstring ¬→ 启用FTP服务 enable-ftp ¬→ 启用压缩功能 enable-zip ¬→ 配置openssl组件 enable-with-openssl → 配置zlib组件 enable-with-zlib → 配置libzip组件 enable-with-libzip → 启用系统控制功能 enable-pcntl → 启用socket组件 enable-with-sockets → 启用xmlrpc组件 with-xmlrpc → 配置soap组件 without pear → 不配置pear组件 with-gettext → 配置session组件 with-curl → 配置jpeg目录 with-jpeg-dir → 配置自由型体目录 with-freetype-dir → 启用opcache组件 enable-opcache

3、Php 编译参数说明

--prefix=/usr/local/php7 # 配置安装目录
--with-config-file-path=/usr/local/php7 # 配置文件 php.ini 的路径
--enable-sockets # 开启 socket
--enable-fpm # 启用 fpm 扩展
--enable-cli # 启用 命令行模式 (从 php 4.3.0 之后这个模块默认开启所以可以不用再加此命令)
--enable-mbstring # 启用 mbstring 库
--enable-pcntl # 启用 pcntl (仅 CLI / CGI)
--enable-soap # 启用 soap
--enable-opcache # 开启 opcache 缓存
--disable-fileinfo # 禁用 fileinfo (由于 5.3+ 之后已经不再持续维护了,但默认是开启的,所以还是禁止了吧)(1G以下内存服务器直接关了吧)
--disable-rpath # 禁用在搜索路径中传递其他运行库。
--with-mysqli # 启用 mysqli 扩展
--with-pdo-mysql # 启用 pdo 扩展
--with-iconv-dir # 启用 XMLRPC-EPI 字符编码转换 扩展
--with-openssl # 启用 openssl 扩展 (需要 openssl openssl-devel)
--with-fpm-user=nginx # 设定 fpm 所属的用户
--with-fpm-group=nginx # 设定 fpm 所属的组别
--with-curl # 启用 curl 扩展
--with-mhash # 开启 mhash 基于离散数学原理的不可逆向的php加密方式扩展库

GD

--with-gd # 启用 GD 图片操作 扩展
--with-jpeg-dir # 开启对 jpeg 图片的支持 (需要 libjpeg)
--with-png-dir # 开启对 png 图片支持 (需要 libpng)
--with-freetype-dir # 开启 freetype

xml

\–enable-simplexml # 启用 simplexml 的支持
\–with-libxml-dir # 启用 libxml2 的支持

--enable-debug # 开启 debug 模式

4、编译安装 Php

make && make install

5、创建 php.ini 配置文件

cp php.ini-production /usr/local/php7/etc/php.ini

在Vim的配置文件路径中指定PHP会话存储位置:/usr/local/php7/etc/php.ini +1371 #指定PHP会话存储位置
设置PHP会话保存位置为/tmp/: session.save_path = "/tmp" #打开注释

6、设置php-fpm配置文件

cd /usr/local/php7/etc

cp php-fpm.conf.default php-fpm.conf

vim php-fpm.conf +17
pid = /var/run/php-fpm.pid #将注释取消并修改

PHP FPM连接配置文件

[root@localhost etc]# cd /usr/local/php7/etc/php-fpm.d/
[root@localhost php-fpm.d]# cp www.conf.default www.conf /复制指定默认配置至目标位置,并备注为PHP FPM子配置文件/
[root@localhost php-fpm.d]# vim www.conf
编辑网站配置文件
user = nginx
group = nginx
listen = 127.0.0.1:9000

7、启动 php-fpm

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

8、检查 php-fpm 是否成功启动

ps aux | grep php-fpm

一旦观察到相关进程,则确认启动成功;在进行查询操作时, 该进程基于 nginx 用户身份运行

9、配置 php-fpm 系统环境变量

cd

配置文件路径为"/etc/profile"。
设置PHP_HOME环境变量为"/usr/local/php7"。
将PATH环境变量扩展到当前值、"/php7/bin"以及"/php7/sbin"。

10、重载环境变量

source /etc/profile

11、配置 php-fpm 开机自启动

Unit

Service

Install

12、php-fpm.service 文件说明

Description: 说明文档
After: 之后的内容
[Service]: 配置服务相关参数
Type=fork时采用的是后台运行的形式。
设置具体的执行命令用于启动该服务。
重启操作由特定的重载命令完成。
停止操作则由终止命令触发。
PrivateTmp=True时会为该服务分配独立的临时存储空间。
注意:所有启动/重启/停止操作均需基于绝对路径进行。
[Install]: 在安装级别下配置的服务相关设置,请将其设为多用户模式(即系统安装级别设为3)。

13、重载 systemctl 配置

systemctl daemon-reload

14、停止 php-fpm

pkill php-fpm

15、用 systemctl 启动 php-fpm

systemctl start php-fpm.service

16、设置 php-fpm 开机启动

systemctl enable php-fpm.service

17、php-fpm 管理命令

systemctl stop php-fpm.service # 停止服务

systemctl restart php-fpm.service # 重新启动服务

systemctl status php-fpm.service # 查看服务当前状态

systemctl disable php-fpm.service # 停止开机自启动

四、Nginx 配置支持PHP

1、添加 Nginx 配置

cd /etc/nginx/

vim nginx.conf
#配置如下
server
{
listen 80;
server_name localhost;
index index.html index.htm index.php;
root /usr/local/nginx/html;
location ~ .php { include fastcgi_params; #指定nginx连接php-fpm的常量 fastcgi_pass 127.0.0.1:9000; #连接php-fpm的地址和端口 fastcgi_index index.php; #指定默认页面 fastcgi_param SCRIPT_FILENAME /usr/local/nginx/htmlfastcgi_script_name; #指定站点根目录
}
}

2、添加 php 探测文件

cd /usr/local/nginx/html/

vim index.php

写入:

3、验证 Nginx 关联 php-fpm

1、重启 php-fpm

systemctl restart php-fpm.service

ps -ef|grep php-fpm

应显示:

该root进程以ID码为14259在时间点[小时]:分钟启动(?标记),空闲时间为零。
两个 nginx 进程池 www 在 [小时]:分钟的时间点同时启动(状态标记)。
另一个 root 进程以ID码为14263在时间点[小时]:分钟持续运行一段时间(pts/标志显示已关闭),空闲时间为零并随后执行grep --color=auto php-fpm命令。

2、重载 Nginx 配置

systemctl restart nginx

systemctl status nginx

3、访问验证

如果出现以上说明LNMP环境编译安装完成!

全部评论 (0)

还没有任何评论哟~