部署LNMP

LNMP部署

关闭防火墙selinux
[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 ~]# vi /etc/selinux/config
SELINUX=disabled
[root@localhost ~]# setenforce 0

创建系统用户
[root@localhost ~]#: useradd -r -M -s /sbin/nologin nginx
[root@localhost ~]#: id nginx
uid=991(nginx) gid=991(nginx) groups=991(nginx)

安装编译工具
[root@localhost ~]#: yum -y install make gcc gcc-c++ pcre-devel openssl openssl-devel gd-devel

创建nginx系统用户
[root@localhost ~]#: mkdir -p /var/log/nginx
[root@localhost ~]#: chown -R nginx.nginx /var/log/nginx

下载与解压nginx
[root@localhost ~]#: wget https://nginx.org/download/nginx-1.24.0.tar.gz
[root@localhost ~]#: ls
anaconda-ks.cfg nginx-1.24.0.tar.gz
[root@localhost ~]#: tar xf nginx-1.24.0.tar.gz

编译安装
[root@localhost ~]#: cd nginx-1.24.0
[root@localhost 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@localhost ~]#: make && 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:22            0.0.0.0:*              
LISTEN  0        4096           127.0.0.1:631           0.0.0.0:*              
LISTEN  0        511              0.0.0.0:80            0.0.0.0:*              
LISTEN  0        4096               [::1]:631              [::]:*              
LISTEN  0        128                 [::]:22               [::]:*      

[root@localhost ~]#: cp /usr/lib/systemd/system/sshd.service
/usr/lib/systemd/system/nginx.service
[root@localhost ~]#: vim /usr/lib/systemd/system/nginx.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 ~]#: systemctl daemon-reload

[root@localhost ~]#: nginx -s stop
[root@localhost ~]#: systemctl enable --now nginx.service
Created symlink /etc/systemd/system/multi-user.target.wants/nginx.service → /usr/lib/systemd/system/nginx.service.
[root@localhost ~]#: ss -antl
State   Recv-Q   Send-Q     Local Address:Port     Peer Address:Port  Process  
LISTEN  0        128              0.0.0.0:22            0.0.0.0:*              
LISTEN  0        4096           127.0.0.1:631           0.0.0.0:*              
LISTEN  0        511              0.0.0.0:80            0.0.0.0:*              
LISTEN  0        4096               [::1]:631              [::]:*              
LISTEN  0        128                 [::]:22               [::]:*      

输入IP地址进入

安装MYSQL
[root@localhost ~]# yum -y install epel-release  //安装一个通用的源
[root@localhost ~]# yum -y install https://mirror.stream.centos.org/9-stream/CRB/x86_64/os/Packages/mariadb-devel-10.5.16-2.el9.x86_64.rpm  //从网上下载的软件包
[root@localhost ~]# yum -y install ncurses-devel openssl-devel openssl cmake 继续下载所需的软件包

解压软件包

[root@localhost ~]# useradd -r -M -s /sbin/nologin mysql   //创建mysql系统用户
[root@localhost ~]# tar xf mysql-8.0.35-linux-glibc2.28-x86_64.tar.xz -C /usr/local //解压软件包
[root@localhost ~]# cd /usr/local/
[root@localhost local]# mv mysql-8.0.35-linux-glibc2.28-x86_64/ mysql  //更改名字
[root@localhost local]# chown -R mysql.mysql mysql   //设置mysql的所属组与所属者

配置环境变量

[root@localhost local]# echo 'export PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh   //配置环境变量
[root@localhost local]# source /etc/profile.d/mysql.sh   //使配置生效
[root@localhost local]# mkdir /opt/data   //创建mysql数据目录
[root@localhost local]# chown -R mysql.mysql /opt/data   //更改data的所属组与所属者

初始化

[root@localhost local]# mysqld --initialize --user mysql --datadir /opt/data
root@localhost: jSiaD.OIV1rh     //jSiaD.OIV1rh 为mysql一次性密码 需保存

编辑配置文件

