搭建LNMP架构

2 篇文章 0 订阅

LNMP

nginx

# 关闭防火墙
[root@slave ~]# systemctl disable --now firewalld
[root@slave ~]# sed -ri 's/^(SELINUX=).*/\1disabled/g' /etc/selinux/config
[root@slave ~]# setenforce 0

# 创建系统用户nginx
[root@slave ~]# useradd  -r -M -s /sbin/nologin nginx

# 安装依赖包
[root@slave ~]# yum -y install pcre-devel openssl openssl-devel gd-devel gcc gcc-c++ make
[root@slave ~]# yum -y groups mark install 'Development Tools'

# 创建日志存放目录
[root@slave ~]# mkdir -p /var/log/nginx
[root@slave ~]# chown -R nginx.nginx /var/log/nginx

# 下载nginx包
[root@slave ~]# cd /usr/src/
[root@master src]# wget http://nginx.org/download/nginx-1.20.1.tar.gz

# 编译安装
[root@slave src]# tar xf nginx-1.20.1.tar.gz
[root@slave src]# cd nginx-1.20.1
[root@slave nginx-1.20.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@slave nginx-1.20.1]# make && make install

# 配置环境变量
[root@sla ve~]# echo 'export PATH=/usr/local/nginx/sbin:$PATH' > /etc/profile.d/nginx.sh
[root@slave ~]# source /etc/profile.d/nginx.sh

# 启动nginx
[root@slave ~]# nginx
[root@slave ~]# 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                     [::]:*    

Mysql

//安装依赖包
[root@slave ~]# yum -y install ncurses-devel openssl-devel openssl cmake mariadb-devel
Updating Subscription Management repositories.
Unable to read consumer identity
This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
上次元数据过期检查:0:18:58 前,执行于 2021年10月26日 星期二 00时14分11秒。
软件包 openssl-devel-1:1.1.1c-15.el8.x86_64 已安装。
软件包 openssl-1:1.1.1c-15.el8.x86_64 已安装。

//创建用户和组
[root@slave ~]# useradd -r -M -s /sbin/nologin mysql

//下载二进制格式的mysql软件包
[root@slave ~]# cd /usr/src/
[root@slave src]# ls
debug  kernels  nginx-1.20.1  nginx-1.20.1.tar.gz
[root@slave src]# ls
debug  kernels  mysql-5.7.35-linux-glibc2.12-x86_64.tar.gz  nginx-1.20.1  nginx-1.20.1.tar.gz
[root@slave src]# tar xf mysql-5.7.35-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
[root@slave src]# ls /usr/local/
bin  games    lib    libexec                              nginx  share
etc  include  lib64  mysql-5.7.35-linux-glibc2.12-x86_64  sbin   src
[root@slave src]# cd /usr/local/
[root@slave local]# mv mysql-5.7.35-linux-glibc2.12-x86_64 mysql
[root@slave local]# ls
bin  etc  games  include  lib  lib64  libexec  mysql  nginx  sbin  share  src
[root@slave local]# 

//修改目录/usr/local/mysql的属主属组
[root@slave local]# chown -R mysql.mysql mysql

//添加环境变量
[root@slave local]# cd
[root@slave ~]# echo 'export PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
[root@slave ~]# source /etc/profile.d/mysql.sh 
[root@slave ~]# ls /usr/local/mysql/
bin   include  LICENSE  README  support-files
docs  lib      man      share
[root@slave ~]# ln -s /usr/local/mysql/include /usr/include/mysql
[root@slave ~]# 
[root@slave ~]# vim /etc/man_db.conf 
[root@slave ~]# cat /etc/man_db.conf 
MANDATORY_MANPATH                       /usr/man
MANDATORY_MANPATH                       /usr/share/man
MANDATORY_MANPATH                       /usr/local/share/man
MANDATORY_MANPATH                       /usr/local/apache/man
MANDATORY_MANPATH                       /usr/local/mysql/man //添加这一行内容

[root@slave ~]# vim /etc/ld.so.conf.d/mysql.conf
[root@slave ~]# cat /etc/ld.so.conf.d/mysql.conf 
/usr/local/msyql/lib
[root@slave ~]# ldconfig
[root@slave ~]# 

//建立数据存放目录
[root@slave ~]# mkdir /opt/data
[root@slave ~]# chown -R mysql.mysql /opt/data
[root@slave ~]# bash

//初始化数据库
[root@slave ~]# chown -R mysql.mysql /opt/data
[root@slave ~]# mysqld --initialize-insecure --user mysql --datadir /opt/data
2021-10-26T04:42:05.716459Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2021-10-26T04:42:05.856574Z 0 [Warning] InnoDB: New log files created, LSN=45790
2021-10-26T04:42:05.890416Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2021-10-26T04:42:05.945415Z 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: 12380e70-3617-11ec-9a5a-000c292ea5d7.
2021-10-26T04:42:05.946349Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2021-10-26T04:42:06.840383Z 0 [Warning] A deprecated TLS version TLSv1 is enabled. Please use TLSv1.2 or higher.
2021-10-26T04:42:06.840411Z 0 [Warning] A deprecated TLS version TLSv1.1 is enabled. Please use TLSv1.2 or higher.
2021-10-26T04:42:06.840771Z 0 [Warning] CA certificate ca.pem is self signed.
2021-10-26T04:42:06.948775Z 1 [Warning] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.


//生成配置文件
[root@slave ~]# cat > /etc/my.cnf <<EOF
> [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
> EOF
[root@slave ~]# cat /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@slave ~]# 

[root@slave ~]# cp /usr/lib/systemd/system/httpd.service /usr/lib/systemd/system/mysqld.service
[root@master ~]# vim /usr/lib/systemd/system/mysqld.service 
[root@master ~]# cat /usr/lib/systemd/system/mysqld.service 
[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
[root@slave ~]# 

[root@slave ~]# vim /usr/local/mysql/support-files/mysql.server
basedir=/usr/local/mysql
datadir=/opt/data  //修改这两行内容

//开机自启
[root@slave ~]# systemctl daemon-reload
[root@slave ~]# systemctl enable --now mysqld
Created symlink /etc/systemd/system/multi-user.target.wants/mysqld.service → /usr/lib/systemd/system/mysqld.service.
[root@slave ~]# 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       80                   *:3306                           *:*                 
LISTEN  0       128               [::]:22                          [::]:*

//修改密码
[root@slave ~]#  yum -y install ncurses-compat-libs

[root@slave ~]# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.35 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> set password = password('xiongke');
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> quit
Bye
[root@slave ~]# 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.35 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(8.0.10)

//安装依赖包
[root@slave src]# 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
Updating Subscription Management repositories.
Unable to read consumer identity
This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
上次元数据过期检查:0:37:56 前,执行于 2021年10月26日 星期二 00时14分11秒。
软件包 libxml2-2.9.7-7.el8.x86_64 已安装。
软件包 libxml2-devel-2.9.7-7.el8.x86_64 已安装。
软件包 openssl-1:1.1.1c-15.el8.x86_64 已安装。
软件包 openssl-devel-1:1.1.1c-15.el8.x86_64 已安装。
软件包 bzip2-1.0.6-26.el8.x86_64 已安装。
软件包 bzip2-devel-1.0.6-26.el8.x86_64 已安装。
软件包 libcurl-7.61.1-12.el8.x86_64 已安装。
软件包 libcurl-devel-7.61.1-12.el8.x86_64 已安装。
软件包 libicu-devel-60.3-2.el8_1.x86_64 已安装。
软件包 libjpeg-turbo-1.5.3-10.el8.x86_64 已安装。
软件包 libjpeg-turbo-devel-1.5.3-10.el8.x86_64 已安装。
软件包 libpng-2:1.6.34-5.el8.x86_64 已安装。
软件包 libpng-devel-2:1.6.34-5.el8.x86_64 已安装。
软件包 openldap-devel-2.4.46-11.el8.x86_64 已安装。
软件包 pcre-devel-8.42-4.el8.x86_64 已安装。
软件包 freetype-2.9.1-4.el8.x86_64 已安装。
软件包 freetype-devel-2.9.1-4.el8.x86_64 已安装。
软件包 gmp-1:6.1.2-10.el8.x86_64 已安装。
软件包 gmp-devel-1:6.1.2-10.el8.x86_64 已安装。
软件包 libmcrypt-2.5.8-26.el8.x86_64 已安装。
软件包 libmcrypt-devel-2.5.8-26.el8.x86_64 已安装。
软件包 readline-7.0-10.el8.x86_64 已安装。
软件包 readline-devel-7.0-10.el8.x86_64 已安装。
软件包 libxslt-1.1.32-4.el8.x86_64 已安装。
软件包 libxslt-devel-1.1.32-4.el8.x86_64 已安装。
软件包 mhash-0.9.9.9-20.el8.x86_64 已安装。
软件包 mhash-devel-0.9.9.9-20.el8.x86_64 已安装。
依赖关系解决。
无需任何处理。
完毕!

//编译安装php
[root@slave ~]# tar xf php-8.0.10.tar.xz -C /usr/src/
[root@slave php-8.0.10]# yum -y install libsqlite3x-devel.x86_64
[root@slave php-8.0.10]# 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@slave 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

[root@slave php-8.0.11]# make && make install

//安装后配置
[root@master php-8.0.11]# echo 'export PATH=/usr/local/php8/bin:$PATH' > /etc/profile.d/php.sh
[root@master php-8.0.11]# source /etc/profile.d/php.sh
[root@slave php-8.0.11]# php -v
PHP 8.0.11 (cli) (built: Oct 26 2021 01:16:09) ( NTS )
Copyright (c) The PHP Group
Zend Engine v4.0.11, Copyright (c) Zend Technologies
[root@slave php-8.0.11]# 

//配置php-fpm

[root@slave php-8.0.11]# cp php.ini-production /etc/php.ini 
cp:是否覆盖'/etc/php.ini'? y
[root@slave php-8.0.11]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
[root@slave php-8.0.11]# chmod +x /etc/init.d/php-fpm 
[root@slave php-8.0.11]# cp /usr/local/php8/etc/php-fpm.conf.default /usr/local/php8/etc/php-fpm.conf
cp:是否覆盖'/usr/local/php8/etc/php-fpm.conf'? y
[root@slave php-8.0.11]# cp /usr/local/php8/etc/php-fpm.d/www.conf.default /usr/local/php8/etc/php-fpm.d/www.conf
cp:是否覆盖'/usr/local/php8/etc/php-fpm.d/www.conf'? y
[root@slave php-8.0.11]# service php-fpm start
Starting php-fpm  done
[root@slave php-8.0.11]# ss -antl
State   Recv-Q  Send-Q   Local Address:Port                Peer Address:Port              
LISTEN  0       128            0.0.0.0:8080                     0.0.0.0:*                 
LISTEN  0       128            0.0.0.0:22                       0.0.0.0:*                 
LISTEN  0       128          127.0.0.1:9000                     0.0.0.0:*                 
LISTEN  0       128                  *:80                             *:*                 
LISTEN  0       128               [::]:22                          [::]:*                 
LISTEN  0       80                   *:3306                           *:*                 
[root@slave php-8.0.11]# 

配置虚拟主机

//编辑php测试页面
[root@slave ~]# cd /usr/local/nginx/html/
[root@slave html]# ls
50x.html  index.html  index.php
[root@slave html]# cat index.
cat: index.: 没有那个文件或目录
[root@slave html]# cat index.php 
<?php
    phpinfo();
?>
[root@slave html]# 

//编辑虚拟主机
[root@slave conf]# vim 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  /usr/local/nginx/html$fastcgi_script_name;
            include        fastcgi_params;
        }

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值