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支持平滑加载新的配置,还能够在不间断服务的情况下进行软件版本的升级。
环境说明:
使用的系统平台 | IP | 需要安装的服务 |
---|---|---|
CentOS8 | 192.168.200.9 | nginx-1.20.1.tar.gz mysql-5.7.34 php-8.0.10 |
nginx部署
安装依赖包和工具包
[root@localhost ~]# yum -y install pcre-devel openssl openssl-devel gd-devel gcc gcc-c++ make wget
[root@localhost ~]# yum -y group mark install "Development Tools"
创建用户
[root@localhost ~]# useradd -r -M -s /sbin/nologin nginx
创建日志存放目录
[root@localhost ~]# mkdir /var/log/nginx -p
下载nginx,解压
[root@localhost ~]# cd /usr/src/soft/
[root@localhost soft]# wget http://nginx.org/download/nginx-1.20.1.tar.gz
[root@localhost soft]# ls
nginx-1.20.1.tar.gz
[root@localhost soft]# tar xf nginx-1.20.1.tar.gz -C /usr/local
[root@localhost soft]# cd /usr/local/
[root@localhost local]# ls
bin games lib libexec sbin src
etc include lib64 mysql nginx-1.20.1 share
编译安装
[root@localhost 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
.......
checking for OS
+ Linux 4.18.0-257.el8.x86_64 x86_64
checking for C compiler ... found
......
nginx http proxy temporary files: "proxy_temp"
nginx http fastcgi temporary files: "fastcgi_temp"
nginx http uwsgi temporary files: "uwsgi_temp"
nginx http scgi temporary files: "scgi_temp"
[root@localhost nginx-1.20.1]# make && make install
安装后配置
配置环境变量
[root@localhost local]# nginx
-bash: nginx: command not found
[root@localhost local]# echo "export PATH=/usr/local/nginx/sbin:$PATH" > /etc/profile.d/nginx.sh
[root@localhost local]# bash
[root@localhost local]# which nginx
/usr/local/nginx/sbin/nginx
启动nginx
[root@localhost ~]# /usr/local/nginx/sbin/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 128 0.0.0.0:80(nginx端口) 0.0.0.0:*
LISTEN 0 128 [::]:22 [::]:*
给nginx配置开机自启
[root@localhost ~]# cp /usr/lib/systemd/system/sshd.service /usr/lib/systemd/system/nginxd.service
[root@localhost ~]# vim /usr/lib/systemd/system/nginxd.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
KillMode=process
[Install]
WantedBy=multi-user.target
[root@localhost ~]# systemctl daemon-reload # 重新加载
[root@localhost ~]# systemctl enable --now nginxd # 启动nginx并设置开机自启
Created symlink /etc/systemd/system/multi-user.target.wants/nginxd.service → /usr/lib/systemd/system/nginxd.service.
MySQL部署
下载mysql
https://downloads.mysql.com/archives/get/file/mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz
将其上传到/usr/src目录下
[root@localhost src]# ls
debug kernels mysql-5.7.34-linux-glibc2.12-x86_64.tar.gz
创建用户和组
[root@localhost src]# groupadd -r mysql
[root@localhost src]# useradd -M -s /sbin/nologin -g mysql mysql
将其解压到/usr/local/目录下
[root@localhost src]# tar xf mysql-5.7.34-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
[root@localhost ~]# cd /usr/local/
[root@localhost local]# ls
bin games lib libexec sbin src
etc include lib64 mysql-5.7.34-linux-glibc2.12-x86_64 share
创建软连接
[root@localhost local]# ln -s mysql-5.7.34-linux-glibc2.12-x86_64 mysql
[root@localhost local]# ll
total 0
......
lrwxrwxrwx. 1 root root 35 Aug 25 08:33 mysql -> mysql-5.7.34-linux-glibc2.12-x86_64
drwxr-xr-x. 9 root root 129 Aug 25 08:32 mysql-5.7.34-linux-glibc2.12-x86_64
修改目录/usr/local/mysql的属主属组
[root@localhost local]# chown -R mysql.mysql /usr/local/mysql*
[root@localhost local]# ll
total 0
.......
lrwxrwxrwx. 1 mysql mysql 35 Aug 25 08:33 mysql -> mysql-5.7.34-linux-glibc2.12-x86_64
drwxr-xr-x. 9 mysql mysql 129 Aug 25 08:32 mysql-5.7.34-linux-glibc2.12-x86_64
添加环境变量
[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 ~]# echo $PATH
/usr/local/mysql/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
建立数据存放目录
[root@localhost ~]# mkdir /opt/data
[root@localhost ~]# chown -R mysql.mysql /opt/data
[root@localhost ~]# ll /opt/
total 0
drwxr-xr-x. 2 mysql mysql 6 Aug 25 08:48 data
初始化数据库
[root@localhost ~]# /usr/local/mysql/bin/mysqld --initialize --user=mysql --datadir=/opt/data/
2021-10-26T15:50:24.131273Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2021-10-26T15:53:24.270072Z 0 [Warning] InnoDB: New log files created, LSN=45790
2021-10-26T15:53:25.295810Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2021-10-26T15:53:25.352039Z 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: 03d19f26-05a3-11ec-bf6e-000c29bb4cb2.
2021-10-26T15:53:25.352561Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2021-10-26T15:53:25.039258Z 0 [Warning] CA certificate ca.pem is self signed.
2021-10-26T15:53:25.357745Z 1 [Note] A temporary password is generated for root@localhost: gr;JyBKJF3(u
//请注意,这个命令的最后会生成一个临时密码,此处密码是 gr;JyBKJF3(u
//再次注意,这个密码是随机的,每个人都不一样,一定要记住这个密码,因为一会登录时会用到
//将临时密码写入到一个文件里,等下会用到
[root@localhost ~]# echo "gr;JyBKJF3(u" > passwd
[root@localhost ~]# cat passwd
gr;JyBKJF3(u
查看是否缺少依赖包
[root@localhost ~]# ldd /usr/local/mysql/bin/mysql(ldd是看某一个程序文件它所依赖的包,如果没有,就不能用,就需要用yum安装,查找哪个包提供的(yum whatprovides pkgs_name))
linux-vdso.so.1 (0x00007ffdf09ea000)
libpthread.so.0 => /lib64/libpthread.so.0 (0x00007fdfeb8dd000)
librt.so.1 => /lib64/librt.so.1 (0x00007fdfeb6d4000)
libdl.so.2 => /lib64/libdl.so.2 (0x00007fdfeb4d0000)
libncurses.so.5 => /lib64/libncurses.so.5 (0x00007fdfeb2aa000)
libstdc++.so.6 => /lib64/libstdc++.so.6 (0x00007fdfeaf15000)
libm.so.6 => /lib64/libm.so.6 (0x00007fdfeab93000)
libgcc_s.so.1 => /lib64/libgcc_s.so.1 (0x00007fdfea97b000)
libc.so.6 => /lib64/libc.so.6 (0x00007fdfea5b9000)
libtinfo.so.5 => /lib64/libtinfo.so.5 (0x00007fdfea38e000)
/lib64/ld-linux-x86-64.so.2 (0x00007fdfebafd000)
生成配置文件
[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 support-files]# pwd
/usr/local/mysql/support-files
[root@localhost support-files]# vim mysql.server
44 # overwritten by settings in the MySQL configuration files.
45
46 basedir=/usr/local/mysql # 安装mysql的位置
47 datadir=/opt/data # 数据存放的位置
使用脚本控制服务启动
[root@localhost ~]# /usr/local/mysql/support-files/mysql.server 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
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 80 *:3306 *:*
尝试登录数据库
[root@localhost ~]# cat passwd
gr;JyBKJF3(u
[root@localhost ~]# mysql -uroot -p'gr;JyBKJF3(u'
mysql: error while loading shared libraries: libncurses.so.5: cannot open shared object file: No such file or directory # 提示没有 "libncurses.so.5"
[root@localhost ~]# yum whatprovides libncurses.so.5 # 查找并安装
Failed to set locale, defaulting to C.UTF-8
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.
Last metadata expiration check: 0:00:52 ago on Wed Aug 25 09:25:55 2021.
ncurses-compat-libs-6.1-7.20180224.el8.i686 : Ncurses compatibility libraries
Repo : baseos
Matched from:
Provide : libncurses.so.5
[root@localhost ~]# yum -y install ncurses-compat-libs # 安装
Failed to set locale, defaulting to C.UTF-8
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.
.....
再次登录并修改密码
# 登录
[root@localhost ~]# mysql -uroot -p'gr;JyBKJF3(u'
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.34
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('redhat'); #修改密码
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> exit
Bye
[root@localhost ~]# mysql -uroot -predhat # 使用修改后的密码登录成功
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 3
Server version: 5.7.34 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>
配置启动脚本
# 找一个文件修改
[root@localhost system]# pwd
/usr/lib/systemd/system
[root@localhost system]# cp sshd.service mysqld.service # 复制一份内容sshd.service到mysqld.service
[root@localhost system]# cat mysqld.service # 修改完成后
[Unit]
Description=mysql server daemon # mysql服务器守护程序
After=network.target # 网络之后启动
[Service]
Type=forking # 类型为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 system]# systemctl daemon-reload
[root@localhost system]# systemctl start mysqld
[root@localhost system]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 0.0.0.0:22 0.0.0.0:* LISTEN 0 128 0.0.0.0:80 0.0.0.0:*
LISTEN 0 80 *:3306 *:*
LISTEN 0 128 [::]:22 [::]:*
[root@localhost system]# systemctl status mysqld
● mysqld.service - mysql server daemon
Loaded: loaded (/usr/lib/systemd/system/mysqld.service; disabled; vendor preset: disabled)
Active: active (running) since Wed 2021-08-25 09:56:30 EDT; 12s ago
PHP部署
安装php
下载依托源
[root@localhost ~]# yum -y install epel-release
下载依赖包
yum -y install sqlite-devel libzip-devel 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
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
下载php
官网是php.net
[root@localhost ~]# wget https://www.php.net/distributions/php-8.0.10.tar.gz
解压
[root@localhost]# ls
php-8.0.10.tar.gz
[root@localhost]# tar xf php-8.0.10.tar.gz
编译安装php
[root@localhost ~]# cd /usr/local/php-8.0.10/
[root@localhost 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
....
--------------------------------------------------------------------+
| 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.0.10]# make
[root@localhost php-8.0.10]# make install
添加环境变量
[root@localhost ~]# echo 'export PATH=/usr/local/php8/bin:$PATH' > /etc/profile.d/php.sh
[root@localhost ~]# source /etc/profile.d/php.sh
配置php-fpm
[root@localhost ~]# cd php-8.0.10
[root@localhost php-8.0.10]# cp php.ini-production /etc/php.ini
cp: overwrite '/etc/php.ini'? y
[root@localhost php-8.0.10]# cd sapi/
[root@localhost sapi]# cd fpm/
[root@localhost fpm]# cp init.d.php-fpm /etc/init.d/php-fpm
[root@localhost fpm]# pwd
/root/php-8.0.10/sapi/fpm
[root@localhost fpm]# chmod +x /etc/init.d/php-fpm
[root@localhost ~]# cd /usr/local/php8/
[root@localhost php8]# ls
bin etc include lib php sbin var
[root@localhost php8]# cd etc/
[root@localhost etc]# cp php-fpm.conf.default php-fpm.conf
[root@localhost etc]# ls
pear.conf php-fpm.conf php-fpm.conf.default php-fpm.d
[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 ~]# service php-fpm start # 启动PHP
Starting php-fpm done
[root@localhost ~]# 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: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 [::]:*
nginx配置支持PHP
[root@localhost ~]# cd /usr/local/nginx/conf/
[root@localhost conf]# vim nginx.conf
........
46 location / {
47 root html;
48 index index.html index.php index.htm; # 添加index.php
49 }
.......
68 location ~ \.php$ {
69 root html;
70 fastcgi_pass 127.0.0.1:9000;
71 fastcgi_index index.php;
72 #fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; # 注释掉
73 include fastcgi.conf; # 将fastcgi_params改为fastcgi.conf
74 }
.......
[root@localhost conf]# nginx -t # 检查配置文件/usr/local/nginx/conf/nginx.conf 修改是否正确
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@localhost conf]# nginx -s reload # 重新加载配置文件/usr/local/nginx/conf/nginx.conf
添加php文件
[root@localhost ~]# cd /usr/local/nginx/html/
[root@localhost html]# ll
total 12
-rw-r--r--. 1 root root 494 Oct 26 00:49 50x.html
-rw-r--r--. 1 root root 612 Oct 26 00:49 index.html
[root@localhost html]# vim index.php
[root@localhost html]# cat index.php
<?php
phpinfo();
?>
[root@localhost ~]# nginx -s reload # 重新加载
[root@localhost ~]# service php-fpm restart # 重启php
Gracefully shutting down php-fpm . done
Starting php-fpm done
访问测试