常用软件运维部署篇(七)–Nginx 1.6.3
1.1. 安装Nginx 1.6.3
1.1.1. 先安装gcc-c++编译器
yum install gcc-c++
yum install -y openssl openssl-devel
1.1.2. 再安装pcre包
yum install -y pcre pcre-devel
1.1.3. 再安装zlib包
yum install -y zlib zlib-devel
1.1.4. 安装nginx
将nginx-1.6.3.tar.gz解压后上传到 /home/lamp/soft/ 目录下
解压
tar zxvf nginx-1.6.3.tar.gz
编译安装
cd nginx-1.6.3
//关键所在:因为默认会安装在/usr/local/nginx,所以此处需要指明目的文件夹
./configure --prefix=/home/lamp/soft/nginx --with-http_stub_status_module --with-http_ssl_module
make
make install
查找安装路径
whereis nginx
1.1.5. 启动nginx
进入sbin目录,可以看到有一个可执行文件nginx,直接./nginx执行就OK了。
cd /home/lamp/soft/nginx/sbin/
./nginx
查看是否启动成功
ps -ef | grep nginx
然后在网页上访问自己的IP就可以了默认端口为80(出现如下欢迎界面就成功了!)
1.1.6. 配置开机自启动
1.1.6.1. 创建系统服务
在系统服务目录里创建nginx.service文件
vim /lib/systemd/system/nginx.service
写入内容如下:
[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
ExecStart=/home/lamp/soft/nginx/sbin/nginx
ExecReload=/home/lamp/soft/nginx/sbin/nginx -s reload
ExecStop=/home/lamp/soft/nginx/sbin/nginx -s quit
PrivateTmp=true
[Install]
WantedBy=multi-user.target
Description:描述服务
After:描述服务类别
[Service]服务运行参数的设置
Type=forking是后台运行的形式
ExecStart为服务的具体运行命令
ExecReload为重启命令
ExecStop为停止命令
PrivateTmp=True表示给服务分配独立的临时空间
注意:[Service]的启动、重启、停止命令全部要求使用绝对路径
[Install]运行级别下服务安装的相关设置,可设置为多用户,即系统运行级别为3
1.1.6.2. 设置开机启动
设置开机启动
systemctl enable nginx.service
#重新加载配置
systemctl daemon-reload
查看nginx状态
systemctl status nginx.service
1.1.7. 常用命令
启动nginx服务
systemctl start nginx.service
设置开机自启动
systemctl enable nginx.service
停止开机自启动
systemctl disable nginx.service
查看服务当前状态
systemctl status nginx.service
重新启动服务
systemctl status nginx.service
查看所有已启动的服务
systemctl list-units --type=service
1.1.8. Systemd 命令和 sysvinit 命令的对照表
1.1.9. 根据业务配置nginx.conf
user root;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include 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 logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 10000;
server_name localhost;
underscores_in_headers on;
location /lamp-web {
proxy_set_header Host $host:$server_port;
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_connect_timeout 60;
proxy_read_timeout 600;
proxy_send_timeout 600;
# 这个路径是存放打包后的 lamp-web 项目的路径
root /usr/local/data/projects/;
index index.html;
}
#切记, 在docker启动的nginx 必须将该文件中所有的 localhost 改成服务器的 内网ip(不能为 127.0.0.1)
location /api {
proxy_set_header Host $host:$server_port;
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_connect_timeout 60;
proxy_read_timeout 600;
proxy_send_timeout 600;
proxy_pass http:///8.152.123.31:8760/api;
}
#文件下载和访问 配置
location ^~ /file {
if ($request_uri ~* ^.*\/(.*)\.(apk|java|txt|doc|pdf|rar|gz|zip|docx|exe|xlsx|ppt|pptx|jpg|png)(\?fileName=([^&]+))$) {
add_header Content-Disposition "attachment;filename=$arg_attname";
}
root /usr/local/data/projects/uploadfile;
index index.html;
}
location /{
#Vue项目build后生成的dist文件路径,
root /usr/local/data/projects/lamp-web;
#为了解决刷新404,需要配置这个,vue-router官方给出的方案
try_files $uri $uri/ /index.html;
}
}
server {
listen 3100;
server_name localhost-pro;
underscores_in_headers on;
location /lamp-web-pro {
proxy_set_header Host $host:$server_port;
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_connect_timeout 60;
proxy_read_timeout 600;
proxy_send_timeout 600;
# 这个路径是存放打包后的 lamp-web 项目的路径
root /home/lamp/data/projects-pro4x/;
index index.html;
}
#切记, 在docker启动的nginx 必须将该文件中所有的 localhost 改成服务器的 内网ip(不能为 127.0.0.1)
location /api {
proxy_set_header Host $host:$server_port;
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_connect_timeout 60;
proxy_read_timeout 600;
proxy_send_timeout 600;
proxy_pass http://8.152.123.31:18760/api;
}
#文件下载和访问 配置
location ^~ /file {
if ($request_uri ~* ^.*\/(.*)\.(apk|java|txt|doc|pdf|rar|gz|zip|docx|exe|xlsx|ppt|pptx|jpg|png)(\?fileName=([^&]+))$) {
add_header Content-Disposition "attachment;filename=$arg_attname";
}
root /home/lamp/data/projects-pro4x/uploadfile;
index index.html;
}
location /{
#Vue项目build后生成的dist文件路径,
root /home/lamp/data/projects-pro4x/lamp-web-pro;
#为了解决刷新404,需要配置这个,vue-router官方给出的方案
try_files $uri $uri/ /index.html;
}
}
# 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;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}