lnmp部署(源码)

lnmp部署说明

环境说明:
系统centos8

主机名IP部署功能版本
root@lnmp8192.168.143.105nginx部署、mysql的部署、php的部署nginx1.20.1 mysql.5.7.31 php8.0.10

nginx部署

//关闭防火墙、selinux
[root@lnmp8~]#  systemctl disable --now firewalld
[root@lnmp8~]#  sed -ri 's/^(SELINUX=).*/\1disabled/g' /etc/selinux/config
[root@lnmp8~]#  reboot
//创建系统用户nginx
[root@lnmp8~]# useradd -r -M -s /sbin/nologin nginx

//安装依赖环境
[root@lnmp8 ~]# yum -y install pcre-devel openssl openssl-devel gd-devel gcc gcc-c++
安装过程略....
[root@lnmp8 ~]# yum -y groups mark install 'Development Tools'
//创建日志存放目录
[root@lnmp8 ~]# mkdir -p /var/log/nginx
[root@lnmp8 ~]# chown -R nginx.nginx /var/log/nginx

//下载nginx
[root@lnmp8 ~]# cd /usr/src/
[root@lnmp8  src]# wget http://nginx.org/download/nginx-1.20.1.tar.gz
//编译安装
[root@lnmp8  src]# ls
debug  kernels  nginx-1.12.1.tar.gz
[root@lnmp8  src]# tar xf nginx-1.12.1.tar.gz
[root@lnmp8 src]# cd nginx-1.12.1
[root@lnmp8  nginx-1.12.1]# ./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@lnmp8 nginx-1.12.1]# make -j $(grep 'processor' /proc/cpuinfo | wc -l) && make install
安装过程略....
//设置开机自启
[root@lnmp8 ~]# cat > /usr/lib/systemd/system/nginx.service <<EOF
[Unit]
Description=nginx service
After=network.target 
   
[Service] 
Type=forking 
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
PrivateTmp=true 
   
[Install] 
WantedBy=multi-user.target
EOF
[root@lnmp8 ~]# systemctl daemon-reload
[root@lnmp8 ~]# systemctl enable --now nginx   
//设置环境变量
[root@lnmp8 ~]# echo 'export PATH=/usr/local/nginx/sbin:$PATH' > /etc/profile.d/nginx.sh
[root@lnmp8 ~]# source /etc/profile.d/nginx.sh
[root@lnmp8 ~]# nginx
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
^C
[root@lnmp8 ~]# ss -atnl
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}

测试web页面
在这里插入图片描述

mysql的部署

#安装依赖包(cmake:编译自动配置工具)
[root@lnmp8 ~]# yum -y install ncurses-devel openssl-devel openssl cmake mariadb-devel libaio-devel.x86_64
#创建用户
[root@lnmp8 ~]# useradd -r -M -s /sbin/nologin mysql
#下载mysql5.7.31包
[root@lnmp8 ~]# wget https://downloads.mysql.com/archives/get/p/23/file/mysql-5.7.31-linux-glibc2.12-x86_64.tar.gz
#解压放到/usr/local下
#改名字方便操作
[root@lnmp8 ~]# cd /usr/local
[root@lnmp8 ~]# mv mysql5.7.31 mysql
#改属主
[root@lnmp8 ~]# chown -R mysql.mysql mysql/
#环境变量
[root@lnmp8 ~]# echo 'export PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
[root@lnmp8 ~]# source /etc/profile.d/mysql.sh
#头文件映射
l[root@lnmp8 ~]# ln -s /usr/local/mysql/include /usr/include/mysql
#帮助文档
[root@lnmp8 ~]# vim /etc/man_db.conf
 23 MANDATORY_MANPATH                       /usr/local/apache/man
 24 MANDATORY_MANPATH                       /usr/local/mysql/man
#创建数据目录
[root@lnmp8 ~]# mkdir /opt/data
[root@lnmp8 ~]# chown -R mysql.mysql /opt/data
#初始化
[root@lnmp8 ~]# mysqld --initialize --user=mysql --datadir=/opt/data 
//无密码初始化
[root@lnmp8 ~]# /usr/local/mysql/bin/mysqld --initialize-insecure --user mysql --datadir=/opt/data
#编写配置文件
[root@lnmp8 ~]# cat >/etc/my.cnf <<EOF
[mysqld]
basedir = /usr/local/mysql #安装目录
datadir = /opt/data        #数据目录
socket = /tmp/mysql.sock   #套接字
user = mysql			  #用户
port = 3306				  #端口号
pid-file = /opt/data/mysql.pid #进程号文件
skip-name-resolve          #跳过域名解析
EOF						退出

#编写脚本
[root@lnmp8 ~]# cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
[root@lnmp8 ~]# vim /etc/init.d/mysqld
datadir=/opt/data //修改此行为
basedir=/usr/local/mysql //修改此行为

#启动mysql
[root@lnmp8 ~]# service mysqld start
[root@lnmp8 ~]# ss -antl

#安装libncurses.so.5包
[root@lnmp8 ~]# yum whatprovides libncurses.so.5
[root@lnmp8 ~]# yum -y install ncurses-compat-libs

#配置system服务启动文件
[root@lnmp8 ~]# cat > /usr/lib/systemd/system/mysqld.service <<EOF
[Unit]
Description=Mysql server daemon
After=network.target

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

[Install]
WantedBy=multi-user.target
EOF

#加载启动服务文件
[root@lnmp8 ~]# systemctl daemon-reload
[root@lnmp8 ~]#  systemctl enable --now mysqld

