LNMP部署


环境说明

用户IP服务
Nginx192.168.40.99nginx
Mysql192.168.40.100mysql
Php192.168.40.101php

关闭三台防火墙和selinux

# systemctl status firewalld.service 
● firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
   Active: inactive (dead)
     Docs: man:firewalld(1)
[root@nginx ~]# getenforce 
Disabled

配置yum源配置yum源

cd /etc/yum.repos.d/
wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo
yum install -y https://mirrors.aliyun.com/epel/epel-release-latest-8.noarch.rpm

源码部署nginx

创建nginx服务用户

[root@nginx ~]# useradd -r -M -s /sbin/nologin nginx
[root@nginx ~]# id nginx 
uid=975(nginx) gid=974(nginx) 组=974(nginx)

安装依赖环境

[root@nginx ~]# dnf -y install pcre-devel openssl openssl-devel gd-devel gcc gcc-c++ make
[root@nginx ~]# dnf -y groups mark install 'Development Tools'
上次元数据过期检查:0:13:17 前,执行于 2022年10月09日 星期日 23时39分57秒。
依赖关系解决。
=============================================================================================================
 软件包                   架构                    版本                        仓库                      大小
=============================================================================================================
安装组:
 Development Tools                                                                                          

事务概要
=============================================================================================================

完毕!

创建日志存放目录

[root@nginx ~]# cd /var/log/
[root@nginx log]# mkdir nginx
[root@nginx log]# chown -R nginx.nginx /var/log/nginx/
[root@nginx log]# ll /var/log/ | grep nginx
drwxr-xr-x  2 nginx  nginx        6 10月  9 23:54 nginx

安装nginx

[root@nginx ~]# cd /usr/local/
[root@nginx local]# wget http://nginx.org/download/nginx-1.22.0.tar.gz
--2022-10-09 23:59:20--  http://nginx.org/download/nginx-1.22.0.tar.gz
正在解析主机 nginx.org (nginx.org)... 3.125.197.172, 52.58.199.22, 2a05:d014:edb:5702::6, ...
正在连接 nginx.org (nginx.org)|3.125.197.172|:80... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:1073322 (1.0M) [application/octet-stream]
正在保存至: “nginx-1.22.0.tar.gz”

nginx-1.22.0.tar.gz         100%[========================================>]   1.02M   401KB/s  用时 2.6s    

2022-10-09 23:59:24 (401 KB/s) - 已保存 “nginx-1.22.0.tar.gz” [1073322/1073322])
[root@nginx local]# tar xf nginx-1.22.0.tar.gz -C /usr/local/src/
[root@nginx local]# cd /usr/local/src/
[root@nginx src]# ls
nginx-1.22.0
[root@nginx src]# cd nginx-1.22.0/
[root@nginx 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@nginx nginx-1.22.0]# make -j $(grep 'processor' /proc/cpuinfo | wc -l) && make install

配置环境变量

[root@nginx ~]# echo 'export PATH=/usr/local/nginx/sbin:$PATH' > /etc/profile
[root@nginx ~]# . /etc/profile

服务控制方式,使用nginx命令
    -t  //检查配置文件语法
    -v  //输出nginx的版本
    -c  //指定配置文件的路径
    -s  //发送服务控制信号,可选值有{stop|quit|reopen|reload

启动服务

