Skip to content

Instantly share code, notes, and snippets.

@curder
Last active December 23, 2021 01:56
Show Gist options
  • Save curder/7a460a7a6099ad5061d8edc14e9558d4 to your computer and use it in GitHub Desktop.
Save curder/7a460a7a6099ad5061d8edc14e9558d4 to your computer and use it in GitHub Desktop.
LNMP 环境搭建

LNMP 环境搭建

  • Linux CentOS8.2

  • Nginx 1.19.10

  • MySQL 8.0.23

  • PHP 8.0.3

在参考当前搭建过程前,请确保对centos服务器进行了初始化,可以参考这里

Nginx

Nginx下载与安装

nginx官网地址:http://nginx.org/

点击右侧的download链接进入到下载页面,下载地址:http://nginx.org/en/download.html

当前我们下载的版本是:1.19.10,下载地址是:http://nginx.org/download/nginx-1.19.10.tar.gz

安装 Nginx 依赖

yum install -y zlib-devel pcre-devel openssl-devel

准备 Nginx 用户

useradd -M -s /sbin/nologin nginx

在命令行中执行命令 id nginx 检查是否创建成功,如果输出对应nginx用户的uid、gid和groups信息则表示创建成功。

[root@iZt4n6rg14xyss2ttlvfm1Z ~]# id nginx
uid=1000(nginx) gid=1000(nginx) groups=1000(nginx)

编译和安装

# 解压压缩包并进入到压缩包
tar xf nginx-1.19.10.tar.gz && cd nginx-1.19.10/

# 编译
./configure \
  --prefix=/usr/local/nginx \
  --with-http_stub_status_module \
  --with-http_ssl_module \
  --with-http_random_index_module \
  --with-http_sub_module

# 安装
make && make install

nginx 相关文件安装在 /usr/local/nginx 目录下。

简单配置

user nginx;

修改 nginx 的主配置文件的 /usr/local/nginx/conf/nginx.conf 的内容,由 #user nobody; 修改为 user nginx;

软链接

每次执行 nginx 可执行脚本都需要打全路径会比较麻烦,为了更方便的执行 nginx,可以通过下面的命令创建一个软连接。

ln -s /usr/local/nginx/sbin/nginx /usr/bin/nginx

为了更快的访问nginx服务所在目录,可以在 /etc 下创建一个软连接。

ln -s /usr/local/nginx /etc/nginx

进程管理

systemctl 方式(建议)

拷贝如下内容到 /lib/systemd/system/nginx.service

# 启动
systemctl start nginx

# 关闭
systemctl stop nginx

# 重载
systemctl reload nginx

普通方式

# 配置语法检测
/usr/local/nginx/sbin/nginx -t

# 启动
/usr/local/nginx/sbin/nginx

# 关闭
pkill nginx

# 重载
pkill -HUP nginx # 一般在修改配置文件之后重载配置文件

检查

# 检查进程状态
pstree -up |grep nginx # 查看运行用户和pid

# 检查端口
netstat -tunpl |grep nginx

# 客户端测试
curl -I http://47.241.98.44

自启动

将启动脚本添加到 /etc/rc.local 中即可实现程序的自启动,但是在 CentOS 8中,/etc/rc.local 并没有执行的权限,我们首先需要添加可执行权限。

chmod +x /etc/rc.local

如果使用普通方式管理nginx进程,则需要将 nginx 的启动命令添加到上面的文件中。

#!/bin/bash
# THIS FILE IS ADDED FOR COMPATIBILITY PURPOSES
#
# It is highly advisable to create own systemd services or udev rules
# to run scripts during boot instead of using this file.
#
# In contrast to previous versions due to parallel execution during boot
# this script will NOT be run after all other services.
#
# Please note that you must run 'chmod +x /etc/rc.d/rc.local' to ensure
# that this script will be executed during boot.

touch /var/lock/subsys/local

/usr/local/nginx/sbin/nginx # Nginx

如果使用 systemd 的方式管理 nginx 进程,则可以直接执行命令。

