LNMP部署
目录
nginx官网下载包
mysql官网下载包
php官网下载包
lnmp简介
LNMP是指一组通常一起使用来运行动态网站或者服务器的自由软件名称首字母缩写。L指Linux,N指Nginx,M一般指MySQL,也可以指MariaDB,P一般指PHP,也可以指Perl或Python。
LNMP代表的就是:Linux系统下Nginx+MySQL+PHP这种网站服务器架构
linux是Unix计算机操作系统的统称,是目前最流行的免费的操作系统,代表有Debian、centos、ubuntu、fedora、gentoo等。
Nginx是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP代理服务器。
Mysql是一个小型关系型数据库管理系统
PHP是一种在服务器端执行的嵌入HTML文档的脚本语言
这四种软件均为免费开源软件,组合到一起,成为一个免费、高效、扩展性强的网站服务系统。
环境说明:
| 系统 | IP |
|---|---|
| centos8 | 192.168.170.135 |
LNMP部署(Nginx+MYSQL+PHP)
安装nginx
#关闭防火墙和selinux
[root@localhost ~]# systemctl disable --now firewalld.service
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 ~]# useradd -r -M -s /sbin/nologin nginx
[root@localhost ~]# dnf -y install pcre-devel openssl openssl-devel gd-devel gcc gcc-c++ make wget vim
[root@localhost ~]# dnf -y groups mark install 'Development Tools'
#创建日志存放目录
[root@localhost ~]# mkdir -p /var/log/nginx
[root@localhost ~]# chown -R nginx.nginx /var/log/nginx
#编译安装
[root@localhost src]# tar xf nginx-1.20.2.tar.gz
[root@localhost src]# cd nginx-1.20.2/
[root@localhost nginx-1.22.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@localhost src]# make -j $(grep 'processor' /proc/cpuinfo | wc -l) && make install
[root@localhost ~]# cd /usr/local/
[root@localhost local]# ls //安装完成后安装目录下会有nginx目录
bin etc games include lib lib64 libexec nginx sbin share src
配置nginx
#设置环境变量
[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 [::]:*
#配置service文件
[root@localhost ~]# cd /usr/lib/systemd/system/
[root@localhost system]# cp -r sshd.service nginx.service
[root@localhost system]# cat nginx.service
[Unit]
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
KillMode=process
Restart=on-failure
RestartSec=42s
[Install]
WantedBy=multi-user.target
[root@localhost system]# systemctl daemon-reload
[root@localhost ~]# systemctl enable --now nginx.service //设置开机自启
[root@localhost system]# systemctl status nginx.service
nginx.service
Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
Active: active (running) since Sun 2022-09-04 00:03:17 CST; 6s ago
安装mysql
#配置依赖环境
[root@localhost ~]# useradd -r -M -s /sbin/nologin mysql
[root@localhost ~]# yum install -y ncurses-compat-libs
#解压软件包
[root@localhost ~]# tar xf mysql-8.0.11-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
[root@localhost ~]# cd /usr/local/
[root@localhost local]# mv mysql-8.0.11-linux-glibc2.12-x86_64/ mysql
[root@localhost local]# ls
bin etc games include lib lib64 libexec mysql nginx sbin share src
[root@localhost local]# chown -R mysql.mysql mysql/
配置mysql
#软连接 lib库 man文档
[root@localhost ~]# ln -s /usr/local/mysql/include/ /usr/include/mysql
[root@localhost ~]# cat /etc/ld.so.conf.d/mysql.conf
/usr/local/mysql/lib
[root@localhost ~]# vim /etc/man_db.conf
MANDATORY_MANPATH /usr/local/mysql/man
[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 Sep 4 00:12 data
#格式化
[root@localhost ~]# mysqld --initialize --user=mysql --datadir=/opt/data/
2022-09-03T16:32:38.539715Z 0 [System] [MY-013169] [Server] /usr/local/mysql/bin/mysqld (mysqld 8.0.11) initializing of server in progress as process 298658
2022-09-03T16:32:40.684832Z 5 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: r6Dup-I_.:!T
2022-09-03T16:32:41.598157Z 0 [System] [MY-013170] [Server] /usr/local/mysql/bin/mysqld (mysqld 8.0.11) initializing of server has completed
#生成配置文件
[root@localhost ~]# cat /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
#修改mysql的root用户密码
[root@localhost ~]# mysql -uroot -p'r6Dup-I_.:!T'
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 8
Server version: 8.0.11
Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
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 '123';
Query OK, 0 rows affected (0.10 sec)
# 验证
[root@localhost ~]# mysql -uroot -p'123'
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.11 MySQL Community Server - GPL
Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
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
配置mysql的service文件
[root@localhost ~]# /usr/local/mysql/support-files/mysqld stop //关闭
Shutting down MySQL. SUCCESS!
[root@localhost ~]# cd /usr/lib/systemd/system
[root@localhost system]# cp sshd.service mysqld.service
[root@localhost system]# vim mysqld.service
[root@localhost system]# cat mysqld.service
[Unit]
Description=mysql server daemon
After=network.target sshd-keygen.target
[Service]
Type=forking
ExecStart=/usr/local/mysql/support-files/mysqld start
ExecStop=/usr/local/mysql/support-files/mysqld stop
ExecReload=/bin/kill -HUP $MAINPID
KillMode=process
Restart=on-failure
RestartSec=42s
[Install]
WantedBy=multi-user.target
[root@localhost system]# systemctl daemon-reload
[root@localhost ~]# systemctl enable --now mysqld.service //设置开机自启
[root@localhost system]# systemctl status mysqld.service
鈼� mysqld.service - mysql server daemon
Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
Active: active (running) since Sun 2022-09-04 00:42:42 CST; 5s ago
Process: 349534 ExecStart=/usr/local/mysql/support-files/mysqld start (code=exited, status=0/SUC>
Main PID: 349555 (mysqld_safe)
安装php
# 安装依赖包
[root@localhost ~]# yum -y install autoconf freetype gd libpng libpng-devel libjpeg libxml2 libxml2-devel zlib curl curl-devel net-snmp-devel libjpeg-devel php-ldap openldap-devel openldap-clients freetype-devel gmp-devel libzip libzip-devel sqlite-devel
# 安装oniguruma 依赖包
[root@localhost ~]# yum install autoconf automake libtool -y
[root@localhost ~]# wget https://github.com/kkos/oniguruma/archive/v6.9.4.tar.gz -O oniguruma-6.9.4.tar.gz
[root@localhost ~]# tar xf oniguruma-6.9.4.tar.gz && cd oniguruma-6.9.4
[root@localhost oniguruma-6.9.4]# ./autogen.sh && ./configure --prefix=/usr
Generating autotools files.
[root@localhost oniguruma-6.9.4]# make && make install
[root@localhost ~]# tar Jxf php-8.0.23.tar.xz
[root@localhost ~]# cd php-8.0.23/
[root@localhost php-8.0.23]# ./configure --prefix=/usr/local/php8 --with-config-file-path=/usr/local/php8/etc --enable-fpm --with-fpm-user=nginx --with-fpm-group=nginx --enable-mysqlnd --with-mysqli --with-pdo-mysql --enable-opcache --with-pcre-jit --enable-gd --with-jpeg --with-freetype --with-gettext --with-curl --with-openssl --enable-sockets --enable-mbstring --enable-xml --with-zip --with-zlib --with-snmp --with-mhash --enable-ftp --enable-bcmath --enable-soap --enable-shmop --enable-sysvsem --enable-pcntl --with-gmp
......
+--------------------------------------------------------------------+
| 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.23]# make && make install
配置php
#配置环境变量
[root@localhost ~]# echo 'export PATH=/usr/local/php8/bin:$PATH' > /etc/profile.d/php8.sh
[root@localhost ~]# source /etc/profile.d/php8.sh
[root@localhost ~]# which php
/usr/local/php8/bin/php
#配置php-fpm
[root@localhost php-8.0.23]# echo 'export PATH=/usr/local/php8/bin:$PATH' > /etc/profile.d/php8.sh
[root@localhost php-8.0.23]# source /etc/profile.d/php8.sh
[root@localhost php-8.0.23]# which php
/usr/local/php8/bin/php
[root@localhost ~]# cd php-8.0.23/
[root@localhost php-8.0.23]# cp php.ini-production /etc/php.ini
cp: overwrite '/etc/php.ini'? y
[root@localhost php-8.0.23]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
[root@localhost php-8.0.23]# chmod +x /etc/rc.d/init.d/php-fpm
[root@localhost php-8.0.23]# cp /usr/local/php8/etc/php-fpm.conf.default /usr/local/php8/etc/php-fpm.conf
[root@localhost php-8.0.23]# cp /usr/local/php8/etc/php-fpm.d/www.conf.default /usr/local/php8/etc/php-fpm.d/www.conf
#开启php
[root@localhost ~]# service php-fpm start
Starting php-fpm done
[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 127.0.0.1:9000 0.0.0.0:*
LISTEN 0 128 [::]:22 [::]:*
LISTEN 0 70 *:33060 *:*
LISTEN 0 128 *:3306 *:*
配置nginx.conf文件
[root@localhost conf]# pwd
/usr/local/nginx/conf
[root@localhost conf]# vim nginx.conf
#在43行到46行
location / {
root html;
index index.php index.html index.htm; //添加idnex.php
}
#在65行到71行
##取消注释
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; 添加这一行(脚本文件请求的路径)
include fastcgi_params;
}
#编写php测试文件
[root@localhost ~]# cd /usr/local/nginx/html/
[root@localhost html]# vim index.php
[root@localhost html]# cat index.php
<?php
phpinfo();
?>
#重启服务
[root@localhost ~]# systemctl restart nginx.service


浙公网安备 33010602011771号