[root@nginx ~]# nginx 
[root@nginx ~]# ss -antlp
State         Recv-Q        Send-Q               Local Address:Port                 Peer Address:Port        Process                                                                                                      
LISTEN        0             128                        0.0.0.0:111                       0.0.0.0:*            users:(("rpcbind",pid=904,fd=4),("systemd",pid=1,fd=35))                                                    
LISTEN        0             128                        0.0.0.0:80                        0.0.0.0:*            users:(("nginx",pid=25434,fd=9),("nginx",pid=25433,fd=9))                                                   
LISTEN        0             32                   192.168.122.1:53                        0.0.0.0:*            users:(("dnsmasq",pid=1790,fd=6))                                                                           
LISTEN        0             128                        0.0.0.0:22                        0.0.0.0:*            users:(("sshd",pid=983,fd=4))                                                                               
LISTEN        0             5                        127.0.0.1:631                       0.0.0.0:*            users:(("cupsd",pid=1132,fd=10))                                                                            
LISTEN        0             128                           [::]:111                          [::]:*            users:(("rpcbind",pid=904,fd=6),("systemd",pid=1,fd=37))                                                    
LISTEN        0             128                           [::]:22                           [::]:*            users:(("sshd",pid=983,fd=6))                                                                               
LISTEN        0             5                            [::1]:631                          [::]:*            users:(("cupsd",pid=1132,fd=9))                                                                             

部署Mysql

创建mysql服务用户

[root@mysql ~]# useradd -r -M -s /sbin/nologin mysql
[root@mysql ~]# id mysql 
uid=975(mysql) gid=974(mysql) 组=974(mysql)

安装依赖包

[root@mysql ~]# dnf -y install ncurses-devel openssl-devel openssl cmake mariadb-devel

安装MySQL

[root@mysql ~]# rz -E
rz waiting to receive.
[root@mysql ~]# ls
公共  视频  文档  音乐  anaconda-ks.cfg       mysql-5.7.37-linux-glibc2.12-x86_64.tar.gz
模板  图片  下载  桌面  initial-setup-ks.cfg
[root@mysql ~]# tar xf mysql-5.7.37-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
[root@mysql ~]# cd /usr/local/
[root@mysql local]# ls
bin  etc  games  include  lib  lib64  libexec  mysql-5.7.37-linux-glibc2.12-x86_64  sbin  share  src
[root@mysql local]# ln -s mysql-5.7.37-linux-glibc2.12-x86_64/ mysql
[root@mysql local]# ls
bin  games    lib    libexec  mysql-5.7.37-linux-glibc2.12-x86_64  share
etc  include  lib64  mysql    sbin                                 src
[root@mysql local]# chown mysql.mysql mysql*

创建数据库存放目录,配置环境变量

[root@mysql local]# mkdir /opt/data
[root@mysql local]# chown mysql.mysql /opt/data/
[root@mysql local]#  echo 'export PATH=/usr/local/mysql/bin:$PATH' > /etc/profile
[root@mysql local]# . /etc/profile

初始化安装

[root@mysql local]# mysqld --initialize --user=mysql --datadir=/opt/data/
2022-10-11T03:57:29.626560Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2022-10-11T03:57:29.818087Z 0 [Warning] InnoDB: New log files created, LSN=45790
2022-10-11T03:57:29.853255Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2022-10-11T03:57:29.915578Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: d3c2ce07-4918-11ed-841d-000c296fdbfa.
2022-10-11T03:57:29.917227Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2022-10-11T03:57:30.574865Z 0 [Warning] A deprecated TLS version TLSv1 is enabled. Please use TLSv1.2 or higher.
2022-10-11T03:57:30.574906Z 0 [Warning] A deprecated TLS version TLSv1.1 is enabled. Please use TLSv1.2 or higher.
2022-10-11T03:57:30.575451Z 0 [Warning] CA certificate ca.pem is self signed.
2022-10-11T03:57:30.899485Z 1 [Note] A temporary password is generated for root@localhost: :fw9D70.;mqg
[root@mysql local]# echo "fw9D70.;mqg" > pass
[root@mysql local]# cat pass 
fw9D70.;mqg

配置mysql主配置文件

[root@mysql local]# vim /etc/my.cnf
[root@mysql local]# vim /etc/my.cnf
[root@mysql local]# cat /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
[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@mysql local]# vim /usr/local/mysql/support-files/mysql.server
basedir=/usr/local/mysql
datadir=/opt/data
[root@mysql local]# vim /usr/lib/systemd/system/mysql.service
[Unit]
Description=mysql server daemon
After=network.targe

