lnmp部署

本文档详细记录了在Linux系统上部署LNMP(Nginx、MySQL、PHP)环境的过程,包括Nginx的安装与配置,MySQL数据库的安装与初始化,以及PHP的编译安装和配置。此外,还展示了如何配置PHP-FPM服务,并通过Nginx代理处理PHP请求。最后,通过创建一个简单的PHP信息页面验证了整个环境的正确性。
摘要由CSDN通过智能技术生成

lnmp部署

安装nginx

[root@server1 bag]# wget http://nginx.org/download/nginx-1.20.1.tar.gz

这里我用脚本安装的

[root@serevr1 lnmp]# cat install.sh 
#!/bin/bash

if [ $UID -ne 0 ];then
        echo "Please use administrator account"
        exit
fi

app_a=nginx-1.20.1.tar.gz

dir_a=/usr/local
dir_b=/var/log
dir_c=nginx-1.20.1

if [ ! -d $dir_b/nginx ];then
        mkdir -p $dir_b/nginx
fi
chown -R nginx.nginx $dir_b/nginx

yum -y install pcre-devel openssl openssl-devel gd-devel gcc gcc-c++  make
yum -y groups mark install 'Development Tools'

id nginx &>/dev/null 
if [ $? -ne 0 ];then
        useradd -r -M -s /sbin/nologin nginx
fi

tar xf bag/$app_a -C $dir_a

cd  $dir_a/$dir_c
if [ ! -d $dir_a/nginx ];then
        ./configure \
                --prefix=$dir_a/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=$dir_b/nginx/access.log \
                --error-log-path=$dir_b/nginx/error.log  && make  && make install
fi

cd ..
if [ ! -f /etc/profile.d/nginx.sh  ];then
        echo "export PATH=$dir_a/nginx/sbin:\$PATH" > /etc/profile.d/nginx.sh
fi

cat > /usr/lib/systemd/system/nginx.service << EOF
[Unit]
Description=Nginx server daemon
Wants=sshd-keygen.target

[Service]
Type=forking
ExecStart=$dir_a/nginx/sbin/nginx
ExecStop=$dir_a/nginx/sbin/nginx -s stop
ExecReload=/bin/kill -HUP $MAINPID

[Install]
WantedBy=multi-user.target
EOF


systemctl daemon-reload
systemctl enable --now nginx

查看nginx服务

[root@serevr1 ~]# ss -antl
State    Recv-Q   Send-Q       Local Address:Port                     Peer Address:Port                 
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                               [::]:*       
[root@serevr1 ~]# ps -ef | grep nginx
root       29712       1  0 15:45 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx      29713   29712  0 15:45 ?        00:00:00 nginx: worker process
root       33425   31719  0 15:47 pts/0    00:00:00 grep --color=auto nginx
[root@serevr1 ]# systemctl status nginx
● nginx.service - Nginx server daemon
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
   Active: active (running) since Tue 2021-10-26 15:45:23 CST; 7min ago
 Main PID: 29712 (nginx)
    Tasks: 2 (limit: 11301)
   Memory: 4.0M
   CGroup: /system.slice/nginx.service
           ├─29712 nginx: master process /usr/local/nginx/sbin/nginx
           └─29713 nginx: worker process

Oct 26 15:45:23 server4 systemd[1]: Starting Nginx server daemon...
Oct 26 15:45:23 server4 systemd[1]: Started Nginx server daemon.

安装数据库

#!/bin/bash

if [ $UID -ne 0 ];then
        echo "root?"
        exit
fi

dir_a=/usr/local
dir_b=/opt/data
app_a=mysql-5.7.34-linux-glibc2.12-x86_64.tar.gz
app_b=mysql-5.7.34-linux-glibc2.12-x86_64

id mysql  &>/dev/null
if [ $? -ne 0 ];then
        useradd -r -M -s /sbin/nologin mysql
 fi


yum -y install ncurses-compat-libs ncurses-devel openssl-devel openssl cmake mariadb-devel 


if [ ! -d $dir_a/$app_b ];then
        tar xf bag/$app_a -C $dir_a
fi

if [ ! -d $dir_a/mysql ];then
        ln -sv $dir_a/$app_b  $dir_a/mysql
fi
chown -R mysql:mysql $dir_a/mysql*

echo "export PATH=$dir_a/mysql/bin:\$PATH" > /etc/profile.d/mysql.sh

source /etc/profile.d/mysql.sh

if [ ! -d /$dir_b ];then
        mkdir -p /$dir_b 
        chown -R mysql.mysql /$dir_b
fi


content=$(ls $dir_b | wc -l)
if [ $content -eq 0  ];then
        mysqld --initialize-insecure --user mysql --datadir $dir_b
fi

cat > /etc/my.cnf <<EOF
[mysqld]
basedir = $dir_a/mysql    
datadir = $dir_b           
socket = /tmp/mysql.sock      
port = 3306                   
pid-file = $dir_b/mysql.pid
user = mysql                  
skip-name-resolve
EOF


sed -ri "s#^(basedir=).*#\1$dir_a/mysql#g" $dir_a/mysql/support-files/mysql.server
sed -ri "s#^(datadir=).*#\1$dir_b#g" $dir_a/mysql/support-files/mysql.server


cat > /usr/lib/systemd/system/mysqld.service <<EOF
[Unit]
Description=mysql server daemon
After=network.target 

[Service]
Type=forking

ExecStart=$dir_a/mysql/support-files/mysql.server start
ExecStop=$dir_a/mysql/support-files/mysql.server stop
ExecReload=/bin/kill -HUP $MAINPID

[Install]
WantedBy=multi-user.target
EOF

systemctl  daemon-reload
systemctl  enable --now  mysqld

查看服务

