部署LNMP架构
下载安装包
[root@lnmp ~]# wget http://nginx.org/download/nginx-1.20.1.tar.gz
[root@lnmp ~]# wget https://downloads.mysql.com/archives/get/p/23/file/mysql-5.7.35-linux-glibc2.12-x86_64.tar.gz
[root@lnmp ~]# wget https://www.php.net/distributions/php-8.0.10.tar.gz
关闭防火墙与SELINUX
[root@lnmp ~]# systemctl disable --now firewalld
[root@lnmp ~]# setenforce 0
[root@lnmp ~]# cat /etc/sysconfig/selinux
7 SELINUX=disabled
[root@lnmp ~]# reboot
解压安装包
[root@lnmp local]# ls
bin etc games include lib lib64 libexec mysql-5.7.35-linux-glibc2.12-x86_64.tar.gz nginx-1.20.1.tar.gz php-8.0.10.tar.gz sbin share src tomcat
[root@lnmp local]# tar xf mysql-5.7.35-linux-glibc2.12-x86_64.tar.gz
[root@lnmp local]# tar xf nginx-1.20.1.tar.gz
[root@lnmp local]# tar xf php-8.0.10.tar.gz
编译nginx
//创建用户
[root@lnmp ~]# useradd -r -M -s /sbin/nologin nginx
//安装依赖
[root@lnmp local]# yum -y install pcre-devel openssl openssl-devel gd-devel gcc gcc-c++
[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/local/nginx-1.20.1/
[root@lnmp 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
[root@lnmp nginx-1.20.1]# make && make install
//设置环境变量
[root@lnmp nginx-1.20.1]# echo 'export PATH=/usr/local/nginx/sbin:$PATH' > /etc/profile.d/nginx.sh
[root@lnmp nginx-1.20.1]# . /etc/profile.d/nginx.sh
//启动nginx
[root@lnmp nginx-1.20.1]# nginx
[root@lnmp nginx-1.20.1]# ss -anlt
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:80 *:*
LISTEN 0 128 *:22 *:*
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 128 :::22 :::*
LISTEN 0 100 ::1:25 :::*
可以访问
编译mysql
//创建用户
[root@lnmp ~]# useradd -r -M -s /sbin/nologin mysql
//下载依赖包
[root@lnmp ~]# yum -y install ncurses-devel openssl-devel openssl cmake mariadb-devel ncurses-compat-libs
//软链接
[root@lnmp ~]# ln -sv mysql-5.7.35-linux-glibc2.12-x86_64/ mysql
"mysql" -> "mysql-5.7.35-linux-glibc2.12-x86_64/"
//修改mysql的属主
[root@lnmp local]# chown -R mysql.mysql /usr/local/mysql
[root@lnmp local]# ll
lrwxrwxrwx 1 mysql mysql 36 10月 27 11:26 mysql -> mysql-5.7.35-linux-glibc2.12-x86_64/
////添加环境变量
[root@lnmp local]# echo 'export PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
[root@lnmp local]# . /etc/profile.d/mysql.sh
[root@lnmp local]# echo $PATH
/usr/local/mysql/bin:/usr/local/nginx/sbin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
//创建数据存放目录
[root@lnmp local]# mkdir /opt/data
[root@lnmp local]# chown -R mysql.mysql /opt/data/
//初始化
[root@lnmp local]# /usr/local/mysql/bin/mysqld --initialize --user=mysql --datadir=/opt/data/
2021-10-27T03:34:41.828146Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2021-10-27T03:34:42.048489Z 0 [Warning] InnoDB: New log files created, LSN=45790
2021-10-27T03:34:42.089923Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2021-10-27T03:34:42.151286Z 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: d257c91b-36d6-11ec-8604-000c29057182.
2021-10-27T03:34:42.152827Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2021-10-27T03:34:42.889509Z 0 [Warning] A deprecated TLS version TLSv1 is enabled. Please use TLSv1.2 or higher.
2021-10-27T03:34:42.889524Z 0 [Warning] A deprecated TLS version TLSv1.1 is enabled. Please use TLSv1.2 or higher.
2021-10-27T03:34:42.890141Z 0 [Warning] CA certificate ca.pem is self signed.
2021-10-27T03:34:43.216315Z 1 [Note] A temporary password is generated for root@localhost: rx*t>>UsK9z.
//配置mysql
[root@lnmp local]# ln -sv /usr/local/mysql/include/ /usr/local/include/mysql
"/usr/local/include/mysql" -> "/usr/local/mysql/include/"
[root@lnmp local]# echo '/usr/local/mysql/lib' > /etc/ld.so.conf.d/mysql.conf
[root@lnmp local]# ldconfig
//生成配置文件
[root@lnmp local]# 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
//配置服务启动脚本
[root@lnmp local]# cp -a /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
[root@lnmp local]# sed -ri 's#^(basedir=).*#\1/usr/local/mysql#g' /etc/init.d/mysqld
[root@lnmp local]# sed -ri 's#^(datadir=).*#\1/opt/data#g' /etc/init.d/mysqld
//启动mysql并修改密码
[root@lnmp local]# service mysqld start
Starting MySQL.Logging to '/opt/data/lnmp.err'.
SUCCESS!
[root@lnmp local]# mysql -uroot -p
Enter password: rx*t>>UsK9z.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.35
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('1');
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> exit
Bye
编译php
//下载依赖包
[root@lnmp local]# 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 readline readline-devel libxslt libxslt-devel
//安装oniguruma-devel
[root@lnmp local]# 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
//源码安装
[root@lnmp local]# wget https://github.com/kkos/oniguruma/archive/v6.9.4.tar.gz -O oniguruma-6.9.4.tar.gz
[root@lnmp local]# tar -zxf oniguruma-6.9.4.tar.gz
[root@lnmp local]# cd oniguruma-6.9.4
[root@lnmp local]# ./autogen.sh && ./configure --prefix=/usr
[root@lnmp local]# make && make install
[root@lnmp local]# cd /usr/local/php-8.0.10/
[root@lnmp 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
[root@lnmp php-8.0.10]# make && make install
//安装后配置
[root@lnmp php-8.0.10]# echo 'export PATH=/usr/local/php8/bin:$PATH' > /etc/profile.d/php.sh
[root@lnmp php-8.0.10]# source /etc/profile.d/php.sh
[root@lnmp php-8.0.10]# which php
/usr/local/php8/bin/php
[root@lnmp php-8.0.10]# php -v
PHP 8.0.10 (cli) (built: Oct 26 2021 03:20:37) ( NTS )
Copyright (c) The PHP Group
Zend Engine v4.0.10, Copyright (c) Zend Technologies
//配置php-fpm
[root@lnmp php-8.0.10]# cp -f /usr/local/php-8.0.10/php.ini-production /etc/php.ini
[root@lnmp php-8.0.10]# cp /usr/local/php-8.0.10/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm -f
[root@lnmp php-8.0.10]# chmod +x /etc/init.d/php-fpm
[root@lnmp php-8.0.10]# cp -f /usr/local/php8/etc/php-fpm.conf.default /usr/local/php8/etc/php-fpm.conf
[root@lnmp php-8.0.10]# cp -f /usr/local/php8/etc/php-fpm.d/www.conf.default /usr/local/php8/etc/php-fpm.d/www.conf
//启动
[root@lnmp php-8.0.10]# service php-fpm start
Starting php-fpm done
[root@lnmp php-8.0.10]# 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 0.0.0.0:80 0.0.0.0:*
LISTEN 0 128 [::]:22 [::]:*
LISTEN 0 80 *:3306 *:*
创建web访问界面
[root@lnmp ~]# cat /usr/local/nginx/html/index.php
<?php
phpinfo();
?>
修改nginx配置文件
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
41 #access_log logs/host.access.log main;
42
43 location / {
44 root html;
45 index index.html index.php index.htm; //在中间添加index.php
46 }
47
48 #error_page 404 /404.html;
63 # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
64 #
65 location ~ \.php$ {
66 root html;
67 fastcgi_pass 127.0.0.1:9000;
68 fastcgi_index index.php;
69 fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name; //将/script改为/usr/local/nginx/html
70 include fastcgi_params;
71 }
重启服务并访问
[root@lnmp php-8.0.10]# nginx -s stop;nginx
[root@lnmp php-8.0.10]# ss -anlt
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 80 *:3306 *:*
LISTEN 0 128 [::]:22 [::]:*
访问:
评论区