nginx部署+lnmp

一、部署nginx

1、关闭防火墙selinux(略)
[root@stw ~]# 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@stw ~]# getenforce 
Disabled
2、添加yum源(本地源),安装开发工具包
[root@stw ~]# cd /etc/yum.repos.d/
[root@stw yum.repos.d]# ls
CentOS-Base.repo       CentOS-fasttrack.repo  CentOS-Vault.repo
CentOS-CR.repo         CentOS-Media.repo
CentOS-Debuginfo.repo  CentOS-Sources.repo
[root@stw yum.repos.d]# rm -rf *
[root@stw yum.repos.d]# vim server.repo
[root@stw yum.repos.d]# cat server.repo 
[aa]
name=aa1
baseurl=file:///mnt
enabled=1
gpgcheck=0
[root@stw yum.repos.d]# mount /dev/cdrom /mnt
mount: /dev/sr0 is write-protected, mounting read-only
[root@stw yum.repos.d]# yum -y groupinstall "Development Tools"

3、创建nginx用户
[root@stw yum.repos.d]# useradd -r -M -s /sbin/nologin nginx
4、安装依赖包和一些常用工具(使用网络yum仓库)
[root@stw yum.repos.d]# ls
server.repo
[root@stw yum.repos.d]# rm -rf *
[root@stw yum.repos.d]# curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--    100  2523  100  2523    0     0   8273      0 --:--:-- --:--:-- --:--:--  8299
[root@stw yum.repos.d]# yum -y install epel-release
[root@stw yum.repos.d]# ls
CentOS-Base.repo  epel.repo  epel-testing.repo
[root@stw yum.repos.d]# yum -y install pcre-devel openssl openssl-devel gd-devel gcc gcc-c++ make zlib-devel wget lrzsz
5、创建日志存放目录
[root@stw ~]# mkdir -p /var/log/nginx
[root@stw ~]# chown -R nginx.nginx /var/log/nginx
6、解压nginx源码包
[root@stw ~]# rz -E
rz waiting to receive.
[root@stw ~]# ls
anaconda-ks.cfg  Downloads             nginx-1.24.0.tar.gz  Templates
Desktop          initial-setup-ks.cfg  Pictures             Videos
Documents        Music                 Public
[root@stw ~]# tar -zxf nginx-1.24.0.tar.gz -C /usr/local/
[root@stw ~]# cd /usr/local/
[root@stw local]# ls
bin  games    lib    libexec       sbin   src
etc  include  lib64  nginx-1.24.0  share
[root@stw local]# cd nginx-1.24.0/
[root@stw nginx-1.24.0]# ls
auto     CHANGES.ru  configure  html     man     src
CHANGES  conf        contrib    LICENSE  README
7、编译安装
[root@stw nginx-1.24.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@stw nginx-1.24.0]# make
[root@stw nginx-1.24.0]# make install
8、配置环境变量
[root@stw nginx-1.24.0]# echo 'export PATH=/usr/local/nginx/sbin:$PATH' > /etc/profile.d/nginx.sh
[root@stw nginx-1.24.0]# . /etc/profile.d/nginx.sh 
9、启动nginx,并查看端口情况(80端口)

服务控制方式,使用nginx命令

-t 检查配置文件语法

-v 输出nginx的版本

-c 指定配置文件路径

-s 发送服务控制信号,可选值有stop、quit、reopen、reload

[root@stw ~]# 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@stw ~]# nginx
[root@stw ~]# ss -anlt
State      Recv-Q Send-Q Local Address:Port               Peer Address:Port              
LISTEN     0      128        *:111                    *:*                  
LISTEN     0      128        *:80                     *:*                  
LISTEN     0      128        *:6000                   *:*                  
LISTEN     0      5      192.168.122.1:53                     *:*                  
LISTEN     0      128        *:22                     *:*                  
LISTEN     0      128    127.0.0.1:631                    *:*                  
LISTEN     0      100    127.0.0.1:25                     *:*                  
LISTEN     0      128    127.0.0.1:6010                   *:*                  
LISTEN     0      128       :::111                   :::*                  
LISTEN     0      128       :::6000                  :::*                  
LISTEN     0      128       :::22                    :::*                  
LISTEN     0      128      ::1:631                   :::*                  
LISTEN     0      100      ::1:25                    :::*                  
LISTEN     0      128      ::1:6010                  :::*                  

