Nginx安装及其简单配置

2018-01-09  本文已影响41人  王诛魔Phy

环境: centos7

1.下载安装

下载nginx-1.12.0,官网
sudo wget http://nginx.org/download/nginx-1.12.0.tar.gz

解压
sudo tar -zxvf nginx-1.12.0.tar.gz

其他的库,nginx依赖的库

安装

sudo ./configure
  
sudo make
  
sudo make install

默认位置 /usr/local/nginx

如果安装过程中出现文件目录权限问题,百度即可解决.

2.系统中配置

1.在系统服务目录里创建nginx.service文件

vim /lib/systemd/system/nginx.service
[Unit]
Description=nginx
After=network.target
  
[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true
  
[Install]
WantedBy=multi-user.target

2.设置开机启动

systemctl enable nginx.service

3.其他命令

启动nginx服务systemctl start nginx.service

设置开机自启动systemctl enable nginx.service

停止开机自启动systemctl disable nginx.service

查看服务当前状态systemctl status nginx.service

重新启动服务systemctl restart nginx.service

查看所有已启动的服务 systemctl list-units --type=service

3.nginx.conf的配置

[root@wangzhumo hooks]# cd /usr/local/nginx/
[root@wangzhumo nginx]# ll
total 96
drwx------ 2 root root  4096 Dec 24 20:57 client_body_temp
drwxr-xr-x 3 root root  4096 Dec 24 23:24 conf
drwx------ 2 root root  4096 Dec 24 20:57 fastcgi_temp
drwxr-xr-x 2 root root  4096 Dec 23 10:38 html
drwxr-xr-x 2 root root  4096 Dec 24 22:43 logs
-rw-r--r-- 1 root root 53861 Dec 25 13:37 on
drwx------ 2 root root  4096 Dec 24 20:57 proxy_temp
drwxr-xr-x 2 root root  4096 Dec 23 10:38 sbin
drwx------ 2 root root  4096 Dec 24 20:57 scgi_temp
drwx------ 2 root root  4096 Dec 24 20:57 uwsgi_temp

其中/conf存放配置文件/conf/nginx.conf
vim /conf/nginx.conf

什么都不要动,在nginx.conf第一条server配置下面写入:
include vhost/*.conf;

 }
 #此处是第一条可用的server的结束

    #加入这句,其实是把其他地方配置的配置文件加载到里面来
    #主要是为了方便管理,修改
    include vhost/*.conf;
    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

3.1 /vhost/.conf的配置*

当前目录到
cd /usr/local/nginx/conf

创建目录vhost
mkdir /usr/local/nginx/conf/vhost

创建.conf文件(我只用到hexo的配置,所以只有一个配置文件)
vim /usr/local/nginx/conf/vhost/hexo.conf

server
{
    listen 80;
    #listen [::]:80;
    server_name www.phyooos.top phyooos.top;
    index index.html index.htm index.php default.html default.htm default.php;
    #web root path
    root  /home/wzm/hexo;

    #error_page   404   /404.html;
    location ~ .*\.(ico|gif|jpg|jpeg|png|bmp|swf)$
    {
        access_log   on;
        expires      1d;
    }

    location ~ .*\.(js|css|txt|xml)?$
    {
        access_log   on;
        expires      12h;
    }

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

    access_log  /usr/local/nginx/logs/hexo.log  combined;
}

4.重启
systemctl reload nginx

在外面访问即可

上一篇 下一篇

猜你喜欢

热点阅读