[root@localhost local]# vim /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 local]# ls /usr/local/mysql/
bin  docs  include  lib  LICENSE  man  README  share  support-files
[root@localhost local]# ln -s /usr/local/mysql/include/ /usr/include/mysql  //做软连接
[root@localhost local]# vim /etc/ld.so.conf.d/mysql.conf   //让系统找到库文件
/usr/local/mysql/lib
[root@localhost local]# ldconfig -v   //测试能不能找到库文件
/usr/local/mysql/lib: (from /etc/ld.so.conf.d/mysql.conf:1)
        libmysqlclient.so.21 -> libmysqlclient.so.21.2.35  //能找到就可以了

编写帮助文档

[root@localhost local]# ls /usr/local/mysql/
bin  docs  include  lib  LICENSE  man  README  share  support-files
[root@localhost local]# vim /etc/man_db.conf 
MANDATORY_MANPATH                       /usr/man
MANDATORY_MANPATH                       /usr/share/man
MANDATORY_MANPATH                       /usr/local/share/man
MANDATORY_MANPATH                       /usr/local/mysql/man
[root@localhost local]# vim /usr/local/mysql/support-files/mysql.server  //编写脚本文件
basedir=/usr/local/mysql
datadir=/opt/data
[root@localhost local]# /usr/local/mysql/support-files/mysql.server start
Starting MySQL.Logging to '/opt/data/localhost.localdomain.err'.
.. SUCCESS! 

编写mysql配置文件

[root@localhost local]# cp /usr/lib/systemd/system/sshd.service /usr/lib/systemd/system/mysqld.service  //复制配置文件
[root@localhost local]# vim /usr/lib/systemd/system/mysqld.service  /修改配置文件
[Unit]
Description=mysqld 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
[root@localhost ~]# systemctl daemon-reload  //刷新配置文件
[root@localhost ~]# systemctl enable --now mysqld  //设置开机自启并立刻启动服务

更改mysql密码

[root@localhost ~]# mysql -uroot -p'jSiaD.OIV1rh'   //使用一次性密码进入
mysql> alter user root@'localhost' identified with mysql_native_password by 'Passw0rd@_'; //更改密码
Query OK, 0 rows affected (0.01 sec)       

mysql> quit
Bye
安装PHP
[root@localhost ~]# wget https://www.php.net/distributions/php-8.3.0.tar.xz //下载php软件包
--2023-12-11 17:21:59--  https://www.php.net/distributions/php-8.3.0.tar.xz
Resolving www.php.net (www.php.net)... 185.85.0.29, 2a02:cb40:200::1ad
Connecting to www.php.net (www.php.net)|185.85.0.29|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 12431612 (12M) [application/octet-stream]
Saving to: ‘php-8.3.0.tar.xz’

php-8.3.0.tar.xz      100%[=======================>]  11.86M  1.21MB/s    in 12s     

2023-12-11 17:22:12 (1.02 MB/s) - ‘php-8.3.0.tar.xz’ saved [12431612/12431612]

[root@localhost ~]# tar xf php-8.3.0.tar.xz   //解压
[root@localhost ~]# cd php-8.3.0/  切换到php目录

安装必要软件包

[root@localhost php-8.3.0]# 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
[root@localhost php-8.3.0]# cd /etc/yum.repos.d/
[root@localhost yum.repos.d]# ls
epel-cisco-openh264.repo  epel-testing.repo  rocky-devel.repo   rocky.repo
epel.repo                 rocky-addons.repo  rocky-extras.repo
[root@localhost yum.repos.d]# vim rocky-devel.repo
# rocky-devel.repo
#
# devel and no-package-left-behind

[devel]
name=Rocky Linux $releasever - Devel WARNING! FOR BUILDROOT ONLY DO NOT LEAVE ENABLED
mirrorlist=https://mirrors.rockylinux.org/mirrorlist?arch=$basearch&repo=devel-$releasever$rltype
#baseurl=http://dl.rockylinux.org/$contentdir/$releasever/devel/$basearch/os/
gpgcheck=1
enabled=1    //把0修改为1
countme=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-Rocky-9
[root@localhost php-8.3.0]# yum -y install libsqlite3x-devel oniguruma-devel libzip-devel oniguruma-devel
 //安装必要软件包

配置编译文件 .configure

[root@localhost php-8.3.0]# 
./configure --prefix=/usr/local/php7  \
--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

