lnmp部署
lnmp的简介
LNMP代表的就是:Linux系统下Nginx+MySQL+PHP这种网站服务器架构。
(1)、Linux是一类Unix计算机操作系统的统称,是目前最流行的免费操作系统。代表版本有:debian、centos、ubuntu、fedora、gentoo等。
(2)、Nginx (engine x) 是一个高性能的HTTP和反向代理web服务器,同时也提供了IMAP/POP3/SMTP服务。其特点是占有内存少,并发能力强,事实上nginx的并发能力在同类型的网页服务器中表现较好
(3)、MySQL是一种开放源代码的关系型数据库管理系统(RDBMS),使用最常用的数据库管理语言--结构化查询语言(SQL)进行数据库管理。MySQL不仅是开放源代码的,也因为其速度、可靠性和适应性而备受关注。大多数人都认为在不需要事务化处理的情况下,MySQL是管理内容最好的选择。
(4)、PHP即“超文本预处理器”,是一种通用开源脚本语言。PHP是在服务器端执行的脚本语言,与C语言类似,是常用的网站编程语言。因为PHP的开源性、免费性、快捷性等特点使其成为目前最流行的编程语言。
3、优点:
(1)、四种软件均为免费开源软件,组合到一起,成为一个免费、高效、扩展性强的网站服务系统。
(2)、Nginx使用更少的资源,支持更多的并发连接,体现更高的效率。
(3)、Nginx 既可以在内部直接支持Rails和PHP,也可以支持作为 HTTP代理服务器对外进行服务。
(4)、Nginx 安装非常的简单,配置文件非常简洁(还能够支持perl语法)。Nginx支持平滑加载新的配置,还能够在不间断服务的情况下进行软件版本的升级。
部署环境:
ip | 服务 | |
---|---|---|
centos8 | 192.168.171.142 | nginx1.20.2 |
centos8 | 192.168.171.150 | mysql5.7.39 |
centos8 | 192.168.171.133 | php8.1.11 |
nginx的安装
//关闭防火墙和selinux
[root@localhost ~]# sed -i 's/SELINUX=.*/SELINUX=disabled/' /etc/selinux/config
[root@localhost ~]# systemctl disable --now firewalld.service
[root@localhost ~]# setenforce 0
//创建nginx用户
[root@localhost ~]# useradd -r -M -s /sbin/nologin nginx
//安装依赖环境
[root@localhost ~]# yum -y install pcre-devel openssl openssl-devel gd-devel gcc gcc-c++ vim wget make
//下载源码包
[root@localhost ~]# wget http://nginx.org/download/nginx-1.20.2.tar.gz
[root@localhost ~]# ls
anaconda-ks.cfg nginx-1.20.2.tar.gz
//创建日志目录,安装目录
[root@localhost ~]# mkdir -p /usr/local/nginx
[root@localhost ~]# mkdir -p /var/log/nginx
[root@localhost ~]# chown nginx.nginx /var/log/nginx
drwxr-xr-x. 2 nginx nginx 6 Oct 10 10:05 nginx
//解压源码包
[root@localhost ~]# tar xf nginx-1.20.2.tar.gz
[root@localhost ~]# ls
anaconda-ks.cfg nginx-1.20.2 nginx-1.20.2.tar.gz
//进入解压目录进行编译安装
[root@localhost nginx-1.20.2]# ls
CHANGES CHANGES.ru LICENSE README auto conf configure contrib html man src
[root@localhost 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
[root@localhost nginx-1.20.2]# make && make install
//给nginx做环境变量
[root@localhost ~]# echo 'export PATH=/usr/local/nginx/sbin:$PATH' > /etc/profile.d/nginx.sh
[root@localhost ~]# source /etc/profile.d/nginx.sh
//启动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:80 0.0.0.0:*
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 [::]:22 [::]:*
//这样无法让它开机自启,所以这里需要写service文件。
[root@localhost ~]# cp /usr/lib/systemd/system/sshd.service /usr/lib/systemd/system/nginx.service
[root@localhost ~]# vi /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@localhost ~]# systemctl daemon-reload
[root@localhost ~]# systemctl start nginx
[root@localhost ~]# ss -antl
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 [::]:*
//设置服务开机自启
[root@localhost ~]# systemctl enable nginx.service
Created symlink /etc/systemd/system/multi-user.target.wants/nginx.service → /usr/lib/systemd/system/nginx.service.
[root@localhost ~]# systemctl status nginx.service
● nginx.service - nginx server daemon
Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
Active: active (running) since Mon 2022-10-10 10:30:23 EDT; 2min 0s ago
Main PID: 187761 (nginx)
Tasks: 2 (limit: 25291)
Memory: 2.2M
CGroup: /system.slice/nginx.service
├─187761 nginx: master process /usr/local/nginx/sbin/nginx
└─187762 nginx: worker process
Oct 10 10:30:23 localhost.localdomain systemd[1]: Starting nginx server daemon...
Oct 10 10:30:23 localhost.localdomain systemd[1]: Started nginx server daemon.
[root@localhost ~]#
mysql的安装
//下载依赖包
[root@localhost ~]# dnf -y install ncurses-devel openssl-devel openssl cmake mariadb-devel ncurses-compat-libs
//创建用户和组
[root@localhost ~]# useradd -rMs /sbin/nologin mysql
//下载mysql压缩包
[root@localhost ~]# wget https://downloads.mysql.com/archives/get/p/23/file/mysql-5.7.39-linux-glibc2.12-x86_64.tar.gz
[root@localhost ~]# ls
anaconda-ks.cfg mysql-5.7.39-linux-glibc2.12-x86_64.tar.gz
//解压到指定目录
[root@localhost ~]# tar xf mysql-5.7.39-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
[root@localhost ~]# cd /usr/local/
[root@localhost local]# ls
bin etc games include lib lib64 libexec mysql-5.7.39-linux-glibc2.12-x86_64 sbin share src
//给mysql做个软连接
[root@localhost local]# ln -s mysql-5.7.39-linux-glibc2.12-x86_64/ mysql
[root@localhost local]# ls
bin etc games include lib lib64 libexec mysql mysql-5.7.39-linux-glibc2.12-x86_64 sbin share src
[root@localhost local]# chown -R mysql.mysql mysql
[root@localhost local]# ll
lrwxrwxrwx. 1 mysql mysql 36 Oct 11 02:11 mysql -> mysql-5.7.39-linux-glibc2.12-x86_64/
#开始配置环境变量
//把头文件 库文件 和帮助文档做环境变量
[root@localhost mysql]# ln -s /usr/local/mysql/include /usr/include/mysql
[root@localhost mysql]# echo '/usr/local/mysql/lib/' > /etc/ld.so.conf.d/mysql.conf
[root@localhost mysql]# vi /etc/man_db.conf
MANDATORY_MANPATH /usr/local/mysql/man
[root@localhost mysql]# echo 'export PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
[root@localhost mysql]# source /etc/profile.d/mysql.sh
//创建mysql的数据目录然后初始化 ,数据库初始化的时候会生成一个密码
[root@localhost mysql]# mkdir -p /opt/data
[root@localhost mysql]# chown -R mysql.mysql /opt/data/
[root@localhost mysql]# mysqld --initialize --user mysql --datadir /opt/data/
2022-10-11T06:19:22.048698Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2022-10-11T06:19:22.251728Z 0 [Warning] InnoDB: New log files created, LSN=45790
2022-10-11T06:19:22.280212Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2022-10-11T06:19:22.336383Z 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: a58f2482-492c-11ed-a85c-000c2984ee47.
2022-10-11T06:19:22.337499Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2022-10-11T06:19:22.645312Z 0 [Warning] A deprecated TLS version TLSv1 is enabled. Please use TLSv1.2 or higher.
2022-10-11T06:19:22.645367Z 0 [Warning] A deprecated TLS version TLSv1.1 is enabled. Please use TLSv1.2 or higher.
2022-10-11T06:19:22.646091Z 0 [Warning] CA certificate ca.pem is self signed.
2022-10-11T06:19:22.783592Z 1 [Note] A temporary password is generated for root@localhost: j,XRvpe+M9dk
#最后面的密码就是j,XRvpe+M9dk
//给数据库添加配置文件
[root@localhost ~]# vi /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
//让数据库开机自启,我们这边写一下service文件
[root@localhost ~]# vi /usr/lib/systemd/system/mysql.service
[Unit]
Description=mysql
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 restart mysql、
[root@localhost ~]# systemctl enable mysql.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 128 [::]:22 [::]:*
LISTEN 0 80 *:3306 *:*
//进入数据库改一下数据库密码,第一次进入数据库需要用到生成的初始密码
[root@localhost ~]# mysql -uroot -pj,XRvpe+M9dk
mysql> set password = password ('123456');
Query OK, 0 rows affected, 1 warning (0.00 sec)
//退出用创建的密码进来
[root@localhost ~]# mysql -uroot -p123456
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.39 MySQL Community Server (GPL)
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>
//数据库安装完毕
php的安装
//下载网络上的php安装包php8.1.11版本的
[root@localhost ~]# wget https://www.php.net/distributions/php-8.1.11.tar.gz
[root@localhost ~]# ls
anaconda-ks.cfg php-8.1.11.tar.gz
//下载依赖包
[root@localhost ~]# yum -y install epel-release.noarch
[root@localhost ~]# yum -y install libxml2-devel openssl-devel curl-devel libjpeg-devel libpng-devel libicu-devel freetype-devel openldap-devel openldap openldap-devel gcc gcc-c++ sqlite-devel libzip-devel http://mirror.centos.org/centos/8-stream/PowerTools/x86_64/os/Packages/oniguruma-devel-6.8.2-2.el8.x86_64.rpm openssl libcurl-devel.x86_64 libpng.x86_64 libpng-devel.x86_64 freetype-devel --allowerasing
//解压源码包
[root@localhost ~]# tar -xf php-8.1.11.tar.gz
[root@localhost ~]# cd php-8.1.11/
[root@localhost php-8.1.11]# ./configure --prefix=/usr/local/php8 --with-config-file-path=/usr/local/php/etc --enable-mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --enable-fpm --enable-static --enable-sockets --with-zip --enable-calendar --enable-bcmath --enable-mbstring --with-zlib --with-iconv=/usr/local/libiconv --enable-gd --enable-mbstring --with-curl --with-freetype --disable-ipv6 --disable-debug --with-openssl --enable-intl --enable-opcach
+--------------------------------------------------------------------+
| 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: WARNING: unrecognized options: --enable-opcach
//然后编译安装
[root@localhost php-8.1.11]# make && make install
...
/root/php-8.1.11/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/
//做环境变量
[root@localhost php-8.1.11]# echo 'export PATH=/usr/local/php8/bin:$PATH' > /etc/profile.d/php8.sh
[root@localhost php-8.1.11]# source /etc/profile.d/php8.sh
[root@localhost etc]# cp php-fpm.conf.default php-fpm.conf
[root@localhost php-fpm.d]# cp www.conf.default www.conf
//这里写service文件设置开机自启
[root@localhost sbin]# vi /usr/lib/systemd/system/php8.service
[Unit]
Description=php
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/php8/sbin/php-fpm
ExecStop=ps -ef |grep php|grep -v grep|awk '{print $2}' |xargs kill -9
ExecReload=/bin/kill -HUP $MAINPID
[Install]
WantedBy=multi-user.target
[root@localhost sbin]# systemctl status php8.service
● php8.service - php
Loaded: loaded (/usr/lib/systemd/system/php8.service; disabled; vendor preset: disabled)
Active: active (running) since Tue 2022-10-11 04:48:44 EDT; 2s ago
Process: 773338 ExecStart=/usr/local/php8/sbin/php-fpm (code=exited, status=0/SUCCESS)
Main PID: 773339 (php-fpm)
Tasks: 3 (limit: 23460)
Memory: 7.7M
CGroup: /system.slice/php8.service
├─773339 php-fpm: master process (/usr/local/php8/etc/php-fpm.conf)
├─773340 php-fpm: pool www
└─773341 php-fpm: pool www
Oct 11 04:48:44 localhost.localdomain systemd[1]: Starting php...
[root@localhost ~]# systemctl enable php8.service
//配置php,生成php测试页面
[root@localhost ~]# mkdir -p /www/abc
[root@localhost ~]# vim /www/abc/index.php
[root@localhost ~]# cat /www/abc/index.php
<?php
phpinfo();
?>
[root@localhost ~]#
[root@localhost ~]# vim /usr/local/php8/etc/php-fpm.d/www.conf
listen = 192.168.171.150:9000
;listen.allowed_clients = 192.168.171.142
[root@localhost ~]# systemctl restart php8.service
配置nginx
//创建虚拟主机目录并生成php测试页面
[root@localhost ~]# mkdir -p /www/abc
[root@localhost ~]# vim /www/abc/index.php
[root@localhost ~]# cat /www/abc/index.php
<?php
phpinfo();
?>
[root@localhost ~]#
[root@localhost ~]# chown -R nginx.nginx /www/abc/
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
location / {
root /www/abc;
index index.php index.html index.htm;
location ~ \.php$ {
root /www/abc;
fastcgi_pass 192.168.171.150:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /www/abc$fastcgi_script_name;
include fastcgi_params;
[root@localhost ~]# systemctl restart nginx.service