lnmp部署
lnmp部署
环境说明
系统平台 | IP | 需要安装的服务 |
---|---|---|
centos8 | 192.168.118.130 | nginx-1.20.2 |
centos8 | 192.168.118.131 | mysql-5.7.34 |
centos8 | 192.168.118.132 | php-8.1.11 |
1.nginx安装
安装准备
#修改主机名
[root@localhost ~]# hostnamectl set-hostname nginx
[root@localhost ~]# bash
#关闭防火墙和selinux
[root@nginx ~]# setenforce 0
[root@nginx ~]# sed -ri 's/^(SELINUX=).*/\1disabled/g' /etc/selinux/config
[root@nginx ~]# systemctl disable --now firewalld.service
#创建nginx用户
[root@nginx ~]# useradd -rMs /sbin/nologin nginx
#安装所需要的依赖包
[root@nginx ~]# dnf -y install pcre-devel openssl openssl-devel gd-devel gcc gcc-c++ make wget vim
#下载nginx源码包,并解压
[root@nginx ~]# wget http://nginx.org/download/nginx-1.20.2.tar.gz
[root@nginx ~]# tar -xf nginx-1.20.2.tar.gz
[root@nginx ~]# cd nginx-1.20.2/
[root@nginx nginx-1.20.2]#
开始安装
#配置
[root@nginx 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
#编译安装
[root@nginx nginx-1.20.2]# make -j $(grep 'processor' /proc/cpuinfo | wc -l)
[root@nginx nginx-1.20.2]# make install
#安装成功
[root@nginx nginx-1.20.2]# cd /usr/local/nginx/
[root@nginx nginx]# ls
conf html logs sbin
配置文件
#配置环境变量
[root@nginx ~]# echo "export PATH=$PATH:/usr/local/nginx/sbin" > /etc/profile.d/nginx.sh
[root@nginx ~]# source /etc/profile.d/nginx.sh
#编写service文件
[root@nginx ~]# vim /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@nginx ~]# systemctl daemon-reload
启动服务,开机自启
[root@nginx ~]# systemctl enable --now nginx.service
Created symlink /etc/systemd/system/multi-user.target.wants/nginx.service → /usr/lib/systemd/system/nginx.service.
[root@nginx ~]# systemctl status nginx.service
● nginx.service - nginx server daemon
Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disable>
Active: active (running) since Tue 2022-10-11 15:42:27 CST; 36s ago
2.mysql安装
#修改主机名
[root@localhost ~]# hostnamectl set-hostname mysql
[root@localhost ~]# bash
#关闭防火墙和selinux
[root@mysql ~]# setenforce 0
[root@mysql ~]# sed -ri 's/^(SELINUX=).*/\1disabled/g' /etc/selinux/config
[root@mysql ~]# systemctl disable --now firewalld.service
#创建mysql用户
[root@mysql ~]# useradd -rMs /sbin/nologin mysql
#安装所需要的依赖包
[root@mysql ~]# dnf -y install ncurses-devel openssl-devel openssl cmake mariadb-devel ncurses-compat-libs
#下载mysql源码包,并解压
[root@mysql ~]# wget https://downloads.mysql.com/archives/get/p/23/file/mysql-5.7.34-linux-glibc2.12-x86_64.tar
[root@mysql ~]# tar xf mysql-5.7.34-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
#更改属主属组
[root@mysql ~]# cd /usr/local/
[root@mysql local]# mv mysql-5.7.34-linux-glibc2.12-x86_64/ mysql
[root@mysql local]# chown -R mysql.mysql mys
#头文件
[root@mysql local]# cd mysql/
[root@mysql mysql]# ls
LICENSE README bin docs include lib man share support-files
[root@mysql mysql]# ln -sv /usr/local/mysql/include/ /usr/include/mysql/
'/usr/include/mysql/include' -> '/usr/local/mysql/include/'
#库文件
[root@mysql mysql]# echo '/usr/local/mysql/lib' > /etc/ld.so.conf.d/mysql.conf
[root@mysql mysql]# ldconfig
#帮助文档
[root@mysql mysql]# 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@mysql ~]# echo 'export PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
[root@mysql ~]# source /etc/profile.d/mysql.sh
#创建数据存放目录
[root@mysql ~]# mkdir /opt/data
[root@mysql ~]# chown -R mysql.mysql /opt/data/
配置mysql
#初始化数据库
[root@mysql ~]# mysqld --initialize --user mysql --datadir /opt/data/
2022-10-11T08:23:11.162927Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2022-10-11T08:23:11.306836Z 0 [Warning] InnoDB: New log files created, LSN=45790
2022-10-11T08:23:11.336846Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2022-10-11T08:23:11.399613Z 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: f1a03ac7-493d-11ed-b17e-000c290801b6.
2022-10-11T08:23:11.400267Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2022-10-11T08:23:11.789141Z 0 [Warning] CA certificate ca.pem is self signed.
2022-10-11T08:23:11.906371Z 1 [Note] A temporary password is generated for root@localhost: Dd1?:lsqyr3s
#保存密码
[root@mysql ~]# echo 'Dd1?:lsqyr3s' > mysqlpass
#查询到还有mariadb的软件包,将他们卸载
[root@mysql ~]# rpm -qa |grep mariadb
mariadb-connector-c-devel-3.1.11-2.el8_3.x86_64
mariadb-connector-c-config-3.1.11-2.el8_3.noarch
mariadb-devel-10.3.28-1.module_el8.3.0+757+d382997d.x86_64
mariadb-connector-c-3.1.11-2.el8_3.x86_64
[root@mysql ~]# dnf -y remove mariadb*
#生成配置文件
[root@mysql ~]# 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@mysql ~]# vim /usr/lib/systemd/system/mysqld.service
[Unit]
Description=mysql server daemon
After=network.target sshd-keygen.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@mysql ~]# systemctl daemon-reload
#开启mysql并开机自启
[root@mysql ~]# systemctl enable --now mysqld.service
[root@mysql ~]# 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 *:*
#进入mysql,更改密码
[root@mysql ~]# cat mysqlpass
Dd1?:lsqyr3s
[root@mysql ~]# mysql -uroot -p'Dd1?:lsqyr3s'
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('123456');
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> exit
Bye
3.php安装
#修改主机名
[root@localhost ~]# hostnamectl set-hostname php
[root@localhost ~]# bash
#关闭防火墙和selinux
[root@php ~]# setenforce 0
[root@php ~]# sed -ri 's/^(SELINUX=).*/\1disabled/g' /etc/selinux/config
[root@php ~]# systemctl disable --now firewalld.service
[root@php ~]# reboot
#配置yum源
[root@php yum.repos.d]# wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo
[root@php yum.repos.d]# sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo
#安装所需要的依赖包
[root@php ~]# dnf -y install epel-release
[root@php ~]# dnf -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 libmcrypt libmcrypt-devel readline readline-devel libxslt libxslt-devel mhash mhash-devel php-mysqlnd libsqlite3x-devel libzip-devel wget gcc gcc-c++ make
[root@php ~]# dnf -y install http://mirror.centos.org/centos/8-stream/PowerTools/x86_64/os/Packages/oniguruma-devel-6.8.2-2.el8.x86_64.rpm
#下载mysql源码包,并解压
[root@php ~]# wget https://www.php.net/distributions/php-8.1.11.tar.gz
[root@php ~]# tar xf php-8.1.11.tar.gz
#编译安装
[root@php ~]# cd php-8.1.11/
[root@php php-8.1.11]# ./configure --prefix=/usr/local/php \
--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
[root@php php-8.1.11]# make && make install
[root@php php-8.1.11]# cd /usr/local/php/
[root@php php]# ls
bin etc include lib php sbin var
#配置文件
[root@php php]# cd etc/
[root@php etc]# ls
pear.conf php-fpm.conf.default php-fpm.d
[root@php etc]# cp php-fpm.conf.default php-fpm.conf
[root@php etc]# cd php-fpm.d/
[root@php php-fpm.d]# ls
www.conf.default
[root@php php-fpm.d]# cp www.conf.default www.conf
#头文件
[root@php ~]# ln -sv /usr/local/php /usr/include/php
'/usr/include/php' -> '/usr/local/php'
#库文件
[root@php ~]# echo "/usr/local/php/lib" > /etc/ld.so.conf.d/php.conf
[root@php ~]# ldconfig
#编写service文件
[root@php ~]# cat > /usr/lib/systemd/system/php.service << EOF
[Unit]
Description=php server daemon
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/php/sbin/php-fpm
ExecStop=ps -ef |grep php |grep -v grep|awk '{print$2}'|xargs kill
ExecReload=/bin/kill -HUP $MAINPID
[Install]
WantedBy=multi-user.target
EOF
[root@php ~]# systemctl daemon-reload
#开启php服务并开机自启
[root@php ~]# systemctl enable --now php.service
Created symlink /etc/systemd/system/multi-user.target.wants/php.service → /usr/lib/systemd/system/php.service.
[root@php ~]# 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 127.0.0.1:9000 0.0.0.0:*
LISTEN 0 128 [::]:22 [::]:*
4.lnmp服务完成配置后配置
nginx端配置
#配置php网页
[root@nginx ~]# vim /usr/local/nginx/html/index.php
<?php
phpinfo();
?>
#修改配置文件
[root@nginx ~]# vim /usr/local/nginx/conf/nginx.conf
...
location / {
root html;
index index.php index.html index.htm; #查找index,在此行添加index.php
}
...
#65-71行,取消所有注释
location ~ \.php$ {
root /var/www/html; #指向php端index.php文件位置
fastcgi_pass 192.168.118.132:9000; #php服务器ip地址
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; #将/scripts更改为$document_root
include fastcgi_params;
}
#重启服务
[root@nginx ~]# systemctl restart nginx.service
php端配置
#在php端上配置网站
[root@php ~]# mkdir -p /var/www/html
[root@php ~]# vim /var/www/html/index.php
<?php
phpinfo();
?>
#修改配置文件
[root@php ~]# vim /usr/local/php/etc/php-fpm.d/www.conf
...
listen = 192.168.118.132:9000 #修改为php本机IP
...
;listen.allowed_clients = 192.168.118.130 #允许指定ip访问(nginx服务端IP)
...
#重启服务生效配置
[root@php ~]# systemctl restart php.service
[root@php ~]# 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 192.168.118.132:9000 0.0.0.0:*
LISTEN 0 128 [::]:22 [::]:*
浏览器访问