環(huán)境: 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依賴的庫
- 安裝gcc
yum -y install gcc - 安裝pcre
yum -y install pcre pcre-devel - 安裝zlib
yum -y install zlib zlib-devel - 安裝openssl
yum -y install openssl openssl-devel
安裝
sudo ./configure
sudo make
sudo make install
默認位置 /usr/local/nginx
如果安裝過程中出現文件目錄權限問題,百度即可解決.
2.系統(tǒng)中配置
1.在系統(tǒng)服務目錄里創(chuàng)建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
查看服務當前狀態(tài)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
創(chuàng)建目錄vhost
mkdir /usr/local/nginx/conf/vhost
創(chuàng)建.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
在外面訪問即可