[N1盒子] 使用N1命令安装linux+nginx+mysql+php7.3建网站(不用面板,手动安装LNMP)

2019-02-16
0评论
/
6310阅读
爱搜啊

[N1盒子] 使用命令安装linux+nginx+mysql+php7.3建网站(不用面板,手动安装LNMP)

首先,说说建站需要准备什么:

一个域名(可以不要,直接输入ip来访问网站,不推荐)——因为我们用N1做实验,直接在内网访问,所以不要域名了。

一个tls,本文以cloudflare为例,用于加速访问/保护网站。

一个公网ip(没有的话可以动态域名解析、内网穿透,不在本文讨论范围中)

一台vps服务器——本文用的N1

然后,再说说基本思路。搭建一个简单的动态网站,需要以下内容:

一台服务器(Linux系统)

网站服务,一般用三个下面中的一个:

apache——读作“阿帕奇”

Nginx——读作“引擎X”(N1用这个)

caddy——读作“凯蒂”(这个最好使,vps选他!)

一个数据库(mysql

php解释语言。

网站主体部分,本文以wordpress为例。

如果需要其他服务,自行搭建即可。

本文主要讲解使用LCMP(linux+caddy+mysql+php)方式搭建网站,因为caddy能够自动申请ssl证书并自动续签,很方便。服务器租赁,域名购买,cloudfare注册配置,域名解析这些部分都很简单,随便搜索一下就有,这里不再赘述。

debian 9

服务器的系统,我们选择debian 9。

Debian是完全由自由软件组成的类UNIX操作系统,其包含的多数软件使用GNU通用公共许可协议授权,并由Debian计划的参与者组成团队对其进行打包、开发与维护。 作为最早的Linux发行版之一,Debian在创建之初便被定位为在GNU计划的精神指导下进行公开开发并自由发布的项目。

——摘自维基百科

apt update && apt upgrade -y

首先在SSH登录N1输入上面命令升级系统软件包

nginx

Nginx(发音同engine x)是异步框架的 Web服务器,也可以用作反向代理,负载平衡器 和 HTTP缓存。该软件由 Igor Sysoev 创建,并于2004年首次公开发布。同名公司成立于2011年,以提供支持。

Nginx是免费的开源软件,根据类BSD许可证的条款发布。一大部分Web服务器使用Nginx,通常作为负载均衡器。

——摘自维基百科

安装nginx

apt update

apt install nginx

测试

systemctl start nginx

然后在浏览器输入你N1的ip地址,会显示nginx运行成功。

修改配置文件

vim /etc/nginx/sites-enabled/default

使用上面命令打开nginx配置文件

修改下面这些加粗的地方:

##
# You should look at the following URL's in order to grasp a solid understanding
# of Nginx configuration files in order to fully unleash the power of Nginx.
# https://www.nginx.com/resources/wiki/start/
# https://www.nginx.com/resources/ ... ls/config_pitfalls/
# https://wiki.debian.org/Nginx/DirectoryStructure
#
# In most cases, administrators will remove this file from sites-enabled/ and
# leave it as reference inside of sites-available where it will continue to be
# updated by the nginx packaging team.
#
# This file will automatically load configuration files provided by other
# applications, such as Drupal or Wordpress. These applications will be made
# available underneath a path with that package name, such as /drupal8.
#
# Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.
##

# Default server configuration
#
server {
        listen 80 default_server;
        listen [::]:80 default_server;

        # SSL configuration
        #
        # listen 443 ssl default_server;
        # listen [::]:443 ssl default_server;
        #
        # Note: You should disable gzip for SSL traffic.
        # See: https://bugs.debian.org/773332
        #
        # Read up on ssl_ciphers to ensure a secure configuration.
        # See: https://bugs.debian.org/765782
        #
        # Self signed certs generated by the ssl-cert package
        # Don't use them in a production server!
        #
        # include snippets/snakeoil.conf;

        root /var/www/html;


        # Add index.php to the list if you are using PHP
        index index.html index.htm index.php index.nginx-debian.html;

        server_name _;

        location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                try_files $uri $uri/ =404;server {
        listen 80 default_server;
        listen [::]:80 default_server;

        # SSL configuration
        #
        # listen 443 ssl default_server;
        # listen [::]:443 ssl default_server;
        #
        # Note: You should disable gzip for SSL traffic.
        # See: https://bugs.debian.org/773332
        #
        # Read up on ssl_ciphers to ensure
        }

        # pass PHP scripts to FastCGI server
        #
        location ~ \.php$ {
                include snippets/fastcgi-php.conf;
        #
        #       # With php-fpm (or other unix sockets):
         fastcgi_pass unix:/var/run/php/php7.3-fpm.sock;
        #       # With php-cgi (or other tcp sockets):
        #       fastcgi_pass 127.0.0.1:9000;
        }

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        location ~ /\.ht {
                deny all;

        }
}

重启nginx

systemctl restart nginx

MariaDB (MySQL的开源分支)

MariaDB数据库管理系统是MySQL的一个分支,主要由开源社区在维护,采用GPL授权许可。开发这个分支的原因之一是:甲骨文公司收购了MySQL后,有将MySQL闭源的潜在风险,因此社区采用分支的方式来避开这个风险。

——摘自维基百科

按照下面内容进行安装,注意修改黑体字内容。

apt install mysql-server -y

mysql_secure_installation

mysql -u root -p

CREATE DATABASE wordpress DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

GRANT ALL ON wordpress.* TO 'wordpress'@'localhost' IDENTIFIED BY 'password';

FLUSH PRIVILEGES;

EXIT;

php7.3

PHP(全称:PHP:Hypertext Preprocessor,即“PHP:超文本预处理器”)是一种开源的通用计算机脚本语言,尤其适用于网络开发并可嵌入HTML中使用。PHP的语法借鉴吸收C语言、Java和Perl等流行计算机语言的特点,易于一般程序员学习。PHP的主要目标是允许网络开发人员快速编写动态页面,但PHP也被用于其他很多领域。

——摘自维基百科

debain9想要安装php7.3,需要先添加软件源:

apt install ca-certificates apt-transport-https -y

wget -q https://packages.sury.org/php/apt.gpg -O- | apt-key add -

echo "deb https://packages.sury.org/php/ stretch main" | tee /etc/apt/sources.list.d/php.list

apt update

apt install php7.3-fpm -y

apt install php7.3 php7.3-mysql php7.3-gd php7.3-xml php7.3-cgi php7.3-cli php7.3-curl php7.3-zip php7.3-mbstring unzip -y

启动服务

systemctl enable php7.3-fpm

systemctl start php7.3-fpm

最后,把前面修改过配置文件服务重启一下,确保配置文件正确读取

systemctl restart nginx

systemctl restart php7.3-fpm

至此,LNMP就搭建完了,这是我们之后再添加各种服务(自建网盘/博客/论坛/等等等等)的基础。先演示一下搭建最简单的博客Wordpress吧。

WordPress

With our famous 5-minute installation, setting up WordPress for the first time is simple. We’ve created a handy guide to see you through the installation process.

wordpress以操作简单,“五分钟安装”而著称。具体操作可看上面的英文链接的官方指南。太长不看的话直接用我下面的命令即可。

还是自己替换黑体字:

mkdir /var/www/wordpress

cd /var/www/wordpress

wget https://wordpress.org/latest.tar.gz

tar xvf  latest.tar.gz 

mv wordpress/* .

rm wordpress/ -rf

chown caddy:caddy /var/www/wordpress

现在,使用浏览器访问你的网站,按提示输入之前建立的数据库名和密码,恭喜你建站成功。之后可以自己在浏览器里面修改网站的主题、页面等等。


本站附件分享,如果附件失效,可以去找找看

诚通网盘附件百度网盘附件


于2019-02-16发布
相关文章