如何在 Debian 8 上安裝 LEMP(Linux、Nginx、MySQL 和 PHP-FPM)

在本文中,我們將引導您在 Debian 8 上安裝 LEMP(Linux、Nginx、MySQL 和 PHP-FPM)。LEMP 堆棧是 LEMP 服務器或 LEMP Web 服務器的同義詞。 它包括 Linux、Nginx、MySQL (MariaDB) 和 PHP。

更新系統

確保您的服務器完全是最新的:

apt-get update && apt-get upgrade

安裝 NGINX

要在您的 Debian 8 服務器上安裝 Nginx,您需要運行以下命令:

apt-get install nginx

安裝完成後,就可以開始使用Nginx了:

systemctl start nginx

啟用 Nginx 開機啟動:

systemctl enable nginx

可能出現的問題:

如果你在安裝 Nginx 時遇到類似這樣的錯誤:

dpkg: error processing package nginx (--configure):
dependency problems - leaving unconfigured
Processing triggers for systemd (215-17+deb8u1) ...
Errors were encountered while processing:
nginx-full
nginx
E: Sub-process /usr/bin/dpkg returned an error code (1)

然後,您可以通過打開默認的 Nginx 配置文件並註釋掉該行來修復聽 [::]: 80 default_server;... 輸入以下命令:

vim /etc/nginx/sites-available/default

找到線聽 [::]: 80 default_server;並通過在行前添加# 符號將其註釋掉。 重新啟動 Nginx 以使更改生效並運行命令以安裝 Nginx 包管理器:

systemctl restart nginx

apt-get install nginx

通過打開 Web 瀏覽器並訪問您的服務器 IP (https://server_ip) 來驗證 Nginx 是否正在運行。 您應該會看到如下所示的 Nginx 歡迎頁面:

安裝MySQL

現在讓我們安裝MySQL。 輸入以下內容:

apt-get install mysql-server

安裝期間,系統將提示您輸入 MySQL root 用戶的密碼。 不要輸入可以破解的簡單密碼。 它必須至少為 8 個大小寫混合的字符。

現在已經安裝了 MySQL,我們建議您使用以下命令安全地配置 MySQL:

mysql_secure_installation

輸入您的 root 密碼,並在要求更改 MySQL root 密碼時回答“n”。 以下是您可以遵循的整個過程:

Setting the root password ensures that nobody can log into the MySQL
root user without the proper authorisation.

You already have a root password set, so you can safely answer 'n'.

Change the root password? [Y/n] n
... skipping.

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? [Y/n] 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.

Disallow root login remotely? [Y/n] 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.

Remove test database and access to it? [Y/n] y
- Dropping test database...
ERROR 1008 (HY000) at line 1: Can't drop database 'test'; database doesn't exist
... Failed!  Not critical, keep moving...
- 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? [Y/n] y
... Success!

Cleaning up...


All done!  If you've completed all of the above steps, your MySQL
installation should now be secure.

Thanks for using MySQL!

使 MySQL 開機自啟動:

systemctl enable mysql

安裝 PHP-FPM

通過運行以下命令安裝 PHP-FPM:

apt-get install php5-fpm php5-mysql

下一步要做的是修改 Nginx 配置文件。 移動默認文件並創建一個新的 Nginx 文件。 以下命令將完全做到這一點:

mv /etc/nginx/sites-available/default /etc/nginx/sites-available/default.old

vim /etc/nginx/sites-available/default

現在您已經打開了一個新的默認文件,粘貼以下內容:

server {
        listen       80;
        server_name  your_website_name.ru;
        root /var/www/html;
        index index.php index.html index.htm index.nginx-debian.html;
        location / {
                try_files $uri $uri/ =404;
        }
        error_page 404 /404.html;
        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
                root /var/www/html;
        }
        location ~ .php$ {
                try_files $uri =404;
                fastcgi_pass unix:/var/run/php5-fpm.sock;
                fastcgi_index index.php;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                include fastcgi_params;
        }
}

保存並關閉文件。

現在,讓我們創建一個簡單的 PHP 頁面測試。 創建一個 PHP 信息頁面,以便您可以檢查您的 PHP 版本、活動模塊等……

創建一個文件,讓我們命名它信息.php在目錄中/var/www/html:

vim /var/www/html/info.php

將以下代碼粘貼到文件中:

<?php
phpinfo();
?>

重啟 Nginx 以使更改生效:

systemctl restart nginx

現在打開您的網絡瀏覽器並轉到 https://your_server_ip_address/info.php。 我們很高興歡迎您訪問類似於以下網頁的網頁:

infophp

就這樣。 您已成功安裝堆棧LEMP在 Debian 8 上。

相關文章