1.简介
LNMP代表的就是:Linux系统下Nginx+MySQL+PHP这种网站服务器架构。
Linux是一类Unix计算机操作系统的统称,是目前最流行的免费操作系统。代表版本有:debian、centos、ubuntu、fedora、gentoo等。
Nginx是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP代理服务器。
Mysql是一个小型关系型数据库管理系统。
这四种软件均为免费开源软件,组合到一起,成为一个免费、高效、扩展性强的网站服务系统。
2.系统特点
Nginx是一个小巧而高效的Linux下的Web服务器软件,是由 Igor Sysoev 为俄罗斯访问量第二的 Rambler 站点开发的,已经在一些俄罗斯的大型网站上运行多年,相当的稳定。
Nginx性能稳定、功能丰富、运维简单、处理静态文件速度快且消耗系统资源极少。
3.优点
作为 Web 服务器:相比 Apache,Nginx 使用更少的资源,支持更多的并发连接,体现更高的效率。
作为负载均衡服务器:Nginx 既可以在内部直接支持Rails和PHP,也可以支持作为 HTTP代理服务器对外进行服务。Nginx 用C编写,不论是系统资源开销还是CPU使用效率都比Perlbal要好的多。
作为邮件代理服务器:Nginx同时也是一个非常优秀的邮件代理服务器(最早开发这个产品的目的之一也是作为邮件代理服务器),Last/fm 描述了成功并且美妙的使用经验。
Nginx 安装非常的简单,配置文件非常简洁(还能够支持perl语法)。Nginx支持平滑加载新的配置,还能够在不间断服务的情况下进行软件版本的升级。
Nginx官网
MySQL官网
php官网
4.部署lnmp 环境
系统平台 | IP | 部署服务 |
---|---|---|
centos8/redhat8 | 192.168.2.129 | lnmp: 2.mysql |
//配置 yum源
[root@localhost ~]# cd /etc/yum.repos.d/
[root@localhost yum.repos.d]# rm -rf *
[root@localhost yum.repos.d]# curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo
[root@localhost yum.repos.d]# sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo
//配置epel源
[root@localhost yum.repos.d]# yum install -y https://mirrors.aliyun.com/epel/epel-release-latest-8.noarch.rpm
[root@localhost yum.repos.d]# sed -i 's|^#baseurl=https://download.example/pub|baseurl=https://mirrors.aliyun.com|' /etc/yum.repos.d/epel*
[root@localhost yum.repos.d]# sed -i 's|^metalink|#metalink|' /etc/yum.repos.d/epel*
//清理缓存并重建缓存
[root@localhost yum.repos.d]# dnf clean all
[root@localhost yum.repos.d]# dnf makecache
//关闭防火墙跟SElinux
[root@localhost ~]# sed -i '/SELINUX=enforcing/c SELINUX=disabled' /etc/selinux/config
[root@localhost ~]# grep '^SELINUX=' /etc/selinux/config
SELINUX=disabled
[root@localhost ~]# systemctl disable --now firewalld
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
//重启
[root@localhost ~]# reboot
部署Nginx
//下载 nginx
[root@localhost ~]# cd /usr/src/
[root@localhost src]# wget https://nginx.org/download/nginx-1.22.0.tar.gz
//下载 依赖包
[root@localhost ~]# dnf -y install boost-devel --allowerasing pcre-devel openssl openssl-devel gd-devel gcc gcc-c++ make
[root@localhost ~]# yum -y groups mark install 'Development Tools'
//创建系统用户
[root@localhost ~]# useradd -r -M -s /sbin/nologin nginx
[root@localhost ~]# id nginx
uid=995(nginx) gid=992(nginx) groups=992(nginx)
//创建日志存放目录
[root@localhost ~]# mkdir -p /var/log/nginx
[root@localhost ~]# chown -R nginx.nginx /var/log/nginx/
[root@localhost ~]# ll -d /var/log/nginx/
drwxr-xr-x 2 nginx nginx 6 Aug 31 10:21 /var/log/nginx/
//编译安装
[root@localhost ~]# cd /usr/src/
[root@localhost src]# ls
debug kernels nginx-1.22.0.tar.gz
[root@localhost src]# tar xf nginx-1.22.0.tar.gz
[root@localhost src]# cd nginx-1.22.0
[root@localhost nginx-1.22.0]# ./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-debug \
--with-http_ssl_module \
--with-http_realip_module \
--with-http_image_filter_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_stub_status_module \
--http-log-path=/var/log/nginx/access.log \
--error-log-path=/var/log/nginx/error.log
[root@localhost nginx-1.22.0]# nproc //查看核心数
2
[root@localhost nginx-1.22.0]# make -j 2 && make install
//设置环境变量
[root@localhost ~]# echo 'export PATH=/usr/local/nginx/sbin:$PATH' > /etc/profile.d/nginx.sh
[root@localhost ~]# source /etc/profile.d/nginx.sh
[root@localhost ~]# which nginx
/usr/local/nginx/sbin/nginx
//启动 nginx
[root@localhost ~]# nginx
[root@localhost ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:80 0.0.0.0:*
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 [::]:22 [::]:*
# 服务控制方式,使用nginx命令
-t //检查配置文件语法
-v //输出nginx的版本
-c //指定配置文件的路径
-s //发送服务控制信号,可选值有{stop|quit|reopen|reload}
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
#还没有开启 反向代理之前
//开启反向代理
[root@localhost ~]# sed -i '/pass the PHP/{N;/#/{n;s/#//g}}' /usr/local/nginx/conf/nginx.conf
[root@localhost ~]# sed -i '/^ location ~ \\.php/{N;s/#//g}' /usr/local/nginx/conf/nginx.conf
[root@localhost ~]# sed -i '/fastcgi_pass/{s/#//g}' /usr/local/nginx/conf/nginx.conf
[root@localhost ~]# sed -i '/fastcgi_index/{s/#//g}' /usr/local/nginx/conf/nginx.conf
[root@localhost ~]# sed -i '/SCRIPT_FILENAME/c \ fastcgi_param SCRIPT_FILENAME /$document_root$fastcgi_script_name;' /usr/local/nginx/conf/nginx.conf
[root@localhost ~]# sed -i '/fastcgi_params/{s/#//g}' /usr/local/nginx/conf/nginx.conf
[root@localhost ~]# sed -i '/fastcgi_params/{n;s/#//g}' /usr/local/nginx/conf/nginx.conf
[root@localhost ~]# sed -i '/^ index/c \ index index.php index.html index.htm;' /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 /$document_root$fastcgi_script_name;
include fastcgi_params;
}
//配置 PHP 网页
[root@localhost ~]# cd /usr/local/nginx/html/
[root@localhost html]# cat index.php
<?php
phpinfo();
?>
//停掉之后立马启动
[root@localhost ~]# nginx -s stop;nginx
[root@localhost ~]# nginx -s reload // 推荐使用
//设置开机自启
[root@localhost ~]# cd /usr/lib/systemd/system
[root@localhost system]# cp sshd.service nginxd.service
[root@localhost system]# vim nginxd.service
[Unit]
Description=nginx server daemon
After=network.target sshd-keygen.target
[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecStop=/usr/local/nginx/sbin/nginx -s stop
ExecReload=/bin/kill -HUP $MAINPID
[Install]
WantedBy=multi-user.target
[root@localhost system]# systemctl daemon-reload
[root@localhost ~]# systemctl enable --now nginxd
访问 nginx
部署MySQL
//安装依赖包
[root@localhost ~]# dnf -y install ncurses-compat-libs openssl-devel openssl cmake mariadb-devel
//创建mysql系统用户
[root@localhost ~]# useradd -r -M -s /sbin/nologin mysql
[root@localhost ~]# id mysql
uid=994(mysql) gid=991(mysql) groups=991(mysql)
//下载二进制格式的mysql软件包
[root@localhost ~]# cd /usr/src/
[root@localhost src]# wget https://downloads.mysql.com/archives/get/p/23/file/mysql-5.7.37-linux-glibc2.12-x86_64.tar.gz
[root@localhost src]# ls
debug kernels mysql-5.7.37-linux-glibc2.12-x86_64.tar.gz nginx-1.22.0 nginx-1.22.0.tar.gz
//解压软件至/usr/local/
[root@localhost src]# tar xf mysql-5.7.37-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
[root@localhost src]# cd /usr/local/
[root@localhost local]# mv mysql-5.7.37-linux-glibc2.12-x86_64 mysql //重命名为 mysql
[root@localhost local]# ll -d mysql/
drwxr-xr-x 9 root root 129 Aug 31 12:49 mysql/
//修改目录/usr/local/mysql的属主属组
[root@localhost local]# chown -R mysql.mysql mysql/
[root@localhost local]# ll -d mysql/
drwxr-xr-x 9 mysql mysql 129 Aug 31 12:49 mysql/
//添加环境变量
[root@localhost ~]# echo 'export PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
[root@localhost ~]# . /etc/profile.d/mysql.sh
[root@localhost ~]# which mysqld
/usr/local/mysql/bin/mysqld
//配置include
//软链接到/usr/include/mysql,叫mysql
[root@localhost ~]# ln -s /usr/local/mysql/include /usr/include/mysql
//配置 man 文档
[root@localhost ~]# sed -i '/MANDATORY_MANPATH.*.\/local\/share\/man/a MANDATORY_MANPATH\t\t\t/usr/local/mysql/man' /etc/man_db.conf
# \t 表示一个tab
//映射库文件
[root@localhost ~]# echo '/usr/local/mysql/lib' > /etc/ld.so.conf.d/mysql.conf
[root@localhost ~]# ldconfig //让其生效
//建立数据存放目录
[root@localhost ~]# mkdir -p /opt/data
[root@localhost ~]# chown -R mysql.mysql /opt/data/
[root@localhost ~]# ll /opt/data/ -d
drwxr-xr-x 2 mysql mysql 6 Aug 31 12:59 /opt/data/
//初始化数据库
[root@localhost ~]# /usr/local/mysql/bin/mysqld --initialize --user=mysql --datadir=/opt/data/
......省略
root@localhost: jshsdWSjdh:-J
# 请注意,这个命令的最后会生成一个临时密码,此处密码是 jshsdWSjdh:-J
# 这个密码是随机的,一定要记住这个密码,因为一会登录时会用到
//生成配置文件
[root@localhost ~]# cat /etc/my.cnf
[mysqld]
basedir = /usr/local/mysql
datadir = /opt/data
socket = /tmp/mysql.sock
port = 3306
pid-file = /opt/data/mysql.pid
user = mysql
skip-name-resolve
//配置服务启动脚本
[root@localhost ~]# cd /usr/local/mysql/support-files/
[root@localhost support-files]# cp mysql.server /etc/init.d/mysqld
[root@localhost support-files]# ll /etc/init.d/mysqld
-rwxr-xr-x 1 root root 10576 Aug 31 13:04 /etc/init.d/mysqld
//设置所有者所属组为mysql
[root@localhost support-files]# chown -R mysql.mysql /etc/init.d/mysqld
[root@localhost support-files]# ll /etc/init.d/mysqld
-rwxr-xr-x 1 mysql mysql 10576 Aug 31 13:04 /etc/init.d/mysqld
[root@localhost ~]# grep '^basedir=' /etc/init.d/mysqld
basedir= // 需要添加MySQL 路径
[root@localhost ~]# grep '^datadir=' /etc/init.d/mysqld
datadir= // 数据存放路径
[root@localhost ~]# sed -i '/^basedir=/c basedir=/usr/local/mysql' /etc/init.d/mysqld
[root@localhost ~]# sed -i '/^datadir=/c datadir=/opt/data' /etc/init.d/mysqld
[root@localhost ~]# grep '^basedir=' /etc/init.d/mysqld
basedir=/usr/local/mysql
[root@localhost ~]# grep '^datadir=' /etc/init.d/mysqld
datadir=/opt/data
//启动mysql 并开机自启
[root@localhost ~]# chkconfig --add mysqld
[root@localhost ~]# chkconfig mysqld on
[root@localhost ~]# service mysqld start
[root@localhost ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:80 0.0.0.0:*
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 80 *:3306 *:*
LISTEN 0 128 [::]:22 [::]:*
[root@localhost ~]#
//查看mysql 进程
[root@localhost ~]# ps -ef | grep mysqld
root 33663 33589 0 13:06 pts/0 00:00:00 vim /etc/init.d/mysqld
root 33695 1 0 13:09 pts/2 00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/opt/data --pid-file=/opt/data/mysql.pid
mysql 33883 33695 0 13:09 pts/2 00:00:00 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/opt/data --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=localhost.localdomain.err --pid-file=/opt/data/mysql.pid --socket=/tmp/mysql.sock --port=3306
root 33914 10200 0 13:09 pts/2 00:00:00 grep --color=auto mysqld
[root@localhost ~]#
//修改密码
//使用临时密码登录
[root@localhost ~]# mysql -uroot -p'jshsdWSjdh:-J'
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.37
Copyright (c) 2000, 2022, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> set password = password('runtime123!'); # 设置新密码
mysql> quit
Bye
//退出登录验证密码
[root@localhost ~]# mysql -uroot -pruntime123!
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.37 MySQL Community Server (GPL)
Copyright (c) 2000, 2022, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
部署PHP
//下载 PHP
[root@localhost ~]# cd /usr/src/
[root@localhost src]# wget https://www.php.net/distributions/php-7.4.29.tar.xz
........省略
[root@localhost src]# ls
debug mysql-5.7.37-linux-glibc2.12-x86_64.tar.gz nginx-1.22.0.tar.gz
kernels nginx-1.22.0 php-7.4.29.tar.xz
[root@localhost src]# tar xf php-7.4.29.tar.xz //解压
//这是查找MySQL跟PHP打交道的包名
[root@localhost ~]# dnf list all | grep mysql | grep php
php-mysqlnd.x86_64 7.2.24-1.module_el8.2.0+313+b04d0a66 AppStream
//安装依赖包
[root@localhost ~]# dnf -y install libxml2-devel openssl openssl-devel bzip2 bzip2-devel libcurl libcurl-devel libicu-devel libjpeg libjpeg-devel libpng libpng-devel openldap-devel pcre-devel freetype freetype-devel gmp gmp-devel libmcrypt libmcrypt-devel readline readline-devel libxslt libxslt-devel mhash mhash-devel sqlite-devel libzip-devel php-mysqlnd
安装过程略....
//安装 oniguruma 包
[root@localhost ~]# dnf -y install http://mirror.centos.org/centos/8-stream/PowerTools/x86_64/os/Packages/oniguruma-devel-6.8.2-2.el8.x86_64.rpm
//编译安装php
[root@localhost ~]# cd /usr/src/php-7.4.29
[root@localhost php-7.4.29]# ./configure --prefix=/usr/local/php7 \
--with-config-file-path=/etc \
--enable-fpm \
--enable-inline-optimization \
--disable-debug \
--disable-rpath \
--enable-shared \
--enable-soap \
--with-openssl \
--enable-bcmath \
--with-iconv \
--with-bz2 \
--enable-calendar \
--with-curl \
--enable-exif \
--enable-ftp \
--enable-gd \
--with-jpeg \
--with-zlib-dir \
--with-freetype \
--with-gettext \
--enable-json \
--enable-mbstring \
--enable-pdo \
--with-mysqli=mysqlnd \
--with-pdo-mysql=mysqlnd \
--with-readline \
--enable-shmop \
--enable-simplexml \
--enable-sockets \
--with-zip \
--enable-mysqlnd-compression-support \
--with-pear \
--enable-pcntl \
--enable-posix
//编译安装
[root@localhost php-7.4.29]# make
[root@localhost php-7.4.29]# make install
//安装后配置
[root@localhost ~]# echo 'export PATH=/usr/local/php7/bin:$PATH' > /etc/profile.d/php7.sh
[root@localhost ~]# source /etc/profile.d/php7.sh
[root@localhost ~]# which php
/usr/local/php7/bin/php
//配置头文件
[root@localhost ~]# ln -s /usr/local/php7/include /usr/include/php7
//配置库文件
[root@localhost ~]# echo '/usr/local/php7/lib' > /etc/ld.so.conf.d/php7.conf
[root@localhost ~]# ldconfig
//查看版本
[root@localhost ~]# php -v
PHP 7.4.29 (cli) (built: Jul 4 2022 21:07:32) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
//配置php-fpm
[root@localhost ~]# cd /usr/src/php-7.4.29
[root@localhost php-7.4.29]# \cp php.ini-production /etc/php.ini # 这个文件已存在所以要加上\表示覆盖
[root@localhost php-7.4.29]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
[root@localhost php-7.4.29]# ll -d /etc/init.d/php-fpm
-rw-r--r-- 1 root root 2402 Aug 31 13:41 /etc/init.d/php-fpm
[root@localhost php-7.4.29]# chmod +x /etc/init.d/php-fpm
[root@localhost php-7.4.29]# ll -d /etc/init.d/php-fpm
-rwxr-xr-x 1 root root 2402 Aug 31 13:41 /etc/init.d/php-fpm
[root@localhost ~]# cd /usr/local/php7/etc
[root@localhost etc]# cp php-fpm.conf.default php-fpm.conf
[root@localhost etc]# cd php-fpm.d
[root@localhost php-fpm.d]# cp www.conf.default www.conf
[root@localhost ~]# grep '^user =' /usr/local/php7/etc/php-fpm.d/www.conf
user = nobody
[root@localhost ~]# grep '^group =' /usr/local/php7/etc/php-fpm.d/www.conf
group = nobody
[root@localhost ~]# sed -i '/^user = nobody/c user = nginx' /usr/local/php7/etc/php-fpm.d/www.conf
[root@localhost ~]# sed -i '/^group = nobody/c group = nginx' /usr/local/php7/etc/php-fpm.d/www.conf
[root@localhost ~]# grep '^user =' /usr/local/php7/etc/php-fpm.d/www.conf
user = nginx
[root@localhost ~]# grep '^group =' /usr/local/php7/etc/php-fpm.d/www.conf
group = nginx
//编辑php-fpm的配置文件(/usr/local/php7/etc/php-fpm.conf):
//配置fpm的相关选项为你所需要的值:
[root@localhost ~]# vim /usr/local/php7/etc/php-fpm.conf
.....
.....
pm.max_children = 50 ;最多同时提供50个进程提供50个并发服务
pm.start_servers = 5 ;启动时启动5个进程
pm.min_spare_servers = 2 ;最小空闲进程数
pm.max_spare_servers = 8 ;最大空闲进程数
[root@localhost ~]# tail /usr/local/php7/etc/php-fpm.conf
; file.
; Relative path can also be used. They will be prefixed by:
; - the global prefix if it's been set (-p argument)
; - /usr/local/php7 otherwise
include=/usr/local/php7/etc/php-fpm.d/*.conf
pm.max_children = 50
pm.start_servers = 5
pm.min_spare_servers = 2
pm.max_spare_servers = 8
//查看端口号是否监听在9000上
[root@localhost php-fpm.d]# pwd
/usr/local/php7/etc/php-fpm.d
[root@localhost php-fpm.d]# vim www.conf
listen = 127.0.0.1:9000 //查看端口号是否监听在9000上
//启动php-fpm 、 并开机自启
[root@localhost ~]# service php-fpm start
[root@localhost ~]# chkconfig --add php-fpm
[root@localhost ~]# chkconfig php-fpm on
//默认情况下,fpm监听在127.0.0.1的9000端口,也可以使用如下命令验证其是否已经监听在相应的套接字
[root@localhost ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 127.0.0.1:9000 0.0.0.0:*
LISTEN 0 128 0.0.0.0:80 0.0.0.0:*
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 80 *:3306 *:*
LISTEN 0 128 [::]:22 [::]:*
[root@localhost ~]#
//查看 PHP 进程
[root@localhost ~]# ps -ef | grep php
root 164486 1 0 13:43 ? 00:00:00 php-fpm: master process (/usr/localphp7/etc/php-fpm.conf)
nobody 164487 164486 0 13:43 ? 00:00:00 php-fpm: pool www
nobody 164488 164486 0 13:43 ? 00:00:00 php-fpm: pool www
root 164499 33589 0 13:44 pts/0 00:00:00 grep --color=auto php
//最后可以重启是否可以开机自启
[root@localhost ~]# reboot
[root@localhost ~]# ss -antl
State Recv-Q Send-Q Local Address:Port
LISTEN 0 128 0.0.0.0:22
LISTEN 0 128 127.0.0.1:9000
LISTEN 0 128 0.0.0.0:80
LISTEN 0 128 [::]:22
LISTEN 0 80 *:3306
ip访问