当前位置:首页 > CN2资讯 > 正文内容

nginx debian安装教程

4天前CN2资讯


前言

本篇的LNMP是指 Linux(Debian) + Nginx + MariaDB + PHP 。

本篇搭建的环境为当前最新版:

Linux: Debian 9 (stretch) 4.9.0-5-amd64

Nginx: 1.13.12 MariaDB: 10.2.15 PHP: 7.2.5

本篇建立在购买 VPS 并使用 Xshell 连接上的基础上。

如若未曾购买 VPS ,请先阅读我的另一篇文章:VPS与域名

如若还未使用 Xshell 连接 VPS ,请阅读: Xshell登陆VPS

那么,当一切准备就绪,请欣赏我的表演吧,毕竟参加过补习班,是会比你们优秀的了。

注:以下全部语句皆在 root 用户下执行。

准备工作

设置时区

基本上国外的 VPS 创建的 Server 时区都不是中国的,所以首先要修改时区。

vim 命令打开root目录下的 .profile 文件: vim .profile 在末尾添加: TZ='Asiz/Shanghai' export TZ 输入 date 命令验证是否修改成功: date




安装编译环境和一些必要工具

例行更新源: apt-get update apt-get upgrade 安装: apt-get install build-essential apt-transport-https software-properties-common lsb-release ca-certificates dirmngr

Nginx安装

这里安装的是最新开发版(mainline) Nginx 。

获取Nginx包密钥(Key)


wget https://nginx.org/keys/nginx_signing.key apt-key add nginx_signing.key


添加Nginx官方源

echo -e "deb https://nginx.org/packages/mainline/debian/ $(lsb_release -sc) nginx\ndeb-src https://nginx.org/packages/mainline/debian/ $(lsb_release -sc) nginx" | tee /etc/apt/sources.list.d/nginx.list apt-get update


安装Nginx

apt-get remove nginx-common apt-get install nginx


运行Nginx并检测

systemctl start nginx curl -I 127.0.0.1 显示类似如下信息则表示Nginx正常运行: HTTP/1.1 200 OK Server: nginx/1.13.12 Date: Thu, 31 May 2018 16:39:32 GMT Content-Type: text/html Content-Length: 612 Last-Modified: Mon, 09 Apr 2018 16:01:09 GMT Connection: keep-alive ETag: "5acb8e45-264" Accept-Ranges: bytes

浏览器检测

打开浏览器,输入 VPS Server 的IP,将会看到这样的页面:



这就说明Nginx安装成功啦。接下来安装php。

PHP安装

PHP也是安装的最新版。

添加PHP源


wget -O /etc/apt/trusted.gpg.d/php.gpg https://packages.sury.org/php/apt.gpg echo "deb https://packages.sury.org/php/ $(lsb_release -sc) main" | tee /etc/apt/sources.list.d/php.list apt-get update


安装PHP以及部分库

安装PHP


apt-get install php7.2

安装完后会显示运行 Apache 服务失败,这是正常情况,因为我们先安装并运行了Nginx,Nginx占用了80端口,导致 Apache 服务运行失败。

安装必要库

是不是真的必要我也不清楚了,有些是后来发现一些WordPress的插件需要才添加上去的。

apt-get install php7.2-fpm php7.2-cgi php7.2-curl php7.2-gd php7.2-xml php7.2-xmlrpc php7.2-mysql php7.2-bz2


安装非必要库

这些倒是我自己瞎装的,看到可能有用就装了。至于各个库的用途我就不详细说明了,可以上PHP官方查。地址:

http://php.net/manual/zh/extensions.php apt-get install php7.2-bcmath php7.2-gmp php7.2-mbstring php7.2-readline php7.2-zip


安装MariaDB

获取密钥(Key)


apt-key adv --recv-keys --keyserver keyserver.ubuntu.com 0xF1656F24C74CD1D8


添加MariaDB源


echo -e "deb http://mirrors.tuna.tsinghua.edu.cn/mariadb/repo/10.2/debian $(lsb_release -sc) main\ndeb-src http://mirrors.tuna.tsinghua.edu.cn/mariadb/repo/10.2/debian $(lsb_release -sc) main" | tee /etc/apt/sources.list.d/mariadb.list apt-get update


安装MariaDB


apt-get install mariadb-server mariadb-client 安装过程中会要求设置数据库的root用户的密码,如下:


填写好后按回车确定,然后要求再次输入,那就再输入一遍吧,设置完成后等待安装完成即可。

配置Nginx与PHP

安装好后还要配置一下才能用呢,这部分比较麻烦,还请耐心的继续完成它,为了秀(zhuang)出你的个人网站(bi)。

修改PHP参数

打开 php-fpm配置文件 /etc/php/7.2/fpm/php.ini vim /etc/php/7.2/fpm/php.ini 找到 cgi.fix_pathinfo 参数,改为: cgi.fix_pathinfo=0 亲,忘了 vim 的查找指令了吗? /cgi.fix_pathinfo



查看PHP监听方式


vim /etc/php/7.2/fpm/pool.d/www.conf 打开 www.conf 配置文件,找到 user 和 listen


可以看到 PHP 的用户是 www-data,请记住这个用户名。 注意啦这里监听的是一个文件,小伙伴都知道,在 Unix/Linux 里“一切皆文件”,所以在这里监听一个文件是完全正确并OK的,因为在 Unix/Linux 里本地 Socket 的本质就是文件。 不过我们要记住该文件路径,接下来就要用到它。

修改Nginx配置文件

打开 Nginx 配置文件:

vim /etc/nginx/nginx.conf


user nginx; worker_processes 1; error_log /var/log/nginx/error.log warn; pid /var/run/nginx.pid; events { worker_connections 1024; } http { include /etc/nginx/mime.types; default_type application/octet-stream; log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log /var/log/nginx/access.log main; sendfile on; #tcp_nopush on; keepalive_timeout 65; #gzip on; include /etc/nginx/conf.d/*.conf; } 这里可以看到第一行的 user 为 nginx,跟 php 不一致,我们把它改成与 php 一致。 改为 www-data 。 除此之外,我们把 gzip 功能打开,把注释符号(#)去掉即可。你问我为什么要打开这个功能?前面装的 php gzip 扩展库不能白装啊,去掉注释再说。 修改好后如下: user www-data; worker_processes 1; error_log /var/log/nginx/error.log warn; pid /var/run/nginx.pid; events { worker_connections 1024; } http { include /etc/nginx/mime.types; default_type application/octet-stream; log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log /var/log/nginx/access.log main; sendfile on; #tcp_nopush on; keepalive_timeout 65; gzip on; include /etc/nginx/conf.d/*.conf; }


 

最后我们可以看到最后一行:

include /etc/nginx/conf.d/*.conf;

这说明 conf.d 文件夹里还有其他配置文件,我们去看看。


vim /etc/nginx/conf.d/default.conf


server { listen 80; server_name localhost; #charset koi8-r; #access_log /var/log/nginx/host.access.log main; location / { root /usr/share/nginx/html; index index.html index.htm; } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } # proxy the PHP scripts to Apache listening on 127.0.0.1:80 # #location ~ \.php$ { # proxy_pass http://127.0.0.1; #} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # #location ~ \.php$ { # root html; # fastcgi_pass 127.0.0.1:9000; # fastcgi_index index.php; # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; # include fastcgi_params; #} # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #location ~ /\.ht { # deny all; #} }


根据注释信息,我们把该去掉的注释都去掉,并修改 root 路径,以及修改 fpm 的本地端口监听为本地文件监听,即之前在 PHP 配置文件看到的 listen 的文件路径。

修改好后如下:


server { listen 80; server_name localhost; root /home/wordpress; #charset koi8-r; access_log /var/log/nginx/host.access.log main; location / { # root /usr/share/nginx/html; index index.php index.html index.htm; } error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { # root /usr/share/nginx/html; } # proxy the PHP scripts to Apache listening on 127.0.0.1:80 # #location ~ \.php$ { # proxy_pass http://127.0.0.1; #} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # location ~ \.php$ { # root html; fastcgi_pass unix:/run/php/php7.2-fpm.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # location ~ /\.ht { deny all; } }


注意仔细比较,以免遗漏或改错,当然,我贴代码而不是放图片肯定是为了方便你复制粘贴啊,做人要机智点啊,兄dei。

最后输入 Nginx 配置文件语法检查命令来检查语法是否正确:


nginx -t 正确无误的话会显示: nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful 最后我们重启一下 Nginx 服务: systemctl restart nginx


检测Nginx和PHP配置

可以看到,在配置文件中,我将 root 路径设置为 /home/wordpress 。因此,创建该文件夹,添加 PHP 测试文件,并修改用户与用户组为 www-data 。


cd /home mkdir wordpress echo -e "<?php\nphpinfo();\n?>" | tee wordpress/index.php chown -R www-data.www-data wordpress/


打开IE,输入IP地址,如果页面显示 PHP 信息,则说明配置成功啦。如果提示了各种错误的话,请仔细检查配置文件哦。


配置MariaDB

MariaDB初始化

执行以下命令进行初始化:


mysql_secure_installation


输入 MariaDB 的 root 用户的密码后,根据提示信息进行配置即可。

为WordPress建立一个数据库

登陆MariaDB


mysql -u root -p


输入密码,登陆。

创建数据库

创建一个用户:


create user 'user_wordpress'@'localhost' identified by '123456';


user_wordpress 可以改为你喜欢的用户名, 123456 则是该用户的密码。

创建一个数据库:

create database db_wordpress default charset utf8 collate utf8_general_ci; db_wordpress 可以改为你喜欢的库名,utf8 和 utf8_general_ci 是为了设置该数据库使用的字符集。 给 user_wordpress 用户添加库 db_wordpress 的操作权限: grant all privileges on db_wordpress.* to 'user_wordpress'@'localhost' identified by '123456'; flush privileges; 注意,请记住用户名、密码、以及库名,在配置 WordPress 的时候需要填写。 退出: exit


MariaDB 配置完成。

安装WordPress并配置

安装WordPress

删除掉之前为了检验Nginx和PHP配置创建的 wordpress 文件夹:


rm -R wordpress/


下载并解压 WordPress 包:


wget http://wordpress.org/latest.tar.gz tar -zxvf latest.tar.gz


配置WordPress

复制配置文件:


cp wordpress/wp-config-sample.php wordpress/wp-config.php


打开配置文件并修改:


vim wordpress/wp-config.php


根据提示以及创建数据库时使用的信息,修改红框中的内容,从上往下依次是 数据库名、数据库用户、数据库用户密码、字符集:




修改好后大致如下:




保存修改,最后一步,修改 wordpress 文件夹以及全部子文件子文件夹的用户:

chown -R www-data.www-data wordpress/


完成

至此全部后台操作已经完成,出了一口长气,还是挺复杂的。

好了,是时候开始我们的表演的,打开浏览器,输入IP,锵~锵~锵~锵~






oh~我们成功进入了 WordPress 的安装页面。安装好后,就可以自定义你的个人网站啦。

 


    你可能想看:

    扫描二维码推送至手机访问。

    版权声明:本文由皇冠云发布,如需转载请注明出处。

    本文链接:https://www.idchg.com/info/19333.html

    分享给朋友:

    “nginx debian安装教程” 的相关文章

    如何在VPS上轻松安装Chrome浏览器:详细步骤与优化技巧

    1.1 下载Chrome安装包 在VPS上安装Chrome浏览器的第一步是获取安装包。通常,我会选择从网盘下载地址获取Chrome安装包。打开下载链接后,输入提取码即可开始下载。这种方式不仅方便,还能确保安装包的来源可靠。下载完成后,我会将安装包保存到一个易于找到的目录,以便后续操作。 1.2 解压...

    宝塔面板安装指南:轻松搭建与管理云服务器

    宝塔面板是一款专为服务器运维设计的工具,以其简单易用的操作界面受到广泛欢迎。我在使用云服务器建站时,发现宝塔面板让繁琐的服务器管理变得轻松自如。它支持一键安装LAMP和LNMP环境,用户可以在数分钟内搭建出一个完整的网站环境,而且它还集成了监控、数据库管理、FTP等多种功能,真是一个多面手。 无论是...

    水牛VPS:高性能虚拟专用服务器的最佳选择与比较

    水牛城VPS,顾名思义,是在美国纽约州布法罗市托管的虚拟专用服务器。这种服务器因其独特的地理位置和优越的技术配置,吸引了众多用户,特别是需要高性能和灵活性的网站和应用程序。这类服务的定义非常简单,但其特点却非常丰富。通常来说,水牛城VPS提供了良好的网络带宽、灵活的存储选项,以及能够根据用户需求进行...

    双ISP配置:提升网络可靠性与速度的最佳解决方案

    双ISP,顾名思义,就是同时连接两个互联网服务提供商。这种配置听起来可能有点复杂,但其实它是为了确保我们在享受网络服务时能够拥有更高的可靠性和更好的体验。想象一下,当你正在进行重要的在线会议或下载一个大文件,网络突然断了,这可真让人头疼。而双ISP就能帮助我们避免这样的困境。 双ISP的基本概念是,...

    IEPL:企业国际以太网专线的优势与申请指南

    什么是IEPL 当我第一次接触IEPL(International Ethernet Private Line)时,我意识到它是一种为企业提供国际级别的网络连接服务,特别适合那些需要在不同国家和地区之间高效、安全传输数据的公司。这项服务一般由电信运营商提供,旨在帮助企业实现及时的信息交流和数据传输。...

    搬瓦工(BandwagonHOST)VPS服务器购买指南与套餐对比

    搬瓦工,大家熟悉的名字,实际上是BandwagonHOST的中文称呼。这家公司是加拿大IT7 Networks旗下的子公司,专注于提供VPS服务器主机服务,目标用户涵盖了全球多个国家和地区,包括美洲、欧洲和亚洲等地。对于那些需要稳定和高性能服务器的用户来说,搬瓦工绝对是一个值得考虑的选项。 说到搬瓦...