部署lnmp架構(gòu)

lnmp簡(jiǎn)介

LNMP是指一組通常一起使用來(lái)運(yùn)行動(dòng)態(tài)網(wǎng)站或者服務(wù)器的自由軟件名稱首字母縮寫。L指Linux,N指Nginx,M一般指MySQL,也可以指MariaDB,P一般指PHP,也可以指Perl或Python。

LNMP代表的就是:Linux系統(tǒng)下Nginx+MySQL+PHP這種網(wǎng)站服務(wù)器架構(gòu)

linux是Unix計(jì)算機(jī)操作系統(tǒng)的統(tǒng)稱,是目前最流行的免費(fèi)的操作系統(tǒng),代表有Debian、centos、ubuntu、fedora、gentoo等。

Nginx是一個(gè)高性能的HTTP和反向代理服務(wù)器,也是一個(gè)IMAP/POP3/SMTP代理服務(wù)器。

Mysql是一個(gè)小型關(guān)系型數(shù)據(jù)庫(kù)管理系統(tǒng)

PHP是一種在服務(wù)器端執(zhí)行的嵌入HTML文檔的腳本語(yǔ)言

這四種軟件均為免費(fèi)開源軟件,組合到一起,成為一個(gè)免費(fèi)、高效、擴(kuò)展性強(qiáng)的網(wǎng)站服務(wù)系統(tǒng)。

環(huán)境說(shuō)明

系統(tǒng) IP
Redhat8.2 192.168.182.143

安裝nginx

下載nginx的源碼包

[root@localhost ~]# wget http://nginx.org/download/nginx-1.20.1.tar.gz

解決nginx的依賴

yum -y install pcre-devel pcre gcc gcc-c++ openssl-devel zlib zlib-devel make vim wget openssl openssl-devel gd-devel

解壓nginx的rpm包

[root@localhost ~]# tar -xzf nginx-1.20.1.tar.gz -C /usr/local

創(chuàng)建系統(tǒng)用戶nginx

[root@localhost ~]# useradd -r -M -s /sbin/nologin nginx

創(chuàng)建日志存放目錄

[root@localhost ~]# mkdir -p /var/log/nginx
[root@localhost ~]# chown -R nginx.nginx /var/log/nginx

編譯安裝

