配置putty或SecureCRT防止SSH连接中断

当用SSH协议连接Linux服务器时,有时几分钟内没有操作,连接就会中断,必须重新登陆。

本文提供解决方案供参考:

8以CentOS 6.4为例:

1、修改ssh配置文件

vi /etc/ssh/sshd_config

找到ClientAliveInterval,指定了服务器端向客户端发送请求消息的时间间隔, 默认是0,不发送。

将后面的数值设置修改,单位为秒,如10分钟,则可设置参数为600
再找到ClientAliveCountMax,指如果发现客户端没有响应,则判断为一次超时,这个参数设置允许超时的次数,比如10,则代表允许超时 6000秒 = 100分钟。

2、修改SecureCRT会话属性配置

未分类

3、Putty

启用putty keepalive

putty -> Connection -> Seconds between keepalives ( 0 to turn off ),默认为0,改为60。

如下图所示

未分类

其他工具也可根据实际情况修改保活机制相关配置。

samba中实现共享中所有创建的文件都属于指定用户的功能

方法很简单,但很实用。

在需要指定的共享中加入。

force user = 要指定的用户

例如,要求老师得能在“孙锡源”这个学生的共享文件夹中创建文件,但老师创建的文件所有者是老师本人,孙锡源没权限修改及删除文件。那么就可以加一行“force user = sxy”,让老师创建的文件的所有者也是孙锡源本人,这样孙锡源就可以操作文件了。

未分类

使用rsync搭建centos的镜像站

简介

自己一直以来相搭建一个开源镜像站,一方面可以了解搭建镜像站的知识,一方面可以同步那些国内没有的linux发行版软件源,但是最主要的原因只是为了好玩

注意点

我这个教程不是专业教程,但是要注意的是镜像站是一个对I/O要求很高,网络带宽要求很高,磁盘占用量的站点,不然没人用

步骤

  • 安装需要的软件(nginx,rsync)
  • 配置nginx
  • 编写同步脚本

编译安装nginx

  • 安装PCRE库

下载解压

wget https://ftp.pcre.org/pub/pcre/pcre-8.41.tar.gz
tar -zxvf pcre-8.41.tar.gz
cd pcre-8.41

编译安装

./configure --prefix=/usr/local/pcre
make
make install
  • 安装zlib

下载解压

wget http://zlib.net/zlib-1.2.11.tar.gz
tar -zxvf zlib-1.2.11.tar.gz
cd zlib-1.2.11

编译安装

./configure --prefix=/usr/local/zlib
make
make install
  • 安装openssl库
wget https://www.openssl.org/source/openssl-1.0.2l.tar.gz
tar -zxvf openssl-1.0.2l.tar.gz
cd openssl-1.0.2l

编译安装

./config --prefix=/usr/local/openssl
make
make install
  • 安装nginx

下载解压

wget http://mirrors.sohu.com/nginx/nginx-1.12.1.tar.gz
tar -zxvf nginx-1.12.1.tar.gz
cd nginx-1.12.1

编译安装

./configure --prefix=/usr/local/nginx --with-pcre=/usr/local/pcre/ --with-zlib=/usr/local/zlib/ --with-openssl=/usr/local/openssl/
make

报错

[root@bboysoul nginx-1.12.1]# make
make -f objs/Makefile
make[1]: Entering directory `/root/nginx-1.12.1'
cd /usr/local/pcre/ 
&& if [ -f Makefile ]; then make distclean; fi 
&& CC="cc" CFLAGS="-O2 -fomit-frame-pointer -pipe " 
./configure --disable-shared 
/bin/sh: line 2: ./configure: No such file or directory
make[1]: *** [/usr/local/pcre//Makefile] Error 127
make[1]: Leaving directory `/root/nginx-1.12.1'
make: *** [build] Error 2

百度了一下,原来–with-pcre要指定的不是安装目录而是源码目录

./configure --prefix=/usr/local/nginx --with-pcre=/root/pcre-8.41 --with-zlib=/usr/local/zlib/ --with-openssl=/usr/local/openssl/
make

又报错

src/http/modules/ngx_http_log_module.c:13:18: fatal error: zlib.h: No such file or directory
 #include <zlib.h>
                  ^