#修改mysql密码
[root@lnmp8 mysql]# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.7.31 MySQL Community Server (GPL)

Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.

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> quit
Bye

php的部署

//安装依赖包
[root@lnmp8 ~]# yum -y install epel-release
[root@lnmp8 ~]# 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 libmcrypt libmcrypt-devel readline readline-devel libxslt libxslt-devel mhash mhash-devel php-mysqlnd https://repo.almalinux.org/almalinux/8/PowerTools/x86_64/os/Packages/oniguruma-devel-6.8.2-2.el8.x86_64.rpm
//编译安装php
[root@lnmp8 ~]# cd /usr/src/
[root@lnmp8 src~]# wget https://www.php.net/distributions/php-8.0.10.tar.xz
[root@lnmp8 src]# ls
debug  kernels  nginx-1.20.1  php-8.0.10.tar.xz
[root@lnmp8 src]# tar xf php-8.0.10.tar.xz 
[root@lnmp8 src]# cd php-8.0.10/
 [root@lnmp8 src]# ./configure --prefix=/usr/local/php8  \
--with-config-file-path=/etc \
--enable-fpm \
--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-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@lnmp8 php-8.0.10]# make -j $(cat /proc/cpuinfo |grep processor|wc -l)
[root@lnmp8 php-8.0.10]# make install
//配置环境变量
[root@lnmp8 php-8.0.10]# echo 'export PATH=/usr/local/php8/bin:$PATH' > /etc/profile.d/php8.sh
[root@lnmp8 php-8.0.10]# source /etc/profile.d/php8.sh 
[root@lnmp8 php-8.0.10]# which php
/usr/local/php8/bin/php
[root@lnmp8 php-8.0.10]# php -v
PHP 8.0.10 (cli) (built: Oct 26 2021 03:16:49) ( NTS )
Copyright (c) The PHP Group
Zend Engine v4.0.10, Copyright (c) Zend Technologies
//配置php-fpm
[root@lnmp8 php-8.0.10]# cp php.ini-production /etc/php.ini
cp:是否覆盖'/etc/php.ini'? yes
[root@lnmp8 php-8.0.10]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
[root@lnmp8 php-8.0.10]# chmod +x /etc/rc.d/init.d/php-fpm
[root@lnmp8 php-8.0.10]# cd
[root@lnmp8 ~]# cp /usr/local/php8/etc/php-fpm.conf.default /usr/local/php8/etc/php-fpm.conf
[root@lnmp8 ~]# cp /usr/local/php8/etc/php-fpm.d/www.conf.default /usr/local/php8/etc/php-fpm.d/www.conf
[root@lnmp8 ~]# service php-fpm start
Starting php-fpm  done
[root@lnmp8 ~]#  ss -antl |grep 9000
LISTEN 0      128        127.0.0.1:9000      0.0.0.0:* 
//设置开机自启
[root@lnmp8 ~]# cat /usr/lib/systemd/system/php-fpm.service
[Unit]
Description=php-fpm server daemon
After=network.target php-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@lnmp8 ~]# service php-fpm stop
Gracefully shutting down php-fpm . done
[root@lnmp8 ~]# systemctl daemon-reload
[root@lnmp8 ~]# systemctl enable --now 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.
[root@lnmp8 ~]# systemctl status php-fpm.service
● php-fpm.service - php-fpm server daemon
   Loaded: loaded (/usr/lib/systemd/system/php-fpm.service; enabl>
   Active: active (running) since Tue 2021-10-26 03:34:55 EDT; 9s>
  Process: 158405 ExecStart=/etc/init.d/php-fpm start (code=exite>
 Main PID: 158407 (php-fpm)
    Tasks: 3 (limit: 23493)
   Memory: 10.9M
   CGroup: /system.slice/php-fpm.service
           ├─158407 php-fpm: master process (/usr/local/php8/etc/>
           ├─158408 php-fpm: pool www
           └─158409 php-fpm: pool www

1026 03:34:55 lnmp8 systemd[1]: Starting php-fpm server daemon>
1026 03:34:55 lnmp8 php-fpm[158405]: Starting php-fpm  done
1026 03:34:55 lnmp8 systemd[1]: Started php-fpm server daemon.

#创建php测试页面
[root@lnmp8 ~]# vim /usr/local/nginx/html/index.php
<?php
    phpinfo();
?>

#修改nginx主配置文件
[root@lnmp8 ~]#  vim /usr/local/nginx/conf/nginx.conf
...
        location / {
            root   html;
            index index.php  index.html index.htm;	//在index后面添加index.php
        }
...
#取消注释
        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;
        }


...

#重启nginx
[root@lnmp8 ~]#  nginx -s stop
[root@lnmp8 ~]#  nginx

web页面测试
在这里插入图片描述
在这里插入图片描述

故障案例

访问lnmp php页面显示File not found.

1 error日志显示报错信息
2021/10/26 04:33:33 [error] 159156#0: *16 FastCGI sent in stderr: “Primary script unknown” while reading response header from upstream, client: 192.168.143.1, server: localhost, request: “GET / HTTP/1.1”, upstream: “fastcgi://127.0.0.1:9000”, host: “192.168.143.105”

2 错误原因及解决方法:
主配置文件第69行定义调用脚本文件路径:
fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
而Nginx识别不到/scripts路径,所以phpinfo验证信息就无法正常通过,需要修改如下:
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
Note:
$document_root 代表当前请求在root指令中指定的值。
最后重启一下
nginx -s stop
nginx

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值