安装配置 LNMP 环境

/ 0评 / 0

过去的很多年里,我都习惯了用 lnmp 一键包或 oninstack 的一键包来给 VPS 设置网站环境。

可是随着这两家相继被国内的一家企业收购后,接连都爆出了安装包里包含木马的新闻,12

这真是一件让人万分惋惜的事。他们也许有他们的难处,可是这样的事实也确实让人对脚本的开发者失去了信任。不管咋说,感谢他们两位之前为个人站长所做的一切。

于是我决定开始使用安装包的方式来部署环境,这样有两个好处:

当然除了一键脚本之外,还有不少的网站面板也支持预构建网站环境,可是出于以下原因我并没有选择使用面板:

现在记录一下在一台重装完 Debian 系统的 VPS 上设置 LEMP 环境的步骤:

更新软件包

apt update

apt upgrade -y

安装 PHP

curl -sSLo /usr/share/keyrings/deb.sury.org-php.gpg https://packages.sury.org/php/apt.gpg

echo "deb [signed-by=/usr/share/keyrings/deb.sury.org-php.gpg] https://packages.sury.org/php/ $(lsb_release -sc) main" > /etc/apt/sources.list.d/php.list

apt update

apt-cache madison php

apt-cache search php7.4

apt install php7.4-common php7.4-cli php7.4-fpm php7.4-gd php7.4-mysql php7.4-mbstring php7.4-curl php7.4-xml php7.4-xmlrpc php7.4-zip php7.4-intl php7.4-bz2 php7.4-bcmath php7.4-redis php7.4-swoole php7.4-opcache -y

php --version

配置 PHP-FPM

sed -i 's/post_max_size = 8M/post_max_size = 50M/g' /etc/php/7.4/fpm/php.ini
sed -i 's/upload_max_filesize = 2M/upload_max_filesize = 50M/g' /etc/php/7.4/fpm/php.ini
sed -i 's/memory_limit = 128M/memory_limit = 256M/g' /etc/php/7.4/fpm/php.ini

安装 Nginx

curl https://nginx.org/keys/nginx_signing.key | gpg --dearmor | tee /usr/share/keyrings/nginx-archive-keyring.gpg > /dev/null

echo "deb [signed-by=/usr/share/keyrings/nginx-archive-keyring.gpg] http://nginx.org/packages/debian `lsb_release -cs` nginx" | tee /etc/apt/sources.list.d/nginx.list

apt update

apt install nginx

nginx -V

配置 Nginx,将 user nginx; 改为 user www-data www-data;

nano /etc/nginx/nginx.conf

……

user  www-data www-data;

……

Ctrl + x, y , 回车。

安装 MariaDB 和 Redis

apt install mariadb-server redis-server

配置 MariaDB

mysql_secure_installation


NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

In order to log into MariaDB to secure it, we'll need the current
password for the root user. If you've just installed MariaDB, and
haven't set the root password yet, you should just press enter here.

Enter current password for root (enter for none): 
OK, successfully used password, moving on...

Setting the root password or using the unix_socket ensures that nobody
can log into the MariaDB root user without the proper authorisation.

You already have your root account protected, so you can safely answer 'n'.

Switch to unix_socket authentication [Y/n] n
 ... skipping.

You already have your root account protected, so you can safely answer 'n'.

Change the root password? [Y/n] y     
New password: 
Re-enter new password: 
Password updated successfully!
Reloading privilege tables..
 ... Success!


By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB 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, MariaDB 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...
 ... 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? [Y/n] y
 ... Success!

Cleaning up...

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

Thanks for using MariaDB!

配置 Opcache

echo "opcache.enable_cli=1" >> /etc/php/7.4/fpm/conf.d/10-opcache.ini

echo "opcache.memory_consumption=128" >> /etc/php/7.4/fpm/conf.d/10-opcache.ini

echo "opcache.interned_strings_buffer=8" >> /etc/php/7.4/fpm/conf.d/10-opcache.ini

echo "opcache.max_accelerated_files=4000" >> /etc/php/7.4/fpm/conf.d/10-opcache.ini

echo "opcache.revalidate_freq=60" >> /etc/php/7.4/fpm/conf.d/10-opcache.ini

大致就是以上内容,其余需要调整的,要等到弄站点的时候才需要了。

最后,重启一下。

reboot

鸣谢:
How to Install LEMP Stack (Nginx, PHP and MariaDB) on Debian 12

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注