[root@iZt4n6rg14xyss2ttlvfm1Z sbin]# systemctl enable nginx
Created symlink /etc/systemd/system/multi-user.target.wants/nginx.service → /usr/lib/systemd/system/nginx.service.

可以看到创建了一个 nginx 的自启动服务。

MySQL

MySQL的下载与安装

下载页面地址:https://dev.mysql.com/downloads/mysql/

操作系统:源代码方式

内核版本:Generic Linux

然后选择 Compressed TAR Archive, Includes Boost Headers 文件进行下载。

下载地址:https://dev.mysql.com/get/Downloads/MySQL-8.0/mysql-boost-8.0.23.tar.gz

  • MySQL依赖
yum install -y cmake ncurses-devel libtirpc-devel gcc-c++

下载和安装 rpcsvc-proto

wget https://github.com/thkukuk/rpcsvc-proto/releases/download/v1.4.2/rpcsvc-proto-1.4.2.tar.xz

tar -xf rpcsvc-proto-1.4.2.tar.xz && cd rpcsvc-proto-1.4.2
./configure && make && make install

下载和安装 mysql-boost

wget https://dev.mysql.com/get/Downloads/MySQL-8.0/mysql-boost-8.0.23.tar.gz

# 解压并进入到源代码目录
tar xf mysql-boost-8.0.23.tar.gz && cd mysql-8.0.23/

cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DWITH_BOOST=boost -DFORCE_INSOURCE_BUILD=1

make && make install

MySQL的管理

MySQL配置文件

将下列对应的配置文件放在对应目录。

/etc/my.cnf
#
# This group is read both both by the client and the server
# use it for options that affect everything
#
[client-server]

#
# include all files from the config directory
#
!includedir /etc/my.cnf.d
/etc/my.cnf.d/client.cnf
#
# These two groups are read by the client library
# Use it for options that affect all clients, but not the server
#


[client]

# This group is not read by mysql client library,
# If you use the same .cnf file for MySQL and MariaDB,
# use it for MariaDB-only client options
[client-mariadb]
/etc/my.cnf.d/mysql-default-authentication-plugin.cnf
#
# MySQL 8.0.4 introduced 'caching_sha2_password' as its default authentication plugin.
# It is faster and provides better security then the previous default authentication plugin.
#
# Until now (09/2018), it does not work with some other software (eg. MariaDB client, MariaDB connectors,  ...)
#
# This configuration file changes MySQL default server configuration, so it behaves the same way as in MySQL 5.7.
#
# To change the behaviour back to the upstream default, comment out the following lines:

[mysqld]
default_authentication_plugin=mysql_native_password
/etc/my.cnf.d/mysql-server.cnf
#
# This group are read by MySQL server.
# Use it for options that only the server (but not clients) should see
#
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/en/server-configuration-defaults.html

# 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 mysqld according to the
# instructions in http://fedoraproject.org/wiki/Systemd

[mysqld]

#skip-grant-tables

创建 MySQL 用户

useradd -M -s /sbin/nologin mysql

初始化数据库

/usr/local/mysql/bin/mysqld --initialize --user=mysql

初始化时会自动生成一个临时的密码,请记录下来并在下面的重置ROOT密码时会使用到。

启动MySQL

cp support-files/mysql.server /etc/init.d/mysql # 在源码目录拷贝自启动脚本,并添加自启动
systemctl start mysql

# 或者通过下面的命令启动
# /usr/local/mysql/bin/mysqld_safe --user=mysql &

重置 Root 密码

/usr/local/mysql/bin/mysqladmin -uroot -p'bNzu0plkJT:-' password 'NewPwd';

通过上面的命令设置 MySQL 的 root 密码为新密码,原密码通过在上一步初始化数据时得到

客户端登录

/usr/local/mysql/bin/mysql -uroot -pNewPwd

进程管理

# 查看进程
pstree -pu | grep mysql

# 查看端口,默认开起来本机的3306和33060端口
netstat -tunpl|grep mysqld

# 关闭MySQL
pkill mysqld