+--------------------------------------------------------------------+
| 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.

报错解决

./configure --help | grep 报错的软件包名

编译PHP

[root@localhost php-8.3.0]# make -j2
[root@localhost php-8.3.0]# make install
Installing shared extensions:     /usr/local/php7/lib/php/extensions/no-debug-non-zts-20230831/
Installing PHP CLI binary:        /usr/local/php7/bin/
Installing PHP CLI man page:      /usr/local/php7/php/man/man1/
Installing PHP FPM binary:        /usr/local/php7/sbin/
Installing PHP FPM defconfig:     /usr/local/php7/etc/
Installing PHP FPM man page:      /usr/local/php7/php/man/man8/
Installing PHP FPM status page:   /usr/local/php7/php/php/fpm/
Installing phpdbg binary:         /usr/local/php7/bin/
Installing phpdbg man page:       /usr/local/php7/php/man/man1/
Installing PHP CGI binary:        /usr/local/php7/bin/
Installing PHP CGI man page:      /usr/local/php7/php/man/man1/
Installing build environment:     /usr/local/php7/lib/php/build/
Installing header files:          /usr/local/php7/include/php/
Installing helper programs:       /usr/local/php7/bin/
  program: phpize
  program: php-config
Installing man pages:             /usr/local/php7/php/man/man1/
  page: phpize.1
  page: php-config.1
Installing PEAR environment:      /usr/local/php7/lib/php/
[PEAR] Archive_Tar    - installed: 1.4.14
[PEAR] Console_Getopt - installed: 1.4.3
[PEAR] Structures_Graph- installed: 1.1.1
[PEAR] XML_Util       - installed: 1.4.5
warning: pear/PEAR dependency package "pear/Archive_Tar" installed version 1.4.14 is not the recommended version 1.4.4
[PEAR] PEAR           - installed: 1.10.13
Wrote PEAR system config file at: /usr/local/php7/etc/pear.conf
You may want to add: /usr/local/php7/lib/php to your php.ini include_path
Installing PDO headers:           /usr/local/php7/include/php/ext/pdo/

配置环境变量

[root@localhost php-8.3.0]# ls /usr/local/php7/
bin etc include lib php sbin var
[root@localhost php-8.3.0]# echo 'export
PATH=/usr/local/php7/bin:/usr/local/php7/sbin:$PATH' > /etc/profile.d/php.sh
[root@localhost php-8.3.0]# source /etc/profile.d/php.sh
[root@localhost php-8.3.0]# ln -s /usr/local/php7/include/ /usr/include/php
[root@localhost php-8.3.0]# vim /etc/ld.so.conf.d/php.conf
/usr/localphp7/lib
[root@localhost php-8.3.0]# ldconfig -v
[root@localhost php-8.3.0]# which php
/usr/local/php7/bin/php
[root@localhost php-8.3.0]# php -v
PHP 8.3.0 (cli) (built: Dec 11 2023 04:11:34) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.3.0, Copyright (c) Zend Technologies
[root@localhost php-8.3.0]#

配置php-fpm

[root@localhost php-8.3.0]# cp php.ini-production /etc/php.ini
cp: overwrite '/etc/php.ini'? y
[root@localhost php-8.3.0]# mkdir /etc/init.d
[root@localhost php-8.3.0]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
[root@localhost php-8.3.0]# chmod +x /etc/init.d/php-fpm
[root@localhost php-8.3.0]# mv /usr/local/php7 /usr/local/php8
[root@localhost php-8.3.0]# cd /usr/local/php8/etc/
[root@localhost etc]# ls
pear.conf php-fpm.conf.default php-fpm.d
[root@localhost etc]#
[root@localhost etc]# cp php-fpm.conf.default php-fpm.conf
[root@localhost etc]# cd php-fpm.d/
[root@localhost php-fpm.d]# ls
www.conf.default
[root@localhost php-fpm.d]# cp www.conf.default www.conf
[root@localhost php-fpm.d]# ls
www.conf www.conf.default
[root@localhost php-fpm.d]# cd