[Service]
Type=forking
ExecStart=/usr/local/mysql/support-files/mysql.server start
ExecStop=/usr/local/mysql/support-files/mysql.serve stop
ExecReload=/bin/kill -HUP \$MAINPID

[Install]
WantedBy=multi-user.target
[root@mysql local]# systemctl daemon-reload 
[root@mysql local]# systemctl start mysql.service 
[root@mysql local]# systemctl enable mysql.service 
Created symlink /etc/systemd/system/multi-user.target.wants/mysql.service → /usr/lib/systemd/system/mysql.service.
[root@mysql local]# ss -antl
State      Recv-Q      Send-Q           Local Address:Port           Peer Address:Port     Process     
LISTEN     0           128                    0.0.0.0:111                 0.0.0.0:*                    
LISTEN     0           32               192.168.122.1:53                  0.0.0.0:*                    
LISTEN     0           128                    0.0.0.0:22                  0.0.0.0:*                    
LISTEN     0           5                    127.0.0.1:631                 0.0.0.0:*                    
LISTEN     0           80                           *:3306                      *:*                    
LISTEN     0           128                       [::]:111                    [::]:*                    
LISTEN     0           128                       [::]:22                     [::]:*                    
LISTEN     0           5                        [::1]:631                    [::]:*                    
[root@mysql local]# mysql -uroot -p"fw9D70.;mqg"
mysql: error while loading shared libraries: libncurses.so.5: cannot open shared object file: No such file or directory

需要安装依赖包
[root@mysql local]# dnf whatprovides libncurses.so.5
上次元数据过期检查:0:04:22 前,执行于 2022年10月11日 星期二 00时10分09秒。
ncurses-compat-libs-6.1-9.20180224.el8.i686 : Ncurses compatibility libraries
仓库        :base
匹配来源:
提供    : libncurses.so.5

[root@mysql local]# dnf -y install ncurses-compat-libs
登录数据库,重新设置密码
[root@mysql ~]# mysql -uroot -pfw9D70.;mqg
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

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('123456');
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> 

安装后配置

[root@mysql ~]# vim /etc/man_db.conf
MANDATORY_MANPATH                       /usr/local/mysql/man
[root@mysql ~]# vim /etc/man_db.conf 
[root@mysql ~]# cd /etc/ld.so.conf.d/
[root@mysql ld.so.conf.d]# ls
kernel-4.18.0-358.el8.x86_64.conf  libiscsi-x86_64.conf
[root@mysql ld.so.conf.d]# echo "/usr/local/mysql/lib" > mysql.conf
[root@mysql ld.so.conf.d]# ls
kernel-4.18.0-358.el8.x86_64.conf  libiscsi-x86_64.conf  mysql.conf

源码安装php

安装依赖包和工具

[root@php local]# dnf -y install make libxml2 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 php-mysqlnd --nobest
[root@php local]# 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@php ~]# ls
公共  视频  文档  音乐  anaconda-ks.cfg       php-7.1.10.tar.gz
模板  图片  下载  桌面  initial-setup-ks.cfg
[root@php ~]# tar xf php-7.1.10.tar.gz -C /usr/local/
[root@php ~]# cd /usr/local/
[root@php local]# ls
bin  etc  games  include  lib  lib64  libexec  php-7.1.10  sbin  share  src
[root@php local]# cd php-7.1.10/
[root@php php-7.1.10]#  ./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@php php-7.1.10]# make -j4 && make install

安装后配置

[root@php local]# echo 'export PATH=/usr/local/php7/bin:$PATH' > /etc/profile.d/php7.sh
[root@php local]# ls
bin  etc  games  include  lib  lib64  libexec  php7  php-7.1.10  sbin  share  src
[root@php local]# cd php7
[root@php php7]# ln -s /usr/local/php7/include/ /usr/include/php7
[root@php php7]# echo '/usr/local/php7/lib/' > /etc/ld.so.conf.d/php7.conf
[root@php php7]# ldconfig
[root@php php7]# source /etc/profile.d/php7.sh

配置php-fpm