compilation terminated.
make[1]: *** [objs/src/http/modules/ngx_http_log_module.o] Error 1
make[1]: Leaving directory `/root/nginx-1.12.1'
make: *** [build] Error 2

直接指定zlib的源码目录好了

./configure --prefix=/usr/local/nginx --with-pcre=/root/pcre-8.41 --with-zlib=/root/zlib-1.2.11 --with-openssl=/usr/local/openssl/
make
make install

成功

  • 测试一下

如果你这个时候还是访问不了你的nginx,出现ERR_ADDRESS_UNREACHABLE这个错误,你要检查一下你的防火墙,可以配置也可以选择关闭

systemctl stop firewalld.service

其实到这一步的时候我已经忘记我在干什么了,看了一下标题,哦!原来我在做一个镜像站,,,,,

安装rsync

yum install rsync

配置

之后,配置一下站点

在home文件夹下创建一个站点文件来存放同步过来的资源

mkdir /home/mirror

之后修改nginx.conf的server段为

    server {
        listen       80;
        server_name  localhost;
        root    /home/mirror;
        location / {    
                autoindex on;
                autoindex_exact_size off;
                autoindex_localtime on;
        }

    }

重启一下服务器

[root@bboysoul sbin]# pidof nginx
31632 31631
[root@bboysoul sbin]# kill 31632 31631
[root@bboysoul sbin]# pidof nginx
[root@bboysoul sbin]# ./nginx
[root@bboysoul sbin]# 

接着你去访问站点,看到的就会是下面这样,因为打开了nginx的目录浏览功能autoindex

Index of /
../

安装createrepo

yum install createrepo

因为centos的软件包目录有点多,而且大,所以为了方便我就只同步epel软件源了

mkdir epel
cd epel
mkdir 7

之后编写一个同步脚本

rsync -avz rsync://mirrors.tuna.tsinghua.edu.cn/epel/7/SRPMS/ /home/mirror/epel/7/SRPMS/
createrepo /home/mirror/epel/7/SRPMS/

rsync -avz rsync://mirrors.tuna.tsinghua.edu.cn/epel/7/aarch64/ /home/mirror/epel/7/aarch64/
createrepo /home/mirror/epel/7/aarch64/

rsync -avz rsync://mirrors.tuna.tsinghua.edu.cn/epel/7/ppc64/ /home/mirror/epel/7/ppc64/
createrepo /home/mirror/epel/7/ppc64/

rsync -avz rsync://mirrors.tuna.tsinghua.edu.cn/epel/7/ppc64le/ /home/mirror/epel/7/ppc64le/
createrepo /home/mirror/epel/7/ppc64le/

rsync -avz rsync://mirrors.tuna.tsinghua.edu.cn/epel/7/x86_64/ /home/mirror/epel/7/x86_64/
createrepo /home/mirror/epel/7/x86_64/

echo "bboysoul done"

赋予执行权限

sudo chmod +x rsync.sh

安装screen

yum install screen

建立一个新的会话

screen -S rsync

执行脚本

./rsync.sh

ctrl+a+d退出

你可以安装bwm-ng来监控网速,我这边的状态是这样的

同步时间很久的,注意磁盘有没有被占满

同步时间肯定很久。

rsync工具远程数据同步备份

基本信息:

  • A:192.168.1.10 源服务器(源数据服务器)
  • B:192.168.1.20 目的服务器(备份存放服务器)

两台服务器系统均为CentOS

1、在A服务器配置rsync服务端

安装rsync

#yum install rsync

编辑配置文件

#vim /etc/rsyncd.conf
[global]
uid=nobody
gid=nobody
use chroot = yes
max connections = 4
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsyncd.lock
log file = /var/log/rsyncd.log
Timeout = 600
Log format = %t %a %m %f %b

[backup]
path=/tmp/data_bak
ignore errors
read only = yes
list = no
auth users = backupuser
secrets file = /etc/rsyncd/rsyncd.secrets
hosts allow = 192.168.1.20
hosts deny = 0.0.0.0/0

创建密码文件,采用这种方式不能使用系统用户对客户端进行认证,所以需要创建一个密码文件,其格式为“username:password”,用户名可以和密码可以随便定义,不要和系统帐户重名,同时要把创建的密码文件权限设置为600,一行一个账号密码,账号与密码用:号隔开

#echo "back:abc123" > /etc/rsyncd.secrets
#chmod 600 /etc/rsyncd.secrets

设置备份数据目录权限为755

#chmod -R 755 /tmp/data_bak

开启防火墙并重启

#iptables -A INPUT -p tcp -m state --state NEW -m tcp --dport 873 -j ACCEPT
#service iptables save
#service iptables restart

启动rsyncd服务

#/usr/bin/rsync --daemon --config=/etc/rsyncd.conf

2、在B服务器配置rsync客户端

这里很简单,只要安装和配置连接密码即可

#yum install rsync
#echo "abc123" > /etc/rsyncd.secrets

执行备份命令

#rsync -vrtopg --password-file=/etc/rsyncd.secrets [email protected]::backup /home/data_bak

rsync 命令的选项含义参考:

http://man.linuxde.net/rsync

这里执行这个命令可能会报一些错误,解决办法如下:

  • 问题1:
@ERROR: chdir failed
rsync error: error starting client-server protocol (code 5) at main.c(1503) [receiver=3.0.6]

原因:服务器端配置文件/etc/rsyncd.conf中use chroot = yes是否配置

  • 问题2:
@ERROR: chroot failed
rsync error: error starting client-server protocol (code 5) at main.c(1503) [receiver=3.0.6]

原因:
服务器端的目录不存在或无权限,创建目录并修正权限可解决问题。注意查看/etc/rsyncd.conf中的path是否配置正确。

  • 问题3:
rsync: opendir "data/2017-08-29" (in backup) failed: Permission denied (13)

原因:
注意查看服务器端同步的目录权限是否为755。

将备份命令加入任务计划

#crontab -e
* * */1 * * rsync -vrtopg --password-file=/etc/rsyncd.secrets [email protected]::backup /home/data_bak

CentOS7安装Certbot解决提示Python-urllib3安装失败

Certbot是一个部署Let’s Encrypt证书的客户端(Let’s Encrypt是一个证书发布机构CA,Let’s Encrypt支持域名加密,即为域名启用https)。

Certbot能够自动的在Web服务器(Apache,Nginx等)上部署从Let’s Encrypt获取的证书,非常简单易用。

Certbot提供了为特定系统和特定服务器安装Certbot的简便方法,使用Certbot提供的为CentOS7系统Apache服务器安装Certbot时,提示:

安装失败

python-urllib3.nonarch 0:1.10.2-2.el7_1

1、安装失败的原因:

那是因为在安装Python时,你已经在系统中安装了urllib3包,而且安装的版本高于1.10。

2、解决方法:

卸载已经安装的Python的urllib3包

pip uninstall urllib3

然后就能够成功安装python-urllib3软件包

3、运行certbot命令提示urllib3版本低

如果你成功安装了python-urllib3软件包,在运行certbot命令时提示urllib3版本太低,可以使用pip命令升级urllib3包:

pip install --upgrade urllib3

python升级后使用yum出现No module named yum错误处理

最近在看python,虚拟机装的是Centos6.6,自带的python版本是2.6.6,打算升级到2.7。

我的升级过程大致如下:

  • 下载2.7源码包https://www.python.org/downloads/source/
  • 卸载旧的python,rpm -e python
  • 编译安装python2.7

整个过程没有遇到问题,但升级完后,再用yum安装软件时报如下错误

[root@localhost python]# yum
There was a problem importing one of the Python modules
required to run yum. The error leading to this problem was:

   No module named yum

Please install a package which provides this module, or
verify that the module is installed correctly.

It's possible that the above module doesn't match the
current version of Python, which is:
2.7.13 (r266:84292, Jan 22 2014, 09:37:14) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-4)]

If you cannot solve this problem yourself, please go to 
the yum faq at:
  http://yum.baseurl.org/wiki/Faq

yum不可用了,这下问题就大了,spacer.gif没有yum,安装rpm包特别费劲。

开始百度关键字“No module named yum”,出来很多帖子和博客,博客上边说的大概意思就是yum就基于python的,升级python后,yum与高版本的python不兼容,导致yum无法使用。博客上给出的方法都是修改/usr/bin/yum文件的头部,把/usr/bin/python修改为/usr/bin/python2.6就可以了,但我的情况是我把旧的python已经卸载了,于是又开始安装2.6版本的python。

安装好2.6.6版本的之后以为就好了,但是还是报有错误:

[root@localhost python]# yum
There was a problem importing one of the Python modules
required to run yum. The error leading to this problem was:

   No module named yum

Please install a package which provides this module, or
verify that the module is installed correctly.

It's possible that the above module doesn't match the
current version of Python, which is:
2.6.6 (r266:84292, Jan 22 2014, 09:37:14) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-4)]

If you cannot solve this problem yourself, please go to 
the yum faq at:
  http://yum.baseurl.org/wiki/Faq

这就奇怪了,都已经把旧版本的装上了,还是不行。这时看到一篇博客是把python和yum都全部卸载后重新安装的。想想这也是个办法于是就又全部下载

whereis python |xargs rm -rf
rpm -e --nodeps python

卸载后重新安装,从光盘镜像里找到python和yum的包

rpm -ivh --nodeps python*
rpm -ivh --nodeps yum*

这次总该好了吧,验证一下

[root@localhost python]# python
Python 2.6.6 (r266:84292, Jan 22 2014, 09:37:14) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import yum
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.6/site-packages/yum/__init__.py", line 23, in <module>
    import rpm
ImportError: No module named rpm

还是报错。。。

spacer.gif不过这次错误变了,变成了“No module named rpm”

再次百度,看到有人说是缺少包:rpm -ivh rpm-python-4.8.0-37.el6.i686.rpm

安装上面的包后一切yum恢复正常了。

那问题又来了难道因为yum就不升级python版本吗?当然不是。其实是我在安装新版python的时候把旧版本的也卸载了才导致这样的问题。

后来在不卸载2.6版本的python是,重新编译安装2.7版本,安装成功并且yum仍然可用。

openresty resty_lua_http模块unable to get local issuer certificate异常处理

最近刚换工作,新公司作为一资讯公司有为客户提供相关SDK去接入公司系统进行一些信息查询作业。随之请求流量的增加,缺少网关层进行API保护,系统常常会因为流量暴增时间段搞垮。自然而然,作为招入公司重构原有系统职责中的开发计划的第一步自然就是打算先做网关了。之前主要是做Java开发,对Openresty做涉及到相关技术见解都很肤浅(欢迎大家拍砖),对中间学习使用Openresty所遇到一些异常在这里做个小记 。(持续更新中…)

模块:Resty_Lua_Http

发起SSL请求异常: 20: unable to get local issuer certificate

示例:

local http = require("resty.http")

local httpc = http.new()

local resp, err = httpc:request_uri("https://m.taobao.com", {
    method = "GET",
    path = "/#index",
    headers = {
        ["User-Agent"] = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.111 Safari/537.36"
    },
})

if not resp then
    ngx.say("request error:", err)
    return
end

原因:nginx没有获得本地颁发者证书

解决方法:在nginx.conf http -> server 加入已安装OpenSSL根证书地址,见第(6,7行)

server {
    listen 8000;
    server_name _;
    resolver 8.8.8.8;
    --
    lua_ssl_verify_depth 2;
    lua_ssl_trusted_certificate /etc/ssl/certs/GlobalSign_Root_CA.pem;
    ---
    location /api {
        default_type "text/html";
        lua_code_cache off;
        content_by_lua_file /home/hf/IdeaProjects/apigateway/lua/http.lua;
    }
}

使用fluentd实时收到nginx日志到mysql数据库

前言

本篇介绍如何使用fluentd把nginx的log日志读取,并且解析成为一个一个MySQL的字段,最后存储到mysql的数据库中。

环境

我用的是aws的ec2,操作系统是amazon定制的Amazon Linux AMI

安装fluentd

使用root用户

curl -L https://toolbelt.treasuredata.com/sh/install-redhat-td-agent2.sh | sh

安装完毕后,在/usr/sbin/下会有td-agent , td-agent-gem ,td-agent-ui三个可执行文件。其中td-agent-gem用来安装之外的fluent的插件。

配置文件在/etc/td-agent/td-agent.conf

启动fluentd命令集合

/etc/init.d/td-agent start
/etc/init.d/td-agent stop
/etc/init.d/td-agent restart
/etc/init.d/td-agent status

log可以在/var/log/td-agent/td-agent.log查看

编辑fluentd配置文件

定义一个source,读取nginx的log文件

<source>
  @type tail
  path /tmp/nginx.log
  pos_file /var/log/td-agent/nginx.log.pos
  tag nginx.access
  format /^(?<remote>[^ ]*) - (?<user>[^ ]*) [(?<time>[^]]*)] "(?<method>S+)(?: +(?<path>[^"]*) +S*)?" (?<code>[^ ]*) (?<size>[^ ]*)(?: "(?<referer>[^"]*)" "(?<agent>[^"]*)" "(?<http_x_forwarded_for>[^"]*)" "(?<host>[^"]*)" "(?<country>[^"]*)" "(?<city>[^"]*)")?$/
  time_format %d/%b/%Y:%H:%M:%S %z
</source>

注意:format的地方需要根据自己的nginx的log的格式进行相应的调整

接下去定义一个写入到mysql的match。

<match nginx.access>
  @type mysql_bulk
  host your_host
  database your_db
  username your_username
  password your_password
  column_names remote,host,user,method,path,code,size,referer,agent,country,city,http_x_forwarded_for,log_time
  key_names remote,host,user,method,path,code,size,referer,agent,country,city,http_x_forwarded_for,${time}
  table nginx_access
  flush_interval 10s
</match>

附上mysql的建表语句

CREATE TABLE `nginx_access` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `remote` text,
  `host` text,
  `user` text,
  `method` text,
  `path` text,
  `code` text,
  `size` text,
  `referer` text,
  `agent` text,
  `country` text,
  `city` text,
  `http_x_forwarded_for` text,
  `log_time` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB;

安装mysql_bulk的插件

从https://github.com/tagomoris/fluent-plugin-mysql克隆下项目。由于我用的fluentd是0.12版本,所以对应的fluent-plugin-mysql的版本是v0.1.5,所以Git checkout v0.1.5。

进行gem build fluent-plugin-mysql.gemspec生成.gem文件。
然后用/usr/sbin/td-agent-gem install fluent-plugin-mysql-0.1.5.gem命令来安装插件。

如果安装插件失败的话,很大可能是没有mysql-devel。

mysql client is missing. You may need to 'apt-get install libmysqlclient-dev' or 'yum install mysql-devel', and try again.

所以先执行yum install mysql-devel。

[root@x fluent-plugin-mysql]# /usr/sbin/td-agent-gem install fluent-plugin-mysql-0.1.5.gem
Building native extensions.  This could take a while...
Successfully installed mysql2-0.4.9
Fetching: mysql2-cs-bind-0.0.6.gem (100%)
Successfully installed mysql2-cs-bind-0.0.6
Fetching: jsonpath-0.8.7.gem (100%)
Successfully installed jsonpath-0.8.7
Successfully installed fluent-plugin-mysql-0.1.5
Parsing documentation for mysql2-0.4.9
Installing ri documentation for mysql2-0.4.9
Parsing documentation for mysql2-cs-bind-0.0.6
Installing ri documentation for mysql2-cs-bind-0.0.6
Parsing documentation for jsonpath-0.8.7
Installing ri documentation for jsonpath-0.8.7
Parsing documentation for fluent-plugin-mysql-0.1.5
Installing ri documentation for fluent-plugin-mysql-0.1.5
Done installing documentation for mysql2, mysql2-cs-bind, jsonpath, fluent-plugin-mysql after 0 seconds
4 gems installed

结语

可能遇到的坑总结一下:

  • gem命令的时候,需要使用/usr/sbin/td-agent-gem,td-agent使用的是这个环境的gem依赖
  • ruby安装mysql的插件的时候,需要mysql-devel,否则会报错
  • nginx的log format可能根据自己的情况调整
  • source文件的tail需要该输入文件的目录权限是755

使用nginx ngx_http_referer_module模块配置防盗链

nginx referer简介

nginx模块ngx_http_referer_module通常用于阻挡来源非法的域名请求.我们应该牢记,伪装Referer头部是非常简单的事情,所以这个模块只能用于阻止大部分非法请求。我们应该记住,有些合法的请求是不会带referer来源头部的,所以有时候不要拒绝来源头部(referer)为空的请求.

图片防盗链配置示例如下

location ~ .*.(gif|jpg|jpeg|png|bmp|swf)$ {
valid_referers none blocked *.91linux.org 91linux.org server_names ~.google. ~.baidu.;
    if ($invalid_referer) {
    return 403;
    #rewrite ^/ https://www.91linux.org/403.jpg;
    }
}

以上所有来至91linux.org和域名中包含google和baidu的站点都可以访问到当前站点的图片,如果来源域名不在这个列表中,那么$invalid_referer等于1,在if语句中返回一个403给用户,这样用户便会看到一个403的页面,如果使用下面的rewrite,那么盗链的图片都会显示403.jpg。如果用户直接在浏览器输入你的图片地址,那么图片显示正常,因为它符合none这个规则.

示例解析

第一行:

location ~ .*.(gif|jpg|jpeg|png|bmp|swf)$

其中“gif|jpg|jpeg|png|bmp|swf”设置防盗链文件类型,自行修改,每个后缀用“|”符号分开!

第二行:

valid_referers none blocked *.91linux.org 91linux.org server_names ~.google. ~.baidu.;

就是白名单,允许文件链出的域名白名单,自行修改成您的域名! *.91linux.org这个指的是子域名,域名与域名之间使用空格隔开!baidu和google是搜索引擎。

第五行:

rewrite ^/ https://www.91linux.org/403.jpg;

这个图片是盗链返回的图片,也就是替换盗链网站所有盗链的图片。这个图片要放在没有设置防盗链的网站上,因为防盗链的作用,这个图片如果也放在防盗链网站上就会被当作防盗链显示不出来了,盗链者的网站所盗链图片会显示X符号;当然你也可以直接返回403

扩展

这样设置差不多就可以起到防盗链作用了,上面说了,这样并不是彻底地实现真正意义上的防盗链!

我们来看第三行:

valid_referers none blocked *.91linux.org 91linux.org server_names ~.google. ~.baidu.;

valid_referers 里多了“none blocked”

我们把“none blocked”删掉,改成

valid_referers *.91linux.org 91linux.org server_names ~.google. ~.baidu.;
  • none
    “Referer” 来源头部为空的情况

  • blocked
    “Referer”来源头部不为空,但是里面的值被代理或者防火墙删除了,这些值都不以http://或者https://开头.

nginx彻底地实现真正意义上的防盗链完整的代码应该是这样的:

location ~ .*.(gif|jpg|jpeg|png|bmp|swf)$ {
valid_referers *.91linux.org 91linux.org server_names ~.google. ~.baidu.;
    if ($invalid_referer) {
    return 403;
    #rewrite ^/ https://www.91linux.org/403.jpg;
    }
}

这样您在浏览器直接输入图片地址就不会再显示图片出来了,也不可能会再右键另存什么的。

我们应该记住,有些合法的请求是不会带referer来源头部的,所以有时候不要拒绝来源头部(referer)为空的请求.

Ubuntu系统安装Nginx web服务器

在QQ群很多朋友问阿里云服务器怎么安装LNMP环境,怎么把项目放到服务器上面去,在这里,我就从头开始教大家怎么在阿里云服务器安装LNMP环境。

在这之前,我们先要知道什么是LNMP。

  • L: 表示的是Linux系统, 包括Ubuntu、Centos但不限于以上两种的系统版本。
  • N: 表示的是Nginx,这是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP代理服务器。
  • M: 表示的是Mysql,Mysql是一个小型关系型数据库管理系统。
  • P: 表示的是PHP,PHP是一种在服务器端执行的嵌入HTML文档的脚本语言。

首先, 我们使用ssh连接到服务器:

$  ssh [email protected]

接着我们需要更新一下apt管理工具包:

$ apt update

更新完之后,使用包管理工具直接下载Nginx:

$ apt install -y nginx

这个时候可以看一下外面安装的nginx在放在哪里:

$ whereis nginx
nginx: /usr/sbin/nginx /etc/nginx /usr/share/nginx

现在我们先不管配置, 打开浏览器, 输入服务器IP地址访问, 如果出现:

Welcome to nginx!

那就证明已经安装成功了Nginx。

先简单说一下配置:

现在我使用的nginx版本是1.10.3

$ nginx -v
nginx version: nginx/1.10.3 (Ubuntu)

这个版本的配置在/etc/nginx/sites-enabled/default

$ cd /etc/nginx/sites-enabled/
$ ls
default

这个default里面就有我们一会要配置的东西.

还有就是关于Nginx的错误日志的路径/var/log/nginx, 很多时候我们找错误都是从错误日志中找到问题的解决方法,所以这个路径还是要记住的。

$ cd /var/log/nginx
$ ls
access.log  error.log