启动php-fpm
[root@localhost ~]# service php-fpm start
Starting php-fpm  done
[root@localhost ~]# ss -antl
State          Recv-Q          Send-Q                   Local Address:Port                    Peer Address:Port         Process         
LISTEN         0               128                            0.0.0.0:22                           0.0.0.0:*                            
LISTEN         0               4096                         127.0.0.1:9000                         0.0.0.0:*                            
LISTEN         0               4096                         127.0.0.1:631                          0.0.0.0:*                            
LISTEN         0               128                          127.0.0.1:6011                         0.0.0.0:*                            
LISTEN         0               151                                  *:3306                               *:*                            
LISTEN         0               511                                  *:80                                 *:*                            
LISTEN         0               128                               [::]:22                              [::]:*                            
LISTEN         0               70                                   *:33060                              *:*                            
LISTEN         0               128                              [::1]:6011                            [::]:*                            
LISTEN         0               4096                             [::1]:631                             [::]:*                            

开机自启

[root@localhost ~]# service php-fpm stop
[root@localhost ~]# cp /usr/lib/systemd/system/sshd.service  /usr/lib/systemd/system/php-fpm.service
[root@localhost ~]# vim /usr/lib/systemd/system/php-fpm.service 

[Unit]
Description=php server daemon
After=network.target 

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

[Install]
WantedBy=multi-user.target

[root@localhost ~]# systemctl daemon-reload
[root@localhost ~]# systemctl enable --now php-fpm
Created symlink /etc/systemd/system/multi-user.target.wants/php-fpm.service → /usr/lib/systemd/system/php-fpm.service.
[root@localhost ~]# ss -antl
State          Recv-Q          Send-Q                   Local Address:Port                    Peer Address:Port         Process         
LISTEN         0               128                            0.0.0.0:22                           0.0.0.0:*                            
LISTEN         0               4096                         127.0.0.1:9000                         0.0.0.0:*                            
LISTEN         0               4096                         127.0.0.1:631                          0.0.0.0:*                            
LISTEN         0               128                          127.0.0.1:6011                         0.0.0.0:*                            
LISTEN         0               151                                  *:3306                               *:*                            
LISTEN         0               511                                  *:80                                 *:*                            
LISTEN         0               128                               [::]:22                              [::]:*                            
LISTEN         0               70                                   *:33060                              *:*                            
LISTEN         0               128                              [::1]:6011                            [::]:*                            
LISTEN         0               4096                             [::1]:631                             [::]:*                           
配置Nginx
[root@localhost nginx]# cd /usr/local/nginx/conf/
[root@localhost 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@localhost conf]# vim nginx.conf 
修改45行
        location / {
            root   html;
            index  index.html index.php index.htm;   //添加index.php
        }
取消6571行的注释并修改
        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;

检查语法
[root@localhost conf]# nginx -t
bash: nginx: command not found...
Install package 'nginx-core' to provide command 'nginx'? [N/y] y


 * Waiting in queue... 
 * Loading list of packages.... 
The following packages have to be installed:
 nginx-core-1:1.20.1-14.el9_2.1.x86_64  nginx minimal core
 nginx-filesystem-1:1.20.1-14.el9_2.1.noarch    The basic directory layout for the Nginx server
Proceed with changes? [N/y] y


 * Waiting in queue... 
 * Waiting for authentication... 
 * Waiting in queue... 
 * Downloading packages... 
 * Requesting data... 
 * Testing changes... 
 * Installing packages... 
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

[root@localhost conf]# nginx -s reload

[root@localhost conf]# cd /usr/local/nginx/html/
[root@localhost html]# vim index.php
<?php
phpinfo();
?>

[root@localhost conf]# nginx -s reload
[root@localhost html]# ss -antl
State   Recv-Q  Send-Q   Local Address:Port    Peer Address:Port Process 
LISTEN  0       128            0.0.0.0:22           0.0.0.0:*            
LISTEN  0       4096         127.0.0.1:631          0.0.0.0:*            
LISTEN  0       4096         127.0.0.1:9000         0.0.0.0:*            
LISTEN  0       4096             [::1]:631             [::]:*            
LISTEN  0       128               [::]:22              [::]:*            
LISTEN  0       151                  *:3306               *:*            
LISTEN  0       70                   *:33060              *:*            

输入IP地址/index.php进入

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值