[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

[root@localhost nginx-1.20.1]# make && make install

安裝mysql

  1. 安裝依賴包
[root@localhost opt]# yum -y install gcc gcc-c++ make zlib zlib-devel pcre pcre-devel openssl openssl-devel ncurses-compat-libs perl ncurses-devel cmake
  1. 下載MySQL的tar包
[root@localhost opt]# wget https://downloads.mysql.com/archives/get/p/23/file/mysql-5.7.34-linux-glibc2.12-x86_64.tar.gz -C packages

[root@localhost opt]# tar xf packages/mysql-5.7.34-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
  1. 創(chuàng)建MySQL用戶和組
[root@localhost opt]# useradd  -r -M -s /sbin/nologin mysql
  1. 為了后面操作方便將mysql-5.7.32-linux-glibc2.12-x86_64目錄改名或做成軟連接都可以
[root@localhost local]# mv mysql-5.7.34-linux-glibc2.12-x86_64/ mysql
  1. MySQL的屬主和屬組
[root@localhost local]# pwd
/usr/local
[root@localhost local]# chown -R mysql.mysql mysql
  1. 添加環(huán)境變量
[root@localhost local]# echo 'export PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
[root@localhost local]# source /etc/profile.d/mysql.sh
  1. 配置MySQL數(shù)據(jù)庫(kù)的lib庫(kù)文件
[root@localhost local]# vim /etc/ld.so.conf.d/mysql.conf
[root@localhost local]# cat /etc/ld.so.conf.d/mysql.conf
/usr/local/mysql/lib
[root@localhost local]# ldconfig
  1. 創(chuàng)建MySQL的數(shù)據(jù)目錄,并修改其屬主和屬組
[root@localhost local]# mkdir -p /opt/data
[root@localhost local]# chown -R mysql.mysql /opt/data/
  1. 初始化數(shù)據(jù)庫(kù)
[root@localhost ~]# mysqld --initialize-insecure --user mysql --datadir /opt/data/
  1. 生成MySQL配置文件
[root@localhost local]# 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
  1. 配置服務(wù)啟動(dòng)的腳本
[root@localhost ~]# vim /usr/local/mysql/support-files/mysql.server
找到basedir和datadir然后添加下面的內(nèi)容
basedir=/usr/local/mysql
datadir=/opt/data
  1. 配置系統(tǒng)服務(wù)使用systemctl來(lái)管理MySQL
[root@localhost ~]# cp /usr/lib/systemd/system/sshd.service /usr/lib/systemd/system/mysqld.service
[root@localhost ~]# cat /usr/lib/systemd/system/mysqld.service
[Unit]
Description=mysql 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

[root@localhost ~]# systemctl daemon-reload 
[root@localhost ~]# systemctl enable --now mysqld.service
設(shè)置數(shù)據(jù)庫(kù)密碼
[root@localhost local]# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
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> set password = password("1");
Query OK, 0 rows affected, 1 warning (0.00 sec)

安裝php

需要網(wǎng)絡(luò)倉(cāng)庫(kù):curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-8.repo

[root@localhost local]# yum -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 http://mirror.centos.org/centos/8-stream/PowerTools/x86_64/os/Packages/oniguruma-devel-6.8.2-2.el8.x86_64.rpm

下載php的tar包并解壓
可以去官網(wǎng)下載:https://www.php.net/downloads

[root@localhost opt]# https://www.php.net/distributions/php-8.0.11.tar.gz

[root@localhost opt]# tar xf packages/php-8.0.11.tar.xz -C /usr/local/

編譯安裝php

[root@localhost php-8.0.11]# ./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@localhost php-8.0.11]# make && make install

配置環(huán)境變量

[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 php-8.0.11]# cp php.ini-production /etc/php.ini
cp:是否覆蓋'/etc/php.ini'? y

[root@localhost fpm]# pwd
/usr/local/php-8.0.11/sapi/fpm
[root@localhost fpm]# cp init.d.php-fpm /etc/init.d/php-fpm

[root@localhost php-8.0.11]# chmod +x /etc/rc.d/init.d/php-fpm 

[root@localhost php-8.0.11]# cp /usr/local/php8/etc/php-fpm.conf.default /usr/local/php8/etc/php-fpm.conf
[root@localhost php-8.0.11]# cp /usr/local/php8/etc/php-fpm.d/www.conf.default /usr/local/php8/etc/php-fpm.d/www.conf
[root@localhost php-8.0.11]# service php-fpm start
Starting php-fpm  done

配置系統(tǒng)服務(wù)使用systemctl來(lái)管理PHP

[root@localhost ~]# cp /usr/lib/systemd/system/mysqld.service /usr/lib/systemd/system/php-fpm.service
[root@localhost ~]# vim /usr/lib/systemd/system/php-fpm.service
[root@localhost ~]# cat /usr/lib/systemd/system/php-fpm.service
[Unit]
Description=php 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

[root@localhost ~]# pkill php-fpm 
[root@localhost ~]# systemctl enable --now php-fpm.service 
Synchronizing state of php-fpm.service with SysV service script with /usr/lib/systemd/systemd-sysv-install.
Executing: /usr/lib/systemd/systemd-sysv-install enable php-fpm

創(chuàng)建php訪問(wèn)界面


[root@localhost html]# cat index.php
<?php
    phpinfo();
?>
[root@localhost html]# pwd
/usr/local/nginx/html

修改nginx配置文件

[root@localhost conf]# vim nginx.conf
43         location / {
 44             root   html;
 45             index index.php index.html index.htm;
 46         }

[root@localhost conf]# vim nginx.conf
[root@localhost conf]# pwd
/usr/local/nginx/conf
 65         location ~ \.php$ {
 66             root           html;
 67             fastcgi_pass   127.0.0.1:9000;
 68             fastcgi_index  index.php;
 69             fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;  //這個(gè)地方寫你你PHP頁(yè)面的路徑,可寫絕對(duì)路徑,但是最好寫變量
 70             include        fastcgi_params;
 71         }
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容