Debian9 安装LNMP

该博客主要介绍了在系统中安装必要软件及Nginx、PHP、MySQL的详细步骤。包括添加key和源、更新源、安装软件、设置系统启动、查看版本等操作,还涉及PHP防止跨目录攻击的修改、新站点配置,以及MySQL修改root密码和允许远程登录等内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

安装部分必要软件

apt-get install curl vim wget unzip apt-transport-https lsb-release ca-certificates

Nginx安装

  • 增加key

wget -O /etc/apt/trusted.gpg.d/nginx-mainline.gpg https://packages.sury.org/nginx-mainline/apt.gpg

  • 增加源

cat >> /etc/apt/sources.list.d/nginx.list << EOF
deb https://mirror.xtom.com.hk/sury/nginx-mainline/ $(lsb_release -sc) main
EOF

  • 更新源并安装

apt-get update
apt-get install nginx-extras

  • 设置系统启动

systemctl enable nginx

  • 查看版本

nginx -v

PHP安装

  • 加入gpg

wget -O /etc/apt/trusted.gpg.d/php.gpg https://mirror.xtom.com.hk/sury/php/apt.gpg

  • 加入源

sh -c ‘echo “deb https://mirror.xtom.com.hk/sury/php/ $(lsb_release -sc) main” > /etc/apt/sources.list.d/php.list’

  • 更新源

apt-get update

  • 安装PHP7.2

apt install php7.2-fpm php7.2-mysql php7.2-curl php7.2-gd php7.2-mbstring php7.2-xml php7.2-xmlrpc php7.2-zip php7.2-opcache -y

  • 修改 php.ini 防止跨目录攻击

sed -i ‘s/;cgi.fix_pathinfo=1/cgi.fix_pathinfo=0/’ /etc/php/7.2/fpm/php.ini

  • 开启php-fpm

service php7.2-fpm start

  • 开机启动

systemctl enable php7.2-fpm

  • nginx配置新站点

vim /etc/nginx/sites-enabled/example.com.conf

server {  
        listen 80;
        listen [::]:80;

# 指定网站目录,可根据自己情况更换,建议放在 /var/www 目录下
        root /var/www/example.com;
        index index.php index.html index.htm;

# 默认第一个域名,替换 example.com 为您的域名
        server_name example.com;

        location / {
            try_files $uri $uri/ =404;
        }

# 开启 PHP7.2-fpm 模式,如需要安装 PHP 7.1.x 请修改为 fastcgi_pass unix:/run/php/php7.1-fpm.sock;
        location ~ \.php$ {
            include snippets/fastcgi-php.conf;
            fastcgi_pass unix:/run/php/php7.2-fpm.sock;
        }
}
  • 重启nginx

service nginx restart

  • 验证

vim /var/www/example.com/phpinfo.php

<?php phpinfo(); ?>

  • win10 hosts文件添加域名

127.0.0.1 example.com

  • 浏览器输入 http://example.com/phpinfo.php
server {  
        listen 80;
        listen [::]:80;

        root /var/www/example.com;
        index index.php index.html index.htm;


        server_name example.com;

        location / {
            try_files $uri $uri/ =404;
        }

        location ~ \.php$ {
            include snippets/fastcgi-php.conf;
            fastcgi_pass unix:/run/php/php7.2-fpm.sock;
        }
}

MySQL安装

  • 使用 Percona Server 代替原生的 MySQL
  • 导入 Percona Server 的官方源

wget https://repo.percona.com/apt/percona-release_0.1-5.$(lsb_release -sc)_all.deb

dpkg -i percona-release_0.1-5.$(lsb_release -sc)_all.deb

  • 更新源

apt-get update

  • 安装mysql

apt-get install percona-server-server-5.7

  • 输入两次root密码

  • 开启服务,检查版本

service mysql start
mysql -V

  • 开机启动

systemctl enable mysql

  • 修改root密码,允许root用户远程登录
            mysql -u root -p 
mysql> use mysql;
mysql> set password for root@localhost = password('root');
mysql> update user set host='%' where user='root';
mysql> select host, user from user;
mysql> flush privileges;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值