## MySQL进程没有重载,只能关闭后打开

或者使用 systemctl 命令管理 MySQL 进程

systemctl status mysql # 查看状态
systemctl stop mysql # 关闭
systemctl start mysql # 启动

自启动

systemctl enable mysql

PHP

PHP安装前的准备

安装 PHP 依赖

yum install -y autoconf freetype gd libpng libpng-devel libjpeg libxml2 libxml2-devel zlib curl curl-devel net-snmp-devel libjpeg-devel php-ldap openldap-devel openldap-clients freetype-devel gmp-devel libzip libzip-devel sqlite-devel automake libtool

编译安装依赖包 oniguruma

编译安装 PHP 时,如果 --enable-mbstring , 开启了 mbstring 扩展,需要这个正则处理库。

# 下载压缩包,列表地址:https://github.com/kkos/oniguruma/releases
wget https://github.com/kkos/oniguruma/archive/refs/tags/v6.9.7.tar.gz -O oniguruma-6.9.7.tar.gz

# 解压并进入到源码目录
tar xf oniguruma-6.9.7.tar.gz && cd oniguruma-6.9.7/

# 生成 configure
./autogen.sh

# 生成编译配置文件
./configure --prefix=/usr

# 编译并运行
make && make install

下载并安装PHP

安装之前保证 nignx 用户已存在。

# 下载 php-8.0.3,列表地址:https://www.php.net/downloads.php
wget https://www.php.net/distributions/php-8.0.3.tar.gz -O php-8.0.3.tar.gz

# 解压并进入到目录
tar xf php-8.0.3.tar.gz && cd php-8.0.3/

# 生成编译配置
./configure \
    --prefix=/usr/local/php \
    --with-config-file-path=/usr/local/php/etc \
    --enable-fpm \
    --with-fpm-user=nginx \
    --with-fpm-group=nginx \
    --enable-mysqlnd \
    --with-mysqli \
    --with-pdo-mysql \
    --enable-opcache \
    --with-pcre-jit \
    --enable-gd \
    --with-jpeg \
    --with-freetype \
    --with-gettext \
    --with-curl \
    --with-openssl \
    --enable-sockets \
    --enable-mbstring \
    --enable-xml \
    --with-zip \
    --with-zlib \
    --with-snmp \
    --with-mhash \
    --enable-ftp \
    --enable-bcmath \
    --enable-soap \
    --enable-shmop \
    --enable-sysvsem \
    --enable-pcntl \
    --with-gmp

# 编译并安装
make && make install

注意: 在生成编译配置的参数中,注意--with-fpm-user--with-fpm-group的配置值跟上面 Nginx 给定的用户保持一致。

PHP配置与管理

配置文件

从 php 的源码目录中拷贝对应的 php.ini 配置文件

# 替换下面的 /source-code-directory 为当前解压php的目录
cp /source-code-directory/php-8.0.3/php.ini-production /usr/local/php/etc/php.ini

# 拷贝 php-fpm 进程配置文件
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

建立软链接

ln -s /usr/local/php/bin/php /usr/bin/php
ln -s /usr/local/php/sbin/php-fpm /usr/bin/php-fpm

php-fpm 创建一个软链接,方便在命令行使用。

语法检测

php-fpm -t

进程管理

systemctl 方式(建议)
# 替换下面的 /source-code-directory 为当前解压php的目录
cp /source-code-directory/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm # 从php源码中拷贝文件
chmod +x /etc/init.d/php-fpm # 设置权限
chkconfig --add php-fpm # 添加服务

# 进程管理
systemctl start php-fpm # 启动进程
systemctl stop php-fpm # 停止进程
systemctl restart php-fpm # 重启进程
systemctl reload php-fpm # 重载进程
普通方式
# 启动php-fpm
php-fpm

# 关闭php-fpm
pkill php-fpm

# 重载php-fpm
pkill -USR2 php-fpm

# 开机启动
/usr/local/php/sbin/php-fpm # 添加到 /etc/rc.local 文件中
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment