首页 > 其他分享 >Lnmp环境搭建

Lnmp环境搭建

时间:2022-10-12 02:22:05浏览次数:59  
标签:Masters -- 环境 Lnmp usr mysql php root 搭建

Lnmp环境搭建

目录

lnmp介绍

LNMP代表的就是:
Linux系统下Nginx+MySQL+PHP这种网站服务器架构。

  • Linux是一类Unix计算机操作系统的统称,是目前最流行的免费操作系统。代表版本有:debian、centos、ubuntu、fedora、gentoo等。
  • Nginx是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP代理服务器。
  • Mysql是一个小型关系型数据库管理系统。
  • PHP是一种在服务器端执行的嵌入HTML文档的脚本语言

lnmp的优势

作为 Web 服务器:
相比 Apache,Nginx 使用更少的资源,支持更多的并发连接,体现更高的效率。

作为负载均衡服务器:
Nginx 既可以在内部直接支持Rails和PHP,也可以支持作为 HTTP代理服务器对外进行服务。Nginx 用C编写,不论是系统资源开销还是CPU使用效率都比Perlbal要好的多。

作为邮件代理服务器:
Nginx同时也是一个非常优秀的邮件代理服务器(最早开发这个产品的目的之一也是作为邮件代理服务器),Last/fm 描述了成功并且美妙的使用经验。

Nginx 安装非常的简单,配置文件非常简洁(还能够支持perl语法)。
Nginx支持平滑加载新的配置,还能够在不间断服务的情况下进行软件版本的升级。

lnmp搭建

环境说明:

主机名称 系统 IP 相应环境
Masters CentOS8 192.168.142.133 Nginx
Masters_A CentOS8 192.168.142.134 MySQL
Masters_B CentOS7(虚拟机不够用了) 192.168.142.136 PHP
环境名称 Nginx MySQL PHP
环境版本 1.20.2 5.7.38 8.11.1

环境准备:

1.关闭防火墙
2.关闭SeLinux
3.配置阿里源

下载安装Nginx

主机名称:Master

//创建系统用户nginx
[root@Masters ~]# useradd -rMs /sbin/nologin nginx

//安装依赖环境
[root@Masters ~]# yum -y install pcre pcre-devel openssl openssl-devel gd-devel gcc gcc-c++ make
安装过程略....

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

//下载nginx
[root@Masters ~]# cd /usr/src/
[root@Masters src]# wget http://nginx.org/download/nginx-1.20.2.tar.gz
......
[root@Masters src]# ls
debug  kernels  nginx-1.20.2.tar.gz  

//编译安装
[root@Masters src]# tar xf nginx-1.20.2.tar.gz 
[root@Masters src]# cd nginx-1.20.2/
[root@Masters nginx-1.20.2]#  ./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
......

//这里使用全部的CPU核心加快编译安装
[root@Masters nginx-1.20.2]# make -j $(grep 'processor' /proc/cpuinfo | wc -l) && make install 
安装过程略....

//配置环境变量
[root@localhost ~]# echo 'export PATH=/usr/local/nginx/sbin:$PATH' > /etc/profile.d/nginx.sh
[root@localhost ~]# . /etc/profile.d/nginx.sh
    
[root@Masters ~]# cp /usr/lib/systemd/system/sshd.service /usr/lib/systemd/system/nginx.service

[root@Masters ~]# vim /usr/lib/systemd/system/nginx.service
[root@Masters ~]# cat /usr/lib/systemd/system/nginx.service 
[Unit]
Description=nginx server daemon
After=network.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@Masters ~]# systemctl daemon-reload 
   
//启动nginx设置开机自启动
[root@Masters ~]# systemctl start nginx.service 
[root@Masters ~]# systemctl enable nginx.service 
Created symlink /etc/systemd/system/multi-user.target.wants/nginx.service → /usr/lib/systemd/system/nginx.service.
[root@Masters ~]# ss -anlt
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                       [::]:* 

下载安装MySQL

主机名称:Master_A

//创建用户和组
[root@Masters_A ~]# cd /usr/src/
[root@Masters_A src]# useradd -rMs /sbin/nologin mysql
 
//解压软件之/usr/local/
[root@Masters_A src]# tar xf mysql-5.7.38-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
 
//生成的链接文件
[root@Masters_A src]# cd /usr/local/
[root@Masters_A local]# ln -sv mysql-5.7.38-linux-glibc2.12-x86_64/ mysql
'mysql' -> 'mysql-5.7.38-linux-glibc2.12-x86_64/'

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

//添加环境变量
[root@Masters_A local]# cd bin
[root@Masters_A bin]# echo 'export PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
[root@Masters_A bin]# . /etc/profile.d/mysql.sh
[root@Masters_A bin]# which mysql
/usr/local/mysql/bin/mysql
 
//创建头文件
[root@Masters_A bin]# cd ..
[root@Masters_A local]# ln -sv /usr/local/mysql/include/ /usr/include/mysql
'/usr/include/mysql' -> '/usr/local/mysql/include/'
[root@Masters_A local]# vim /etc/ld.so.conf.d/mysql.conf
[root@Masters_A local]# cat /etc/ld.so.conf.d/mysql.conf
/usr/local/mysql/lib/
[root@Masters_A local]# ldconfig 
[root@Masters_A local]# vim /etc/man_db.conf 
[root@Masters_A local]# cat /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@Masters_A mysql]# mkdir /opt/data
[root@Masters_A mysql]# chown -R mysql.mysql /opt/data/
[root@Masters_A mysql]# ll /opt/
total 0
drwxr-xr-x. 2 mysql mysql 6 Oct 11 14:55 data
 
//初始化数据库
[root@Masters_A mysql]# mysqld --initialize --user mysql --datadir /opt/data/
......
2022-10-11T06:56:41.800351Z 1 [Note] A temporary password is generated for root@localhost: o;kEyavo<1Ko

//请注意,这个命令的最后会生成一个临时密码,此处密码是 o;kEyavo<1Ko
//注意,这个密码是随机的每次密码生成的不一样
 
//生成配置文件
[root@Masters_A mysql]# vim /etc/my.cnf
[root@Masters_A mysql]# 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
sql-mode = STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
 
//配置服务启动脚本
[root@Masters_A ~]# cp -a /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
[root@Masters_A ~]# vim /etc/init.d/mysqld
[root@Masters_A ~]# cat /etc/init.d/mysqld
······
# overwritten by settings in the MySQL configuration files.
 
basedir=/usr/local/mysql	//添加mysql位置和日志位置
datadir=/opt/data
 
# Default value, in seconds, afterwhich the script should timeout waiting
# for server start. 
 
//启动mysql
[root@Masters_A ~]# service mysqld start
Starting MySQL.Logging to '/opt/data/Masters_A.err'.
 SUCCESS! 
[root@Masters_A ~]# ss -anlt
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           80                            *:3306                        *:*                      
LISTEN      0           128                        [::]:22                       [::]:*    
 
//修改密码
//使用临时密码登录
//这里是缺少依赖ncurses-compat-libs
[root@Masters_A ~]# mysql -uroot -p'o;kEyavo<1Ko'
mysql: error while loading shared libraries: libncurses.so.5: cannot open shared object file: No such file or directory

//下载依赖环境
[root@Masters_A ~]# yum -y install ncurses-compat-libs
 
[root@Masters_A ~]# mysql -uroot -p'o;kEyavo<1Ko'
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.38

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>

mysql> set password = password('Qwer1234!');
Query OK, 0 rows affected, 1 warning (0.00 sec)

//给Masters_B主机权限
mysql> grant all on *.* to [email protected] identified by 'Qwer1234!';
Query OK, 0 rows affected, 1 warning (0.01 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

mysql> quit
Bye

下载安装PHP

主机名称:Master_B

//安装依赖包
yum -y install pcre-devel openssl openssl-devel gd-devel gcc gcc-c++   wget  make

//安装依赖包
 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    libxml2-devel   sqlite-devel    https://vault.centos.org/centos/8/PowerTools/x86_64/os/Packages/oniguruma-devel-6.8.2-2.el8.x86_64.rpm  https://vault.centos.org/centos/8/AppStream/x86_64/os/Packages/libzip-devel-1.5.1-2.module_el8.2.0+313+b04d0a66.x86_64.rpm  --nobest


//content7的安装方法不一样,如这俩包:(https://vault.centos.org/centos/8/PowerTools/x86_64/os/Packages/oniguruma-devel-6.8.2-2.el8.x86_64.rpm  https://vault.centos.org/centos/8/AppStream/x86_64/os/Packages/libzip-devel-1.5.1-2.module_el8.2.0+313+b04d0a66.x86_64.rpm):

解决方法:
dnf -y install oniguruma-devel = 6.8.2
wget https://libzip.org/download/libzip-1.3.2.tar.gz
tar zxf libzip-1.3.2.tar.gz
cd libzip-1.3.2/
./configure && make && make install
后面的编译步骤都一样
///



//安装依赖包
[root@Masters_B php-8.1.11]# wget https://vault.centos.org/centos/8/BaseOS/x86_64/os/Packages/libcurl-7.61.1-22.el8.x86_64.rpm
[root@Masters_B php-8.1.11]# yum install -y libcurl-devel.x86_64

//编译安装php
[root@Masters_B ~]# cd /usr/src/
[root@Masters_B src]# tar xf php-8.1.11.tar.gz -C /usr/local/
[root@Masters_B src]# cd /usr/local/php-8.1.11/
[root@Masters_B php-8.1.11]#./configure --prefix=/usr/local/php8  --with-config-file-path=/etc --enable-fpm --enable-inline-optimization --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-json --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@Masters_B php-8.1.11]# make && make install
......

//安装后配置
root@Masters_B php-8.1.11]# echo 'export PATH=/usr/local/php8/bin:$PATH' > /etc/profile.d/php8.sh
[root@Masters_B php-8.1.11]# source /etc/profile.d/php8.sh 
[root@Masters_B php-8.1.11]# php -v
PHP 8.1.11 (cli) (built: Oct 11 2022 16:54:32) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.1.11, Copyright (c) Zend Tech 
 
//配置php-fpm
[root@Masters_B php-8.1.11]# cp php.ini-production /etc/php.ini 
cp: overwrite '/etc/php.ini'? y
[root@Masters_B php-8.1.11]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
[root@Masters_B php-8.1.11]# chmod +x /etc/rc.d/init.d/php-fpm
[root@Masters_B php-8.1.11]# cp /usr/local/php8/etc/php-fpm.conf.default /usr/local/php8/etc/php-fpm.conf
[root@Masters_B php-8.1.11]# cp /usr/local/php8/etc/php-fpm.d/www.conf.default /usr/local/php8/etc/php-fpm.d/www.conf

[root@Masters_B ~]# vim /usr/local/php8/etc/php-fpm.d/www.conf
listen = 192.168.142.136:9000  // 修改ip为Masters_B
listen.allowed_clients = 192.168.142.133   //添加此行 ip为Masters

[root@Masters_B php-8.1.11]# vim /usr/local/php8/etc/php-fpm.conf
//配置文件最后一行后添加
pm.max_children = 50    
pm.start_servers = 5    
pm.min_spare_servers = 2    
pm.max_spare_servers = 8 

//创建网页存放目录
[root@Masters_B ~]# mkdir www
[root@Masters_B ~]# cd /www/
[root@Masters_B www]# vim index.php
[root@Masters_B www]# cat index.php 
<?php
        phpinfo();
?>

//启动php
[root@Masters_B ~]# cd /usr/local/php-8.1.11/
[root@Masters_B php-8.1.11]# service php-fpm start
Starting php-fpm  done
[root@Masters_B php-8.1.11]# ss -anlt
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           128              192.168.142.136:9000                  0.0.0.0:*                      
LISTEN      0           128                        [::]:22                       [::]:*     

配置网站页面

Masters主机

[root@Masters ~]# vim /usr/local/nginx/conf/nginx.conf

server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;
        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.php index.html index.htm;   //加上index.php
        }

//取消注释并修改
 location ~ \.php$ {
            root           /www;
            fastcgi_pass   192.168.142.136:9000; 
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME $document_root $fastcgi_script_name;
            include        fastcgi_params;
        }
        
[root@Masters ~]# cd /usr/local/nginx/html/
[root@Masters html]# vim index.php
[root@Masters html]# cat index.php 
<?php
        phpinfo();
?>

标签:Masters,--,环境,Lnmp,usr,mysql,php,root,搭建
From: https://www.cnblogs.com/soap-bubble/p/16783169.html

相关文章