在这里插入图片描述

10、网页存放位置:/usr/local/nginx/html/index.html
[root@stw ~]# cd /usr/local/nginx
[root@stw nginx]# ls
client_body_temp  fastcgi_temp  logs        sbin       uwsgi_temp
conf              html          proxy_temp  scgi_temp
[root@stw nginx]# cd html/
[root@stw html]# ls
50x.html  index.html
11、nginx -c 指定配置文件路径(查看80端口看是否启动成功)
[root@stw ~]# nginx -s stop   //先停掉服务
[root@stw ~]# cd /usr/local/nginx
[root@stw nginx]# ls
client_body_temp  fastcgi_temp  logs        sbin       uwsgi_temp
conf              html          proxy_temp  scgi_temp
[root@stw nginx]# cd conf/
[root@stw conf]# ls
fastcgi.conf            koi-win             scgi_params
fastcgi.conf.default    mime.types          scgi_params.default
fastcgi_params          mime.types.default  uwsgi_params
fastcgi_params.default  nginx.conf          uwsgi_params.default
koi-utf                 nginx.conf.default  win-utf
[root@stw conf]# cp nginx.conf /opt/   //将两个文件复制到/opt
[root@stw conf]# cp mime.types /opt/
[root@stw conf]# nginx -c /opt/nginx.conf    
[root@stw conf]# ss -anlt
State      Recv-Q Send-Q Local Address:Port               Peer Address:Port              
LISTEN     0      128        *:111                    *:*                  
LISTEN     0      128        *:80                     *:*                  
LISTEN     0      128        *:6000                   *:*                  
LISTEN     0      5      192.168.122.1:53                     *:*                  
LISTEN     0      128        *:22                     *:*                  
LISTEN     0      128    127.0.0.1:631                    *:*                  
LISTEN     0      100    127.0.0.1:25                     *:*                  
LISTEN     0      128    127.0.0.1:6010                   *:*                  
LISTEN     0      128       :::111                   :::*                  
LISTEN     0      128       :::6000                  :::*                  
LISTEN     0      128       :::22                    :::*                  
LISTEN     0      128      ::1:631                   :::*                  
LISTEN     0      100      ::1:25                    :::*                  
LISTEN     0      128      ::1:6010                  :::*                  
[root@stw conf]# nginx -c /opt/nginx.conf -s stop
[root@stw conf]# ss -anlt
State      Recv-Q Send-Q Local Address:Port               Peer Address:Port              
LISTEN     0      128        *:111                    *:*                  
LISTEN     0      128        *:6000                   *:*                  
LISTEN     0      5      192.168.122.1:53                     *:*                  
LISTEN     0      128        *:22                     *:*                  
LISTEN     0      128    127.0.0.1:631                    *:*                  
LISTEN     0      100    127.0.0.1:25                     *:*                  
LISTEN     0      128    127.0.0.1:6010                   *:*                  
LISTEN     0      128       :::111                   :::*                  
LISTEN     0      128       :::6000                  :::*                  
LISTEN     0      128       :::22                    :::*                  
LISTEN     0      128      ::1:631                   :::*                  
LISTEN     0      100      ::1:25                    :::*                  
LISTEN     0      128      ::1:6010                  :::*      

二、LNMP架构部署

1、确保都关闭防火墙selinux
2、搭建运行nginx
[root@stw ~]# nginx
[root@stw ~]# ss -anlt
State      Recv-Q Send-Q Local Address:Port               Peer Address:Port              
LISTEN     0      128        *:111                    *:*                  
LISTEN     0      128        *:80                     *:*                  
LISTEN     0      128        *:6000                   *:*                  
LISTEN     0      5      192.168.122.1:53                     *:*                  
LISTEN     0      128        *:22                     *:*                  
LISTEN     0      128    127.0.0.1:631                    *:*                  
LISTEN     0      100    127.0.0.1:25                     *:*                  
LISTEN     0      128    127.0.0.1:6010                   *:*                  
LISTEN     0      128       :::111                   :::*                  
LISTEN     0      128       :::6000                  :::*                  
LISTEN     0      128       :::22                    :::*                  
LISTEN     0      128      ::1:631                   :::*                  
LISTEN     0      100      ::1:25                    :::*                  
LISTEN     0      128      ::1:6010                  :::*         
3、配置mysql数据库(可直接运行shell脚本配置,略)
[root@stw2 ~]# mysql -uroot -predhat
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 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> 

