LNMP部署
LNMP简介
LNMP代表的就是:Linux系统下Nginx+MySQL+PHP这种网站服务器架构
Nginx是一个高性能的HTTP和 反向代理服务器,也是一个IMAP/POP3/SMTP代理服务器。
Mysql是一个小型 关系型数据库管理系统。
PHP是一种在服务器端执行的嵌入HTML文档的 脚本语言。
这四种软件均为免费 开源软件,组合到一起,成为一个免费、高效、扩展性强的网站服务系统。
Nginx是一个小巧而高效的Linux下的Web 服务器软件,是由 Igor Sysoev 为俄罗斯访问量第二的 Rambler 站点开发的,已经在一些俄罗斯的大型网站上运行多年,相当的稳定。
Nginx性能稳定、功能丰富、运维简单、处理 静态文件速度快且消耗 系统资源极少。
作为 Web 服务器:相比 Apache,Nginx 使用更少的资源,支持更多的 并发连接,体现更高的效率。
作为 负载均衡服务器:Nginx 既可以在内部直接支持 Rails 和 PHP,也可以支持作为 HTTP 代理服务器对外进行服务。Nginx 用C编写,不论是系统资源开销还是CPU使用效率都比Perlbal要好的多。
作为邮件代理服务器:Nginx同时也是一个非常优秀的邮件代理服务器(最早开发这个产品的目的之一也是作为邮件代理服务器),Last/fm 描述了成功并且美妙的使用经验。
Nginx 安装非常的简单,配置文件非常简洁(还能够支持 perl语法)。Nginx支持平滑加载新的配置,还能够在不间断服务的情况下进行软件版本的升级。
LNMP部署
环境说明
系统信息 | IP地址 | 需要安装的服务 |
---|---|---|
centos8 | 192.168.205.152 | nginx |
centos8 | 192.168.205.154 | MySQL |
centos8 | 192.168.205.155 | php |
安装nginx
//关闭防火墙与SELINUX
[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# systemctl disable firewalld
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@localhost ~]# setenforce 0
[root@localhost ~]# sed -ri 's/^(SELINUX=).*/\1disabled/g' /etc/selinux/config
//创建系统用户nginx
[root@localhost ~]# useradd -r -M -s /sbin/nologin nginx
//安装依赖环境
[root@localhost ~]# dnf -y install pcre-devel openssl openssl-devel gd-devel gcc gcc-c++ vim make wget
//创建日志存放目录
[root@localhost ~]# mkdir -p /var/log/nginx
[root@localhost ~]# chown -R nginx.nginx /var/log/nginx/
//下载nginx
[root@localhost ~]# cd /usr/src/
[root@localhost src]# ls
debug kernels nginx-1.20.2.tar.gz
//编译安装
[root@localhost src]# tar xf nginx-1.20.2.tar.gz
[root@localhost src]# cd nginx-1.20.2
[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
//配置环境变量
[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:80 0.0.0.0:*
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 [::]:22 [::]:*
安装MySQL
//关闭防火墙与SELINUX
[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# systemctl disable firewalld
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@localhost ~]# setenforce 0
[root@localhost ~]# sed -ri 's/^(SELINUX=).*/\1disabled/g' /etc/selinux/config
//安装依赖包
[root@localhost ~]# dnf -y install ncurses-devel openssl-devel openssl cmake mariadb-devel
//创建用户和组
[root@localhost ~]# useradd -r -M -s /sbin/nologin mysql
//下载mysql
[root@localhost ~]# cd /usr/src/
[root@localhost src]# ls
debug kernels mysql-5.7.38-linux-glibc2.12-x86_64.tar.gz
//解压软件至/usr/local/
[root@localhost src]# tar xf mysql-5.7.38-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
[root@localhost src]# cd /usr/local/
[root@localhost local]# ln -sv mysql-5.7.38-linux-glibc2.12-x86_64/ mysql
'mysql' -> 'mysql-5.7.38-linux-glibc2.12-x86_64/'
[root@localhost local]# ll
total 0
drwxr-xr-x. 2 root root 6 May 18 2020 bin
drwxr-xr-x. 2 root root 6 May 18 2020 etc
drwxr-xr-x. 2 root root 6 May 18 2020 games
drwxr-xr-x. 2 root root 6 May 18 2020 include
drwxr-xr-x. 2 root root 6 May 18 2020 lib
drwxr-xr-x. 3 root root 17 Sep 27 06:51 lib64
drwxr-xr-x. 2 root root 6 May 18 2020 libexec
lrwxrwxrwx. 1 root root 36 Oct 11 02:46 mysql -> mysql-5.7.38-linux-glibc2.12-x86_64/
drwxr-xr-x. 9 root root 129 Oct 11 02:44 mysql-5.7.38-linux-glibc2.12-x86_64
drwxr-xr-x. 2 root root 6 May 18 2020 sbin
drwxr-xr-x. 5 root root 49 Sep 27 06:51 share
drwxr-xr-x. 2 root root 6 May 18 2020 src
//修改目录/usr/local/mysql的属主属组
[root@localhost local]# chown -R mysql.mysql mysql
[root@localhost local]# ll
total 0
drwxr-xr-x. 2 root root 6 May 18 2020 bin
drwxr-xr-x. 2 root root 6 May 18 2020 etc
drwxr-xr-x. 2 root root 6 May 18 2020 games
drwxr-xr-x. 2 root root 6 May 18 2020 include
drwxr-xr-x. 2 root root 6 May 18 2020 lib
drwxr-xr-x. 3 root root 17 Sep 27 06:51 lib64
drwxr-xr-x. 2 root root 6 May 18 2020 libexec
lrwxrwxrwx. 1 mysql mysql 36 Oct 11 02:46 mysql -> mysql-5.7.38-linux-glibc2.12-x86_64/
drwxr-xr-x. 9 root root 129 Oct 11 02:44 mysql-5.7.38-linux-glibc2.12-x86_64
drwxr-xr-x. 2 root root 6 May 18 2020 sbin
drwxr-xr-x. 5 root root 49 Sep 27 06:51 share
drwxr-xr-x. 2 root root 6 May 18 2020 src
[root@localhost local]# ls /usr/local/mysql
LICENSE README bin docs include lib man share support-files
[root@localhost local]# ln -s /usr/local/mysql/include/ /usr/include/mysql/
[root@localhost local]# echo '/usr/local/mysql/lib/' > /etc/ld.so.conf.d/mysql.conf
[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 ~]# echo 'export PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
[root@localhost ~]# source /etc/profile.d/mysql.sh
[root@localhost ~]# which mysql
/usr/local/mysql/bin/mysql
//建立数据存放目录
[root@localhost ~]# mkdir -p /opt/data
[root@localhost ~]# chown -R mysql.mysql /opt/data
[root@localhost ~]# ll /opt/
total 0
drwxr-xr-x. 2 mysql mysql 6 Oct 11 02:53 data
//初始化数据库
[root@localhost ~]# mysqld --initialize --user=mysql --datadir=/opt/data/
2022-10-11T06:54:57.037135Z 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:54:57.241013Z 0 [Warning] InnoDB: New log files created, LSN=45790
2022-10-11T06:54:57.265445Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2022-10-11T06:54:57.321912Z 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: 9e1c14d6-4931-11ed-b985-000c29120aff.
2022-10-11T06:54:57.322452Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2022-10-11T06:54:57.510335Z 0 [Warning] A deprecated TLS version TLSv1 is enabled. Please use TLSv1.2 or higher.
2022-10-11T06:54:57.510347Z 0 [Warning] A deprecated TLS version TLSv1.1 is enabled. Please use TLSv1.2 or higher.
2022-10-11T06:54:57.510624Z 0 [Warning] CA certificate ca.pem is self signed.
2022-10-11T06:54:57.538833Z 1 [Note] A temporary password is generated for root@localhost: gRX&ikKFi9tL
[root@localhost ~]# echo 'gRX&ikKFi9tL' > pass
[root@localhost ~]# cat pass
gRX&ikKFi9tL
//生成配置文件
[root@localhost ~]# 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@localhost ~]# cd /usr/local/mysql/
[root@localhost mysql]# ls
LICENSE bin include man support-files
README docs lib share
[root@localhost mysql]# cd support-files/
[root@localhost support-files]# ls
magic mysql-log-rotate mysql.server mysqld_multi.server
[root@localhost support-files]# cp mysql.server /etc/init.d/mysqld
[root@localhost support-files]# vim /etc/init.d/mysqld
basedir=/usr/local/mysql //添加
datadir=/opt/data
[root@localhost support-files]# chmod +x /etc/init.d/mysqld
//启动mysql
[root@localhost ~]# service mysqld start
Starting MySQL.Logging to '/opt/data/localhost.localdomain.err'.
SUCCESS!
[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 ~]# cat pass
gRX&ikKFi9tL
[root@localhost ~]# mysql -uroot -p
Enter password:
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> set password = password('jiang123!');
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> exit
Bye
安装php
//关闭防火墙与SELINUX
[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# systemctl disable firewalld
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@localhost ~]# setenforce 0
[root@localhost ~]# sed -ri 's/^(SELINUX=).*/\1disabled/g' /etc/selinux/config
//配置网络源
[root@localhost ~]# dnf -y install epel-release
//下载php
[root@localhost ~]# ls
anaconda-ks.cfg php-8.1.11.tar.gz
//安装依赖包
[root@localhost ~]# dnf -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.
[root@localhost php-8.1.11]# make && make install
................................
Installing man pages: /usr/local/php8/php/man/man1/
page: phpize.1
page: php-config.1
/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 php-8.1.11]# which php
/usr/local/php8/bin/php
//配置php-fpm
[root@localhost php-8.1.11]# cp php.ini-production /etc/php.ini
cp: overwrite '/etc/php.ini'? y
[root@localhost php-8.1.11]# cd sapi/fpm/
[root@localhost fpm]# cp init.d.php-fpm.in /etc/init.d/php-fpm
[root@localhost fpm]# chmod +x /etc/rc.d/init.d/php-fpm
[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]# vim /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
[root@localhost ~]# mkdir -p /www/abc
[root@localhost ~]# vim /www/abc/index.php
[root@localhost ~]# cat /www/abc/index.php
<?php
phpinfo();
?>
[root@localhost php-fpm.d]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 127.0.0.1:9000 0.0.0.0:*
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 [::]:22 [::]:*
配置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.205.154:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /www/abc$fastcgi_script_name;
include fastcgi_params;
[root@localhost ~]# systemctl restart nginx.service