LNMP环境安装步骤
名称 | 版本号 |
---|---|
服务商 | 华为云 |
CentOS | 8.2 64bit |
Nginx | 1.23.2 |
MySQL | 8.0.31 |
PHP | 7.4.16 |
1、服务器为华为云的的先更换yum源
[root@hecs-223575 ~]# cd /etc/yum.repos.d
[root@hecs-223575 yum.repos.d]# mkdir bak // 将当前文件夹下的文件都移动到bak下
#在 /etc/yum.repos.d 文件夹下执行:
[root@hecs-223575 yum.repos.d]# wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-8.repo
crtl+f 搜索并替换:(只需要替换两个地方)
mirrors.cloud.aliyuncs.com 替换为 mirrors.aliyun.com(如果没有改动,这里可以不用替换)
releasever 替换为 releasever-stream
注意:替换所有
然后执行:
[root@hecs-223575 yum.repos.d]# yum clean all
[root@hecs-223575 yum.repos.d]# yum makecache
2、安装nginx
# 安装依赖
[root@hecs-223575 source]# yum -y install zlib-devel pcre-devel openssl-devel gcc
# 下载nginx
[root@hecs-223575 source]# wget https://nginx.org/download/nginx-1.23.2.tar.gz
# 解压
[root@hecs-223575 source]# tar -xvf nginx-1.23.2.tar.gz
# 进入nginx 文件夹
[root@hecs-223575 source]# cd nginx-1.23.2
# 编译、安装
[root@hecs-223575 nginx-1.23.2]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http\_gzip\_static\_module --with-http\_stub\_status\_module --with-http\_ssl\_module
[root@hecs-223575 nginx-1.23.2]# make && make install
# 启动nginx
[root@ecs-223575 nginx-1.23.2]# nginx
-bash: nginx: command not found // 表示nginx命令未引入
[root@ecs-223575 source]# /usr/local/nginx/sbin/nginx
nginx: [emerg] getpwnam("nginx") failed // 没有nginx用户 新增nginx用户及用户组
[root@ecs-223575 source]# groupadd nginx // 新增用户组
[root@ecs-223575 source]# useradd -g nginx nginx // 新增用户nginx 并添加至nginx用户组
[root@ecs-223575 source]# /usr/local/nginx/sbin/nginx
# 将nginx的位置加入到环境变量中
[root@hecs-223575 nginx-1.23.2]# cp /etc/profile /etc/profile.bak
[root@hecs-223575 nginx-1.23.2] echo 'export PATH="/usr/local/nginx/sbin:$PATH"' >> /etc/profile #追加内容
[root@hecs-223575 nginx-1.23.2] source /etc/profile
3、安装MySQL8
# 下载yum包官网地址:https://dev.mysql.com/downloads/repo/yum/
[root@hecs-223575 source]# wget https://dev.mysql.com/get/mysql80-community-release-el8-3.noarch.rpm
[root@hecs-223575 source]# yum install mysql80-community-release-el8-3.noarch.rpm
# 查看该yum源支持哪些版本
[root@ecs-365715 source]# yum repolist enabled | grep mysql
mysql-connectors-community MySQL Connectors Community
mysql-tools-community MySQL Tools Community
mysql80-community MySQL 8.0 Community Server
# 安装
[root@ecs-365715 source]# yum install mysql-community-server
MySQL 8.0 Community Server 574 kB/s | 2.5 MB 00:04
MySQL Connectors Community 39 kB/s | 84 kB 00:02
MySQL Tools Community 169 kB/s | 583 kB 00:03
Last metadata expiration check: 0:00:01 ago on Tue 22 Nov 2022 11:32:22 AM CST.
All matches were filtered out by modular filtering for argument: mysql-community-server
Error: Unable to find a match: mysql-community-server
上述问题原因:centos8官方已经停止提供服务,相应的yum源也已经移到归档源
解决上述方法
1.1
[root@ecs-365715 source]# vim /etc/yum.repos.d/CentOS-Base.repo
注释原来的[BaseOS] [AppStream]添加新的
[BaseOS]
name=Qcloud centos OS - $basearch
baseurl=http://mirrors.cloud.tencent.com/centos/$releasever/BaseOS/$basearch/os/
enabled=1
gpgcheck=1
gpgkey=http://mirrors.cloud.tencent.com/centos/RPM-GPG-KEY-CentOS-Official
[AppStream]
name=Qcloud centos AppStream - $basearch
baseurl=http://mirrors.cloud.tencent.com/centos/$releasever/AppStream/$basearch/os/
enabled=0
gpgcheck=1
gpgkey=http://mirrors.cloud.tencent.com/centos/RPM-GPG-KEY-CentOS-Official
[root@ecs-365715 source]# yum makecache // 重建缓存
[root@ecs-365715 source]# yum install mysql-community-server
# 启动、查看、开机自启动
[root@ecs-365715 source]# systemctl start mysqld // 启动
[root@ecs-365715 source]# systemctl status mysqld // 查看状态
● mysqld.service - MySQL Server
Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
Active: active (running) since Tue 2022-11-22 11:43:39 CST; 45s ago
Docs: man:mysqld(8)
http://dev.mysql.com/doc/refman/en/using-systemd.html
Process: 26867 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
Main PID: 26943 (mysqld)
Status: "Server is operational"
Tasks: 39 (limit: 23712)
Memory: 457.3M
CGroup: /system.slice/mysqld.service
└─26943 /usr/sbin/mysqld
Nov 22 11:43:32 ecs-365715 systemd[1]: Starting MySQL Server...
Nov 22 11:43:39 ecs-365715 systemd[1]: Started MySQL Server.
[root@ecs-365715 source]# systemctl enable mysqld // 开机自启
[root@ecs-365715 source]# systemctl daemon-reload // 重新加载系统配置,使开机自启立马有效
# 登录MySQL
注意:MySQL较大的变动就是,第一次安装会给root用户一个临时密码,我们需要拿到这个临时密码
[root@ecs-365715 source]# grep 'temporary password' /var/log/mysqld.log
2022-11-22T03:43:35.333150Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: 8g_yw%dewqJdfadd2kOVS
还有一种查看默认密码的方式:
[root@ecs-365715 source]# cat /var/log/mysqld.log
2022-11-22T03:43:32.847074Z 0 [System] [MY-013169] [Server] /usr/sbin/mysqld (mysqld 8.0.31) initializing of server in progress as process 26894
2022-11-22T03:43:32.856657Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
2022-11-22T03:43:33.671692Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
2022-11-22T03:43:35.333150Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: 8g_yw%dewqJdfadd2kOVS
2022-11-22T03:43:38.682930Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.31) starting as process 26943
2022-11-22T03:43:38.693295Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
2022-11-22T03:43:38.838271Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
2022-11-22T03:43:39.113525Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.
2022-11-22T03:43:39.113558Z 0 [System] [MY-013602] [Server] Channel mysql_main configured to support TLS. Encrypted connections are now supported for this channel.
2022-11-22T03:43:39.135984Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Bind-address: '::' port: 33060, socket: /var/run/mysqld/mysqlx.sock
2022-11-22T03:43:39.136031Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.0.31' socket: '/var/lib/mysql/mysql.sock' port: 3306 MySQL Community Server - GPL.
2022-11-22T03:45:26.417507Z 8 [Warning] [MY-010058] [Server] Hostname 'zg-1031f-83.stretchoid.com' does not resolve to ''.
[root@ecs-365715 source]# mysql -u root -p // 登录
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY '新密码'; // 修改密码
Query OK, 0 rows affected (0.01 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)
# 开启数据库远程连接
mysql> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> select user,host from user;
+------------------+-----------+
| user | host |
+------------------+-----------+
| mysql.infoschema | localhost |
| mysql.session | localhost |
| mysql.sys | localhost |
| root | localhost |
+------------------+-----------+
4 rows in set (0.00 sec)
mysql> update user set host='%' where user='root';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
# 查看MySQL配置文件
[root@ecs-365715 source]# whereis my.cnf
my: /etc/my.cnf
4、安装PHP
# 安装扩展
[root@hecs-223575 php-7.4.16]# yum search libxml2-devel // 查找是否有libxml2-devel
Last metadata expiration check: 2:44:16 ago on Mon 07 Nov 2022 03:03:23 AM CST.
No matches found.
如果有则直接执行下面语句
[root@hecs-223575 php-7.4.16]# yum install libxml2-devel sqlite-devel oniguruma
如果无则选择源码安装
[root@hecs-223575 source]# wget http://xmlsoft.org/sources/libxml2-2.9.12.tar.gz
[root@hecs-223575 source]# tar -xvf libxml2-2.9.12.tar.gz
[root@hecs-223575 source]# cd libxml2-2.9.12
[root@hecs-223575 libxml2-2.9.12]# ./configure --prefix=/usr/local/libxml2
[root@hecs-223575 libxml2-2.9.12]# make && make install
# 1、下载安装包并解压
[root@hecs-223575 source]# wget https://www.php.net/distributions/php-7.4.16.tar.gz
[root@hecs-223575 source]# tar -zxvf php-7.4.16.tar.gz
[root@hecs-223575 source]# cd php-7.4.16
[root@hecs-223575 php-7.4.16] export PKG_CONFIG_PATH="$PKG_CONFIG_PATH:/usr/local/libxml2/lib/pkgconfig"
[root@hecs-223575 php-7.4.16] ./configure --prefix=/usr/local/php --with-mysql-sock=/usr/local/mysql/mysql.sock --with-mysqli --with-zlib --with-curl --with-gd --with-jpeg-dir --with-png-dir --with-freetype-dir --with-openssl --enable-mbstring --enable-xml --enable-session --enable-ftp --enable-pdo --enable-tokenizer --enable-zip --enable-fpm --with-libxml-dir=/usr/local/libxml2 --enable-bcmath
### 错误1
Package 'libxml-2.0', required by 'virtual:world', not found
[root@hecs-223575 php-7.4.16] yum install libxml2-devel
### 错误2
Package 'oniguruma', required by 'virtual:world', not found)
[root@hecs-223575 source]yum install autoconf automake libtool -y
[root@hecs-223575 source]wget https://github.com/kkos/oniguruma/archive/v6.9.4.tar.gz -O oniguruma-6.9.4.tar.gz
[root@hecs-223575 source]tar xf oniguruma-6.9.4.tar.gz
[root@hecs-223575 source]cd oniguruma-6.9.4/
[root@hecs-223575 source]./autogen.sh && ./configure --prefix=/usr
[root@hecs-223575 source]make && make install
[root@hecs-223575 php-7.4.16] make && make install
[root@hecs-223575 php-7.4.16]# cp php.ini-production /usr/local/php/lib/php.ini
[root@hecs-223575 php-7.4.16]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php7-fpm
[root@hecs-223575 php-7.4.16]# chmod +x /etc/init.d/php7-fpm
[root@hecs-223575 php-7.4.16]# cd /usr/local/php/etc
[root@hecs-223575 etc]# cp php-fpm.conf.default php-fpm.conf
[root@hecs-223575 etc]# cp php-fpm.d/www.conf.default php-fpm.d/www.conf
[root@hecs-223575 etc]# echo 'export PATH="/usr/local/php/bin:$PATH"' >> /etc/profile #追加内容
[root@hecs-223575 etc]# source /etc/profile
安装swoole扩展
## 下载
##安装依赖
[root@hecs-223575 source] yum install -y glibc-headers gcc-c++
[root@hecs-223575 source] wget https://github.com/swoole/swoole-src/archive/v4.4.23.tar.gz
# 如果使用 github 下载慢,请到 pecl 进行下载
# tioncico@tioncico-PC:/tmp$ wget https://pecl.php.net/get/swoole-4.4.23.tgz
## 解压到当前目录
[root@hecs-223575 source] tar -zvxf v4.4.23.tar.gz
## cd 到解压之后的目录
[root@hecs-223575 source]cd swoole-src-4.4.23/
## 使用 phpize 创建 php 编译检测脚本 ./configure
##【注意:需要选择 php 对应版本的 phpize,这里使用的是绝对路径,否则编译安装无法生效】
[root@hecs-223575 source] /usr/local/php/bin/phpize
## 创建编译文件,第一个 --with,后面是 php-config 的所在路径(这个路径一般和 php 在同一个目录) /usr/local/php-7.2.2/bin/php-config,第二个 --enable,是开启 Swoole 的 ssl 功能,第三个 --enable(可选参数),是开启 Swoole 支持 http2 相关的功能
[root@hecs-223575 source] ./configure --with-php-config=/usr/local/php/bin/php-config --enable-openssl --enable-http2
## 编译 Swoole 并把编译好的文件移动到 php 的扩展目录(前面的配置 php 版本的扩展目录) 需要root权限
[root@hecs-223575 source] make && make install
## 编译成功会显示如下:
[root@hecs-223575 swoole-src-4.4.23]# make install
Installing shared extensions: /usr/local/php/lib/php/extensions/no-debug-non-zts-20190902/
Installing header files: /usr/local/php/include/php/
# 编辑php.ini文件,增加swoole扩展支持
[root@hecs-223575 source]# vim /usr/local/php/lib/php.ini
# 增加下面内容
; swoole扩展支持
extension=swoole.so
# 重启php-fpm
[root@hecs-223575 source]# service php-fpm restart
安装redis扩展
[root@hecs-223575 source]# wget https://pecl.php.net/get/redis-5.3.7.tgz
[root@hecs-223575 source]# tar -xvf redis-5.3.7.tgz
[root@hecs-223575 source]# cd redis-5.3.7
[root@hecs-223575 redis-5.3.7]# phpize
Configuring for:
PHP Api Version: 20190902
Zend Module Api No: 20190902
Zend Extension Api No: 320190902
[root@hecs-223575 redis-5.3.7]# ./configure
[root@hecs-223575 redis-5.3.7]# make && make install
# 编辑php.ini文件,增加redis扩展支持
[root@hecs-223575 source]# vim /usr/local/php/lib/php.ini
# 增加下面内容
; redis扩展支持
extension=redis.so
# 重启php-fpm
[root@hecs-223575 source]# service php-fpm restart
安装rabbitmq扩展
[root@hecs-223575 source]# yum install ncurses-devel unixODBC unixODBC-devel // 安装依赖
[root@hecs-223575 source]# wget http://erlang.org/download/otp_src_25.1.2 .tar.gz
[root@hecs-223575 source]# tar -zxvf otp_src_25.1.2 .tar.gz
[root@hecs-223575 source]# cd otp_src_25.1.2
[root@hecs-223575 otp_src_25.1.2 ]# ./configure --prefix=/usr/local/erlang
[root@hecs-223575 otp_src_25.1.2 ]# make && make install
# 配置erlang环境变量
[root@hecs-223575 otp_src_25.1.2 ]# echo 'export PATH="/usr/local/erlang/bin:$PATH"' >> /etc/profile
# 保存退出,并刷新变量
[root@hecs-223575 otp_src_25.1.2 ]# source /etc/profile
# 测试erlang是否安装成功
# 安装完成以后,执行erl看是否能打开eshell,用’halt().’退出,注意后面的点号,那是erlang的结束符。
[root@hecs-223575 source] erl
Erlang/OTP 25 [erts-13.1.2] [source] [64-bit] [smp:2:2] [ds:2:2:10] [async-threads:1] [jit:ns]
Eshell V13.1.2 (abort with ^G)
1>
[root@hecs-223575 source] curl -s https://packagecloud.io/install/repositories/rabbitmq/rabbitmq-server/script.rpm.sh | sudo bash
/etc/yum.repos.d/ 目录下添加一个 rabbitmq.repo 文件 内容如下
# In /etc/yum.repos.d/rabbitmq.repo
##
## Zero dependency Erlang
##
[rabbitmq_erlang]
name=rabbitmq_erlang
baseurl=https://packagecloud.io/rabbitmq/erlang/el/8/$basearch
repo_gpgcheck=1
gpgcheck=1
enabled=1
# PackageCloud's repository key and RabbitMQ package signing key
gpgkey=https://packagecloud.io/rabbitmq/erlang/gpgkey
https://github.com/rabbitmq/signing-keys/releases/download/2.0/rabbitmq-release-signing-key.asc
sslverify=1
sslcacert=/etc/pki/tls/certs/ca-bundle.crt
metadata_expire=300
[rabbitmq_erlang-source]
name=rabbitmq_erlang-source
baseurl=https://packagecloud.io/rabbitmq/erlang/el/8/SRPMS
repo_gpgcheck=1
gpgcheck=0
enabled=1
# PackageCloud's repository key and RabbitMQ package signing key
gpgkey=https://packagecloud.io/rabbitmq/erlang/gpgkey
https://github.com/rabbitmq/signing-keys/releases/download/2.0/rabbitmq-release-signing-key.asc
sslverify=1
sslcacert=/etc/pki/tls/certs/ca-bundle.crt
metadata_expire=300
##
## RabbitMQ server
##
[rabbitmq_server]
name=rabbitmq_server
baseurl=https://packagecloud.io/rabbitmq/rabbitmq-server/el/8/$basearch
repo_gpgcheck=1
gpgcheck=0
enabled=1
# PackageCloud's repository key and RabbitMQ package signing key
gpgkey=https://packagecloud.io/rabbitmq/rabbitmq-server/gpgkey
https://github.com/rabbitmq/signing-keys/releases/download/2.0/rabbitmq-release-signing-key.asc
sslverify=1
sslcacert=/etc/pki/tls/certs/ca-bundle.crt
metadata_expire=300
[rabbitmq_server-source]
name=rabbitmq_server-source
baseurl=https://packagecloud.io/rabbitmq/rabbitmq-server/el/8/SRPMS
repo_gpgcheck=1
gpgcheck=0
enabled=1
gpgkey=https://packagecloud.io/rabbitmq/rabbitmq-server/gpgkey
sslverify=1
sslcacert=/etc/pki/tls/certs/ca-bundle.crt
metadata_expire=300
更新软件包数据
[root@hecs-223575 source] yum update -y
[root@hecs-223575 source] yum -q makecache -y --disablerepo='*' --enablerepo='rabbitmq_erlang' --enablerepo='rabbitmq_server'
# 安装logrotate 依赖
[root@hecs-223575 source] yum install socat logrotate -y
[root@hecs-223575 source] yum install --repo rabbitmq_server rabbitmq-server -y
## 开启Rabbitmq-server 服务
[root@hecs-223575 source] systemctl start rabbitmq-server
## 查看MQ进程
[root@hecs-223575 source] ps -ef | grep rabbitmq
## 查看mq占用的端口
[root@hecs-223575 source] lsof -i | grep rabbit
# 或者
[root@hecs-223575 source] netstat -tunlp | grep rabbitmq
## 启动网页管理控制台插件
[root@hecs-223575 source] rabbitmq-plugins enable rabbitmq_management
默认账号 : guest
默认密码:guest
#第一步:添加 admin 用户并设置密码
rabbitmqctl add_user admin 123456
#第二步:添加 admin 用户为administrator角色
rabbitmqctl set_user_tags admin administrator
#第三步:设置 admin 用户的权限,指定允许访问的vhost以及write/read
rabbitmqctl set_permissions -p "/" admin ".*" ".*" ".*"
#第四步:查看vhost(/)允许哪些用户访问
rabbitmqctl list_permissions -p /
#第五步:查看用户列表
rabbitmqctl list_users
# 安装rabbitmq-c依赖包
[root@hecs-223575 source]# yum install libtool autoconf cmake
# 安装rabbitmq-c
[root@hecs-223575 source]# wget https://github.com/alanxz/rabbitmq-c/archive/v0.10.0.zip
[root@hecs-223575 source]# tar -zxvf v0.10.0
[root@hecs-223575 source]# cd rabbitmq-c-0.10.0/
[root@hecs-223575 rabbitmq-c-0.10.0]# cmake -DCMAKE_INSTALL_PREFIX=/usr/local/rabbitmq-c
[root@hecs-223575 rabbitmq-c-0.10.0]# cmake --build . --target install // 开始编译rabbitmq-c库
# 你可以看一下/usr/local/rabbitmq-c下的目录只有include和lib64。因为后面编译安装amqp扩展的时候系统会到/usr/local/rabbitmq-c/lib目录下搜索依赖库,导致错误。所以这里需要加一步
[root@hecs-223575 rabbitmq-c-0.10.0]ln -s /usr/local/rabbitmq-c/lib64 /usr/local/rabbitmq-c/lib
# 安装PHP扩展 amqp
[root@hecs-223575 source]# wget http://pecl.php.net/get/amqp-1.9.4.tgz
[root@hecs-223575 source]# tar zxvf amqp-1.9.4.tgz
[root@hecs-223575 source]# cd amqp-1.9.4
[root@hecs-223575 amqp-1.9.4]# /usr/local/php/bin/phpize
[root@hecs-223575 amqp-1.9.4]# ./configure --with-php-config=/usr/local/php/bin/php-config --with-amqp --with-librabbitmq-dir=/usr/local/rabbitmq-c
[root@hecs-223575 amqp-1.9.4]# make && make install
# 编辑php.ini文件,增加amqp扩展支持
# 增加下面内容
# rabbitmq扩展支持
# extension=amqp.so
[root@hecs-223575 source]# echo 'extension=amqp.so' >> /usr/local/php/lib/php.ini
# 重启php-fpm
[root@hecs-223575 source]# service php-fpm restart