部署LNMP
部署顺序:
Nginx --> MariaDB -->Php
环境说明
主机名 | 安装服务 | 系统版本 | ip |
---|---|---|---|
LNMP | nginx,mysql,php | redhat8 | 192.168.200.46 |
1.安装niginx
#关闭防火墙 SElinux
[root@LNMP ~]# systemctl disable --now firewalld
[root@LNMP ~]# sed -i 's/^SELINUX=*/SELINUX=disabled/g' /etc/selinux/config
[root@LNMP ~]# setenforce 0
#配置yum源
[root@LNMP ~]# curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo
[root@LNMP ~]# sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo
[root@LNMP ~]# yum makecache
#配置epel源
[root@LNMP ~]# yum install -y https://mirrors.aliyun.com/epel/epel-release-latest-8.noarch.rpm
[root@LNMP ~]# sed -i 's|^#baseurl=https://download.example/pub|baseurl=https://mirrors.aliyun.com|' /etc/yum.repos.d/epel*
[root@LNMP ~]# sed -i 's|^metalink|#metalink|' /etc/yum.repos.d/epel*
#创建系统用户nginx
[root@LNMP ~]# useradd -r -M -s /sbin/nologin nginx
#安装依赖环境
[root@LNMP ~]# yum -y install pcre-devel openssl openssl-devel gd-devel gcc gcc-c++ make wget vim
#预安装Development Tools 包组
[root@LNMP ~]# yum -y groups mark install 'Development Tools'
#创建日志存放目录
[root@LNMP ~]# mkdir -p /var/log/nginx
[root@LNMP ~]# chown -R nginx.nginx /var/log/nginx
#下载nginx
[root@LNMP ~]# cd /usr/src/
[root@LNMP src]# wget http://nginx.org/download/nginx-1.24.0.tar.gz
[root@LNMP src]# tar xf nginx-1.24.0.tar.gz
#编译安装
[root@LNMP ~]# cd /usr/src/nginx-1.24.0
[root@LNMP nginx-1.24.0]#
./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@LNMP nginx-1.24.0]# make -j $(grep 'processor' /proc/cpuinfo | wc -l) && make install
niginx安装后配置
[root@LNMP ~]# echo 'export PATH=/usr/local/nginx/sbin:$PATH' > /etc/profile.d/nginx.sh
[root@LNMP ~]# . /etc/profile.d/nginx.sh
#配置nginx配置文件
[root@LNMP ~]# cd /usr/local/nginx/conf
[root@LNMP conf]# cat nginx.conf
# 指定运行woker进程的用户和组
#user nobody;
# 是否以守护进程方式运行nginx,调试时应设置为off
# daemon {on|off};
# 是否以master/worker模型来运行nginx,调试时可以设置为off
# master_process {on|off};
# 启动n个worker进程,通常设置为cpu总核心数-1或等于总核心数
worker_processes 4;
# 设置所有worker进程最大可以打开的文件数,默认为1024
worker_rlimit_nofile 65535;
# 使用二进制表示cpu核心数
worker_cpu_affinity 0001 0010 0100 1000;
# 错误日志位置
error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
# 指定nginx守护进程的pid文件
pid logs/nginx.pid;
# 每个进程能够接受的最大连接数
events {
worker_connections 1024;
}
# nginx作为web服务器时使用的配置:http{}段的配置参数
# 协议级别http
http {
include mime.types;
default_type application/octet-stream;
# log_format 定义日志格式
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log logs/access.log main;
# 开启高效文件传输模式 sendfile on
# tcp_nopush on 需要在sendfile开启模式才有效,防止网路阻塞,积极的减少网络报文段的数量。将响应头和正文的开始部分一起发送,而不一个接一个的发送。
sendfile on;
tcp_nopush on;
# 长连接的超时时长,默认为65s
#keepalive_timeout 0;
keepalive_timeout 65;
# gzip 压缩模块提供了压缩文件内容的功能,用户请求的内容在发送到客户端之前,Nginx 服务器会根据一些具体的策略实施压缩,以节约网站出口带宽,同时加快数据传输效率,来提升用户访问体验
gzip on;
# server 服务器级别,每个server类似于httpd中的一个<VirtualHost>
# listen 指定监听的端口或地址
# server_name 主机名称
server {
listen 80;
server_name localhost;
#charset koi8-r;
access_log logs/host.access.log main;
# 默认网站根目录为/usr/local/nginx/html
# index file; 默认主页面
location / {
root html;
index index.php index.html index.htm;
}
#开启状态界面
location /status {
stub_status on;
}
# error_page 根据http响应状态码来指明特用的错误页面
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
# 做PHP反向代理时去掉注释
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
# location模块是Nginx的一个定位模块
# fastcgi_pass指定了php-fpm模块的IP地址和端口号
# fastcgi_param参数指定了读取php文件的位置
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $Document_Root$fastcgi_script_name;
include fastcgi_params;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
# 另一个虚拟主机使用基于IP、名称和端口的组合配置
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
# 配置 https时,取消注释
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
#配置服务启动脚本
[root@lnmp ~]# vim /usr/lib/systemd/system/nginx.service
[Unit]
Description=OpenSSH server daemon
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecStop=/usr/local/nginx/sbin/nginx -s stop
ExecReload=/usr/local/nginx/sbin/nginx -s reload
[Install]
WantedBy=multi-user.target
[root@LNMP ]# systemctl daemon-reload //重新加载
[root@LNMP ]# systemctl start nginx
[root@LNMP ]# systemctl enable nginx
Created symlink /etc/systemd/system/multi-user.target.wants/nginx.service → /usr/lib/systemd/system/nginx.service.
[root@LNMP conf]# systemctl status nginx
● nginx.service - OpenSSH server daemon
Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
Active: active (running) since Wed 2023-10-25 15:29:50 CST; 13s ago
Main PID: 24173 (nginx)
Tasks: 5 (limit: 24817)
2. 安装MySQL
#创建MySQL服务的用户和组
[root@LNMP ~]# useradd -r -M -s /sbin/nologin mysql
#下载安装包MySQL
[root@LNMP ~]# cd /usr/src
[root@LNMP src]# wget https://downloads.mysql.com/archives/get/p/23/file/mysql-8.0.33-linux-glibc2.28-x86_64.tar.gz
[root@LNMP src]# tar xf mysql-8.0.33-linux-glibc2.28-x86_64.tar.gz -C /usr/local
[root@LNMP src]# mv /usr/local/mysql-8.0.33-linux-glibc2.28-x86_64 /usr/local/mysql
#修改目录/usr/local/mysql的属主属组
[root@LNMP ~]# chown -R mysql.mysql /usr/local/mysql
#添加环境变量
[root@LNMP ~]# echo 'export PATH=/usr/local/mysql/bin:$PATH' >> /etc/profile.d/mysql.sh
[root@LNMP ~]# source /etc/profile.d/mysql.sh
#安装后配置 include lib
[root@LNMP ~]# cd /usr/local/mysql
[root@LNMP mysql]# ls
bin docs include lib LICENSE man README share support-files
[root@LNMP mysql]# ln -sv /usr/local/mysql/include/ /usr/local/include/mysql
[root@LNMP mysql]# echo '/usr/local/mysql/lib' > /etc/ld.so.conf.d/mysql.conf
[root@LNMP mysql]# ldconfig
#建立数据存放目录
[root@LNMP ~]# mkdir /opt/data
[root@LNMP ~]# chown -R mysql.mysql /opt/data/
[root@LNMP ~]# ll /opt/
total 0
drwxr-xr-x 2 mysql mysql 6 Oct 25 16:11 data
#初始化数据库,密码为空
[root@LNMP ~]# /usr/local/mysql/bin/mysqld --initialize-insecure --datadir=/opt/data/ --user=mysql
#生成配置文件
[root@LNMP ~]# 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
character_set_server=utf8
EOF
#配置服务启动脚本
[root@LNMP ~]# sed -ri 's#^(basedir=).*#\1/usr/local/mysql#g' /usr/local/mysql/support-files/mysql.server
[root@LNMP ~]# sed -ri 's#^(datadir=).*#\1/opt/data#g' /usr/local/mysql/support-files/mysql.server
[root@LNMP ~]# cat > /usr/lib/systemd/system/mysqld.service <<EOF
[Unit]
Description=mysqld 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
EOF
# 刷新配置
[root@LNMP ~]# systemctl daemon-reload
#启动MySQL
[root@LNMP ~]# systemctl start mysqld
[root@LNMP ~]# systemctl enable mysqld
[root@LNMP ~]# systemctl status mysqld
● mysqld.service - mysqld server daemon
Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
Active: active (running) since Wed 2023-10-25 16:13:42 CST; 10s ago
Main PID: 1489 (mysqld_safe)
Tasks: 39 (limit: 24817)
Memory: 377.2M
CGroup: /system.slice/mysqld.service
├─1489 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/opt/data --pid-file=/opt/data/mysql.pid
└─1689 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/opt/data --plugin-dir=/usr/local/>
Oct 25 16:13:38 LNMP systemd[1]: Starting mysqld server daemon...
Oct 25 16:13:38 LNMP mysql.server[1476]: Starting MySQL.Logging to '/opt/data/LNMP.err'.
Oct 25 16:13:42 LNMP mysql.server[1476]: ... SUCCESS!
Oct 25 16:13:42 LNMP systemd[1]: Started mysqld server daemon.
#修改密码
[root@LNMP ~]# mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.33 MySQL Community Server - GPL
Copyright (c) 2000, 2023, 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> ALTER USER 'root'@'localhost' IDENTIFIED BY '12345678';
Query OK, 0 rows affected (0.00 sec)
mysql> quit
Bye
验证
[root@LNMP ~]# mysql -uroot -p12345678
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 9
Server version: 8.0.33 MySQL Community Server - GPL
Copyright (c) 2000, 2023, 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> quit
Bye
3. 安装PHP
#安装依赖包
[root@LNMP ~]# cd /usr/src/
[root@LNMP src]# yum -y install libxml2-devel sqlite-devel openssl-devel libxml2-devel bzip2-devel libcurl-devel readline-devel libpng-devel libjpeg-turbo-devel freetype-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
#./configure 配置相关的选项,并生成Makefile
[root@LNMP src]# wget https://www.php.net/distributions/php-8.2.9.tar.xz
[root@LNMP src]# tar xf php-8.2.9.tar.xz
[root@LNMP src]# cd php-8.2.9
[root@LNMP php-8.2.9]# ./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@LNMP php-8.2.9]# make && make install
#安装后配置 include lib
[root@LNMP ~]# cd /usr/local/php8
[root@LNMP php8]# ls
bin etc include lib php sbin var
[root@LNMP php8]# ln -s /usr/local/php8/include/ /usr/include/php8
[root@LNMP php8]# echo '/usr/local/php8/lib' > /etc/ld.so.conf.d/php8.conf
[root@LNMP php8]# ldconfig
#配置环境变量
[root@LNMP php8]# echo 'export PATH=/usr/local/php8/bin:$PATH' >> /etc/profile.d/php.sh
[root@LNMP php8]# echo 'export PATH=/usr/local/php8/sbin:$PATH' >> /etc/profile.d/php.sh
[root@LNMP php8]# source /etc/profile.d/php.sh
#配置php-fpm
[root@LNMP ~]# cd /usr/src/php-8.2.9
[root@LNMP php-8.2.9]# cp php.ini-production /etc/php.ini
[root@LNMP php-8.2.9]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
[root@LNMP php-8.2.9]# chmod +x /etc/rc.d/init.d/php-fpm
[root@LNMP php-8.2.9]# cp /usr/local/php8/etc/php-fpm.conf.default /usr/local/php8/etc/php-fpm.conf
[root@LNMP php-8.2.9]# cp /usr/local/php8/etc/php-fpm.d/www.conf.default /usr/local/php8/etc/php-fpm.d/www.conf
#配置服务启动脚本
[root@LNMP php-8.2.9]# cat > /usr/lib/systemd/system/php-fpm.service <<EOF
[Unit]
Description=php-fpm server daemon
After=network.target
[Service]
Type=forking
ExecStart=/etc/init.d/php-fpm start
ExecStop=/etc/init.d/php-fpm stop
ExecReload=/bin/kill -HUP $MAINPID
[Install]
WantedBy=multi-user.target
EOF
#启动php-fpm
[root@LNMP ~]# systemctl daemon-reload
[root@LNMP ~]# systemctl start php-fpm
[root@LNMP ~]# systemctl status php-fpm
● php-fpm.service - php-fpm server daemon
Loaded: loaded (/usr/lib/systemd/system/php-fpm.service; disabled; vendor preset: disabled)
Active: active (running) since Wed 2023-10-25 16:41:50 CST; 4s ago
Process: 139707 ExecStart=/etc/init.d/php-fpm start (code=exited, status=0/SUCCESS)
Main PID: 139709 (php-fpm)
Tasks: 3 (limit: 24817)
Memory: 11.9M
CGroup: /system.slice/php-fpm.service
├─139709 php-fpm: master process (/usr/local/php8/etc/php-fpm.conf)
├─139710 php-fpm: pool www
└─139711 php-fpm: pool www
Oct 25 16:41:50 LNMP systemd[1]: Starting php-fpm server daemon...
Oct 25 16:41:50 LNMP php-fpm[139707]: Starting php-fpm done
Oct 25 16:41:50 LNMP systemd[1]: Started php-fpm server daemon.
#创建php的web访问页面
[root@LNMP ~]# cat > /usr/local/nginx/html/index.php <<EOF
> <?php
> phpinfo();
> ?>
> EOF
4.访问测试
[root@LNMP ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
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 70 *:33060 *:*
LISTEN 0 128 *:3306 *:*
LISTEN 0 128 [::]:22 [::]:*
0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 70 *:33060 :
LISTEN 0 128 :3306 :
LISTEN 0 128 [::]:22 [::]:
``