[root@php local]# cd php-7.1.10/
[root@php php-7.1.10]# cp php.ini-production /etc/php.ini
cp:是否覆盖'/etc/php.ini'? y
[root@php php-7.1.10]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
[root@php php-7.1.10]# chmod +x /etc/rc.d/init.d/php-fpm
[root@php php-7.1.10]# cp /usr/local/php7/etc/php-fpm.conf.default /usr/local/php7/etc/php-fpm.conf
[root@php php-7.1.10]# cp /usr/local/php7/etc/php-fpm.d/www.conf.default /usr/local/php7/etc/php-fpm.d/www.conf

启动php-fpm

[root@php php-7.1.10]# cd /usr/lib/systemd/system
[root@php system]# vim php-fpm.service

[Unit]
Description=php-fpm server daemon
After=network.target sshd-keygen.target

[Service]
Type=forking
ExecStart=/etc/init.d/php-fpm start
ExecStop=/etc/init.d/php-fpm  stop
ExecReload=/bin/kill -HUP $MAINPID

[Install]
WantedBy=multi-user.target
[root@php system]# systemctl daemon-reload 
[root@php system]# systemctl restart php-fpm.service 
[root@php system]# systemctl enable php-fpm.service 
Synchronizing state of php-fpm.service with SysV service script with /usr/lib/systemd/systemd-sysv-install.
Executing: /usr/lib/systemd/systemd-sysv-install enable php-fpm
Created symlink /etc/systemd/system/multi-user.target.wants/php-fpm.service → /usr/lib/systemd/system/php-fpm.service.

配置php

[root@php system]# cd /usr/local/php7/etc/
[root@php etc]# ls
pear.conf  php-fpm.conf  php-fpm.conf.default  php-fpm.d
[root@php etc]# cd php-fpm.d/
[root@php php-fpm.d]# ls
www.conf  www.conf.default
[root@php php-fpm.d]# vim www.conf
listen = 0.0.0.0:9000
listen.allowed_clients = 192.168.100.10

生成测试页面

[root@php php-fpm.d]# cd /var/www/html/
[root@php html]# ls
index.html
[root@php html]# vim index.php
<?php
  phpinfo();
?>
[root@php html]# chown -R apache.apache /var/www/html/
[root@php html]# systemctl restart php-fpm.service 
[root@php html]# systemctl enable php-fpm.service

在nginx节点操作,整合nginx和php

[root@nginx ~]# vim /usr/local/nginx/conf/nginx.conf
location / {
            root   html;
            index  index.php index.html index.htm;
        }
        location ~ \.php$ {
            root       /var/www/html;
            fastcgi_pass   192.168.40.101:9000;  
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /var/www/html/$fastcgi_script_name;
            include        fastcgi_params;
        }

测试nginx配置文件

[root@nginx ~]# 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
[root@nginx ~]# nginx -s stop
[root@nginx ~]# nginx   

测试
在这里插入图片描述

