banner
云野阁

云野阁

闲云野鹤,八方逍遥

LNMP部署

基礎環境#

系統環境:CentOS Stream 9

軟體環境:nginx-1.20.1、mysql-8.0.36(mariaDB)、php-8.0.30

部署過程#

方案一:yum 安裝#

步驟一:配置安全策略#

# 開放80端口
firewall-cmd --add-port=80/tcp --permanent
sudo firewall-cmd --reload && sudo firewall-cmd --list-all
# 關閉SELinux
setenforce 0
sed -i "s/SELINUX=enforcing/SELINUX=disabled/g" /etc/selinux/config

步驟二:安裝 nginx#

yum -y install nginx
# 查看nginx版本
nginx -v

步驟三:安裝並配置 mysql#

安裝 mysql

yum -y install mysql-server
#查看mysql版本
mysql -V
# 啟動並設置開機自啟
systemctl start mysqld
systemctl enable mysqld

配置 mysql

(1)查看 root 用戶的初始密碼

grep 'password' /var/log/mysql/mysqld.log

2024-05-08T09:37:31.765242Z 6 [Warning] [MY-010453] [Server] root@localhost is created with an empty password ! Please consider switching off the —initialize-insecure option.

(2)配置 MySQL 的安全性

mysql_secure_installation

Securing the MySQL server deployment.

Connecting to MySQL using a blank password.

VALIDATE PASSWORD COMPONENT can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD component?

設置 MySQL 的新密碼

Press y|Y for Yes, any other key for No: y

There are three levels of password validation policy:

LOW Length >= 8
MEDIUM Length >= 8, numeric, mixed case, and special characters
STRONG Length >= 8, numeric, mixed case, special characters and dictionary file

設置的密碼強度

Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 0
Please set the password for root here.

New password:

Re-enter new password:

Estimated strength of the password: 50
Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : y
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production
environment.

刪除匿名用戶

Remove anonymous users? (Press y|Y for Yes, any other key for No) : y
Success.

Normally, root should only be allowed to connect from
‘localhost’. This ensures that someone cannot guess at
the root password from the network.

禁止使用 root 用戶遠程登錄 MySQL

Disallow root login remotely? (Press y|Y for Yes, any other key for No) : y
Success.

By default, MySQL comes with a database named ‘test’ that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production
environment.

刪除 test 庫以及用戶對 test 庫的訪問權限

Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y

  • Dropping test database…
    Success.
  • Removing privileges on test database…
    Success.

Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.

重新加載授權表

Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y
Success.

All done!

步驟四:安裝 php#

# 下載php組件
 yum install -y php php-cli php-fpm php-common php-mysqlnd php-gd php-mbstring
# 查看php版本
 php -v

修改 nginx 配置文件以啟用 php

# 備份Nginx配置文件
cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.bak
# 在Nginx配置文件中修改內容,在server部分添加內容
vi /etc/nginx/nginx.conf
**************************nginx.conf**************************
    server {
        listen       80;
        listen       [::]:80;
        server_name  _;
        root         /data; #網站根目錄

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;
        location / { #添加
            index index.php index.html index.htm;
        }
        location ~ .php$ {
            root /data; #網站根目錄
            fastcgi_pass 127.0.0.1:9000; #Nginx通過本機的9000端口將PHP請求轉發給PHP-FPM進行處理
            fastcgi_index index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include fastcgi_params; #Nginx調用fastcgi接口處理PHP請求
        }
        error_page 404 /404.html;
        location = /404.html {
        }

        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
        }
    }

**************************nginx.conf**************************

# 啟動nginx,並設置開機自啟
systemctl start nginx
systemctl enable nginx

配置並驗證 php

# 保證 nginx 進程的管理用戶和 PHP 服務進程的管理用戶保持一致
vi /etc/php-fpm.d/www.conf
user = nginx
group = nginx
# 重啟nginx
systemctl restart nginx

# 創建網站根目錄
mkdir /data
# 編寫測試php界面
echo "<?php echo phpinfo(); ?>" >/data/index.php
# 啟動php-fpm,並設置開機自啟
systemctl start php-fpm
systemctl enable php-fpm

步驟五:訪問測試#

在瀏覽器中輸入http://本機ip/index.php進行訪問,如下圖所示,表示 LNMP 環境部署成功。

1

方案二:腳本一鍵安裝#

yum install -y wget
# 下載lnmp安裝腳本
wget https://soft.lnmp.com/lnmp/lnmp2.1beta.tar.gz
# 解壓
tar -vzxf lnmp2.1beta.tar.gz
cd lnmp2.1
# 執行腳本
./install.sh
# 按引導進行安裝即可

在瀏覽器http://本機ip/訪問,出現以下界面,即為部署成功。

2

打開 lnmp2.1/conf/nginx.conf 文件,查看其網站根目錄為 /home/wwwroot/default,

在其網站根目錄 /home/wwwroot/default 中# 編寫測試 php 界面,在瀏覽器中輸入http://本機ip/index.php進行訪問,結果如方案一中的《步驟五》中的圖所示,即為成功。

echo "<?php echo phpinfo(); ?>" >/data/index.php
載入中......
此文章數據所有權由區塊鏈加密技術和智能合約保障僅歸創作者所有。