4、mysql中修改配置文件
[root@stw2 ~]# vim /etc/ld.so.conf.d/mysql.conf 
[root@stw2 ~]# cat /etc/ld.so.conf.d/mysql.conf 
/usr/local/mysql/lib
[root@stw2 ~]# vim /etc/man_db.conf 

在这里插入图片描述

5、部署php
(1)安装依赖包和工具
[root@stw3 ~]# cd /etc/yum.repos.d/
[root@stw3 yum.repos.d]# ls
CentOS-Base.repo       CentOS-fasttrack.repo  CentOS-Vault.repo
CentOS-CR.repo         CentOS-Media.repo
CentOS-Debuginfo.repo  CentOS-Sources.repo
[root@stw3 yum.repos.d]# rm -rf *
[root@stw3 yum.repos.d]# wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
--2025-09-24 15:42:54--  https://mirrors.aliyun.com/repo/Centos-7.repo
Resolving mirrors.aliyun.com (mirrors.aliyun.com)... 150.139.241.198, 150.139.241.199, 150.139.241.202
Connecting to mirrors.aliyun.com (mirrors.aliyun.com)|150.139.241.198|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 2523 (2.5K) [application/octet-stream]
Saving to: ‘/etc/yum.repos.d/CentOS-Base.repo’

100%[==================================>] 2,523       --.-K/s   in 0s      

2025-09-24 15:42:54 (1.41 GB/s) - ‘/etc/yum.repos.d/CentOS-Base.repo’ saved [2523/2523]

[root@stw3 yum.repos.d]# ls
CentOS-Base.repo
[root@stw3 yum.repos.d]# yum -y install epel-release
[root@stw3 yum.repos.d]# ls
CentOS-Base.repo  epel.repo  epel-testing.repo
[root@stw3 ~]# yum -y install 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  readline readline-devel libxslt libxslt-devel  php-mysqlnd
(2)安装并启用php
[root@stw3 ~]# yum -y install https://rpms.remirepo.net/enterprise/remi-release-7.rpm
[root@stw3 ~]# cd /etc/yum.repos.d/
[root@stw3 yum.repos.d]# ls
CentOS-Base.repo   remi-php54.repo  remi-php73.repo  remi-php82.repo
epel.repo          remi-php70.repo  remi-php74.repo  remi-php83.repo
epel-testing.repo  remi-php71.repo  remi-php80.repo  remi.repo
remi-modular.repo  remi-php72.repo  remi-php81.repo  remi-safe.repo
[root@stw3 yum.repos.d]# yum-config-manager --enable remi-php70
[root@stw3 yum.repos.d]# yum -y install php php-cli php-fpm php-mysqlnd php-zip php-devel php-gd php-mbstring php-curl php-xml php-pear php-bcmath php-json php-redis
(3)配置php(改两个地方)
listen = 0.0.0.0:9000
listen.allowed_clients = 192.168.100.10
[root@stw3 ~]# vim /etc/php-fpm.d/www.conf 

在这里插入图片描述

在这里插入图片描述

(4)生成测试页面
[root@stw3 ~]# vim /var/www/html/index.php
[root@stw3 ~]# cat /var/www/html/index.php
<?php
	phpinfo();
?>
[root@stw3 ~]# id apache
uid=48(apache) gid=48(apache) groups=48(apache)
[root@stw3 ~]# chown -R apache.apache /var/www/html/
[root@stw3 ~]# systemctl restart php-fpm
[root@stw3 ~]# systemctl enable php-fpm
Created symlink from /etc/systemd/system/multi-user.target.wants/php-fpm.service to /usr/lib/systemd/system/php-fpm.service.
6、在nginx节点操作
[root@stw ~]# vim /usr/local/nginx/conf/nginx.conf
[root@stw ~]# nginx -s reload  //之前已经启动了nginx,这里不需要重启,但先停再起也可以

在这里插入图片描述

7、测试访问

在这里插入图片描述

更多推荐