<think>好的,我现在需要帮助用户了解如何部署LNMP栈。用户明确要求一个LNMP部署教程,我需要根据提供的引用内容来构建一个结构清晰的回答。首先,我要回顾用户提供的引用,看看有哪些步骤和注意事项需要包含进去。 引用[1]提到了Linux系统的安装,但用户可能已经安装了系统,所以这部分可能不是重点,但可能需要简要提及环境要求。引用[2]详细描述了Nginx的安装步骤,包括关闭防火墙、安装依赖包、创建用户、编译安装和优化路径。引用[4]补充了更多可能需要安装的依赖项,比如gd、libjpeg等。而引用[3]则涉及Docker部署LNMP的方法,这可能是一个备选方案,但用户可能更关心传统的手动部署步骤。 接下来,我需要将LNMP分解为各个组件:LinuxNginx、MySQL、PHP。每个组件的安装步骤需要详细说明,并确保符合用户提供的引用中的步骤。例如,Nginx部分需要按照引用[2]的步骤,包括关闭防火墙、安装依赖、创建用户、编译安装和路径优化。同时,引用[4]中的额外依赖可能需要在安装PHP时考虑,特别是PHP的扩展部分。 另外,需要注意用户提到的系统级指令,比如行内数学表达式使用$...$,但在这个问题中可能用不到数学公式,所以可能不需要处理这点。但是必须确保回答结构清晰,分步骤说明,并引用正确的引用来源。 可能还需要考虑是否需要优化步骤,比如配置文件的修改、服务的启动和开机自启,以及如何测试每个组件是否正常工作。例如,安装完Nginx后,可能需要启动服务并访问测试页面;安装MySQL后需要安全设置;PHP需要测试解析是否正确,可能需要创建一个info.php文件。 另外,用户提供的引用中有提到Docker部署的方法,虽然用户主要询问的是传统部署,但可能可以作为扩展内容简要提及,供用户参考。 需要检查是否有遗漏的步骤,比如MySQL的安装和PHP的依赖安装是否覆盖了所有必要的包。确保每一步都正确引用相关的引用来源,例如安装Nginx的步骤来自引用[2]和[4]。 最后,生成相关问题,可能需要包括优化配置、常见问题解决、安全加固、Docker部署等,这些问题应该基于教程中提到的步骤,引导用户进一步学习。 现在,将这些思考整理成一个结构化的教程,分步骤详细说明,并确保引用正确,同时生成相关问题。</think>### LNMP环境部署教程 LNMPLinux + Nginx + MySQL + PHP)是Web应用开发常用技术栈,部署流程分为以下步骤: #### 一、环境准备 1. 确认系统版本为CentOS 7/8或Ubuntu 18.04+ 2. 使用`yum update`更新系统包(CentOS)或`apt update`(Ubuntu)[^1] 3. 关闭防火墙(测试环境建议操作): ```bash systemctl stop firewalld systemctl disable firewalld setenforce 0 ``` #### 二、安装Nginx(详细步骤见[^2][^4]) 1. 安装依赖包 ```bash yum -y install pcre-devel zlib-devel gcc gcc-c++ make openssl openssl-devel curl curl-devel ``` 2. 创建专用用户 ```bash useradd -M -s /sbin/nologin nginx ``` 3. 编译安装 ```bash cd /opt tar zxvf nginx-1.12.0.tar.gz cd nginx-1.12.0/ ./configure \ --prefix=/usr/local/nginx \ --user=nginx \ --group=nginx \ --with-http_ssl_module \ --with-http_stub_status_module make && make install ``` 4. 创建软链接 ```bash ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/ ``` 5. 启动服务 ```bash nginx ``` 访问`http://服务器IP`显示欢迎页面即成功 #### 三、安装MySQL 1. 添加MySQL YUM源 ```bash rpm -Uvh https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm ``` 2. 安装服务端 ```bash yum -y install mysql-community-server ``` 3. 启动服务 ```bash systemctl start mysqld systemctl enable mysqld ``` 4. 获取初始密码 ```bash grep 'temporary password' /var/log/mysqld.log ``` 5. 执行安全配置向导 ```bash mysql_secure_installation ``` #### 四、安装PHP(依赖项参考[^4]) 1. 安装基础依赖 ```bash yum -y install gd libjpeg-devel libpng-devel freetype-devel libxml2-devel zlib-devel ``` 2. 添加EPEL源 ```bash yum -y install epel-release ``` 3. 安装PHP 7.4 ```bash yum -y install php74-php-fpm php74-php-mysqlnd php74-php-gd ``` 4. 启动PHP-FPM ```bash systemctl start php74-php-fpm systemctl enable php74-php-fpm ``` #### 五、配置整合 1. 修改Nginx配置 ```nginx location ~ \.php$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; include fastcgi.conf; } ``` 2. 重启服务 ```bash nginx -s reload systemctl restart php74-php-fpm ``` 3. 创建测试文件`/usr/local/nginx/html/info.php` ```php <?php phpinfo(); ?> ``` 访问`http://服务器IP/info.php`显示PHP信息即成功
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值