[root@serevr1 ~]# systemctl status mysqld
● mysqld.service - mysql server daemon
   Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
   Active: active (running) since Tue 2021-10-26 16:52:59 CST; 6min ago
  Process: 284858 ExecStart=/usr/local/mysql/support-files/mysql.server start (code=exited, status=0/SUCCESS)
 Main PID: 284871 (mysqld_safe)
    Tasks: 28 (limit: 11301)
   Memory: 176.0M
   CGroup: /system.slice/mysqld.service
           ├─284871 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/opt/data --pid-file=/opt/data/mysql.pid
           └─285059 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/opt/data --plugin-dir=/usr/local/my>

Oct 26 16:52:58 serevr1 systemd[1]: Starting mysql server daemon...
Oct 26 16:52:58 serevr1 mysql.server[284858]: Starting MySQL.Logging to '/opt/data/serevr1.err'.
Oct 26 16:52:59 serevr1 mysql.server[284858]:  SUCCESS!
Oct 26 16:52:59 serevr1 systemd[1]: Started mysql server daemon.
lines 1-15/15 (END


[root@serevr1 ~]# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.34 MySQL Community Server (GPL)

Copyright (c) 2000, 2021, 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> 
mysql> set password = password('123');
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> exit
Bye
[root@serevr1 ~]# mysql
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
[root@serevr1 ~]# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.34 MySQL Community Server (GPL)

Copyright (c) 2000, 2021, 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

[root@server1 ~]# wget https://www.php.net/distributions/php-8.0.10.tar.xz
[root@server1 ~]# ls
php-8.0.10.tar.xz

解压

[root@server1 ~]# tar -xf  php-8.0.10.tar.gz  -C /usr/local/

安装依赖包

[root@server1 ~]# wget http://mirrors.aliyun.com/repo/epel-7.repo
[root@server1 ~]# 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  libsqlite3x-devel libzip-devel
[root@server1 ~]# yum -y install http://mirror.centos.org/centos/8-stream/PowerTools/x86_64/os/Packages/oniguruma-devel-6.8.2-2.el8.x86_64.rpm

编译安装

[root@server1 ~]# cd /usr/localphp-8.0.10/
[root@server1 php-8.0.10]# ./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
......
......
......
config.status: creating ext/phar/phar.phar.1
config.status: creating main/php_config.h
config.status: executing default commands

+--------------------------------------------------------------------+
| License:                                                           |
| This software is subject to the PHP License, available in this     |
| distribution in the file LICENSE. By continuing this installation  |
| process, you are bound by the terms of this license agreement.     |
| If you do not agree with the terms of this license, you must abort |
| the installation process at this point.                            |
+--------------------------------------------------------------------+

Thank you for using PHP.


[root@server1 php-8.0.10]# make
.......
.......
.......
invertedregexiterator.inc
pharcommand.inc
phar.inc

Build complete.
Don't forget to run 'make test'.

[root@server1 php-8.0.10]# make install
......
......
/root/php-8.0.10/build/shtool install -c ext/phar/phar.phar /usr/local/php8/bin/phar.phar
ln -s -f phar.phar /usr/local/php8/bin/phar
Installing PDO headers:           /usr/local/php8/include/php/ext/pdo/

配置php-pfm

[root@server1 ]# cd /usr/loacl/php-8.0.10
[root@server1 php-8.0.10]# ls /etc/php.ini 
/etc/php.ini
[root@server1 php-8.0.10]# cp /etc/php.ini /opt/
[root@server1 php-8.0.10]# cp php.ini-production /etc/php.ini 
cp: overwrite '/etc/php.ini'? y
[root@server1 php-8.0.10]# cd  sapi/
[root@server1 sapi]# cp fpm/init.d.php-fpm /etc/init.d/php-fpm
[root@server1 sapi]# chmod +x /etc/init.d/php-fpm
[root@server1 sapi]# 

[root@server1 ~]# cd  /usr/local/php8/
[root@server1 php8]# ls
bin  etc  include  lib  php  sbin  var
[root@server1 php8]# cd  etc/
[root@server1 etc]# ls
pear.conf  php-fpm.conf.default  php-fpm.d
[root@server1 etc]# cp php-fpm.conf.default php-fpm.conf
[root@server1 etc]# ls
pear.conf     php-fpm.conf.default
php-fpm.conf  php-fpm.d
[root@server1 etc]# cd  php-fpm.d/
[root@server1 php-fpm.d]# ls
www.conf.default
[root@server1 php-fpm.d]# cp www.conf.default www.conf
[root@server1 php-fpm.d]# ls
www.conf  www.conf.default
[root@server1 php-fpm.d]# 

配置service文件

[root@server1 ~]# cat /usr/lib/systemd/system/php-fpm.service 
[Unit]
Description=php-fpm server daemon
After=network.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@server1 ]# systemctl  daemon-reload

启动服务

[root@serevr1 php-fpm.d]# systemctl start php-fpm
[root@serevr1 php-fpm.d]# ss -antl
State    Recv-Q   Send-Q       Local Address:Port                     Peer Address:Port                 
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:82                            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@serevr1 ~]# php -v
PHP 8.0.10 (cli) (built: Oct 26 2021 18:45:29) ( NTS )
Copyright (c) The PHP Group
Zend Engine v4.0.10, Copyright (c) Zend Technologies
[root@serevr1 ~]# 

配置虚拟主机

[root@serevr1 ~]# cd  /usr/local/nginx/html/
[root@serevr1 html]# ls
50x.html  index.html  test
[root@serevr1 html]# vim index.php
[root@serevr1 html]# cat index.php
<?php
    phpinfo();
?>


[root@serevr1 nginx]# vim  conf/nginx.conf
.........
        location / {
            root   html;
            index  index.php  index.html index.htm;
        }
        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.conf;
        }


查看网页
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值