1. 安装nginx的依赖包
# 查看zlib是否安装
dpkg -l | grep zlib
# 解决依赖包openssl安装
sudo apt-get install openssl libssl-dev
# 解决依赖包pcre安装
sudo apt-get install libpcre3 libpcre3-dev
# 解决依赖包zlib安装
sudo apt-get install zlib1g-dev
2. 下载nginx
# 下载nginx
wget http://nginx.org/download/nginx-1.13.1.tar.gz
# 解压nginx
tar -xzvf nginx-1.13.1.tar.gz
# 重命名文件夹
mv nginx-1.13.1 nginx
# 移动文件夹到ubuntu常见软件目录下
mv nginx/ /usr/local/
3. 安装nginx
# 配置nginx
cd /usr/local/nginx
sudo ./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_realip_module
# 编译nginx
sudo make
# 安装nginx
sudo make install
4. 利用Ubuntu的仓库安装
sudo apt-get install nginx
5. 检查nginx是否安装成功
root 11299 1 0 00:26 ? 00:00:00 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
www-data 11300 11299 0 00:26 ? 00:00:00 nginx: worker process
www-data 11301 11299 0 00:26 ? 00:00:00 nginx: worker process
sun 11320 2632 0 00:26 pts/9 00:00:00 grep --color=auto nginx
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok # 配置文件所在位置
nginx: configuration file /etc/nginx/nginx.conf test is successful
6. Nginx操作提示:
sudo ufw status # 查看当前防火墙状态
sudo ufw enable # 命令来开发防火墙
sudo ufw disable # 命令来关闭防火墙
sudo /etc/init.d/nginx start #启动
sudo /etc/init.d/nginx stop #关闭
sudo /etc/init.d/nginx restart #重启
sudo dpkg -S nginx #查询所有nginx的位置
/etc/nginx # 所有的配置文件所在目录 包括nginx.conf
/etc/nginx/sites-available # 所有虚拟主机位置
/usr/sbin/nginx # 程序文件所在位置
/var/www/nginx-default # 默认的虚拟主机的目录设置位置(有的版本默认的虚拟
# 主机的目录设置在了/var/www, 请参考/etc/nginx/sites-available里的配置)
/var/log/nginx/error.log # 错误日志所在位置、
sudo /etc/init.d//nginx start # 启动脚本nginx
sudo /etc/init.d//nginx stop # 停止脚本nginx
cd /etc/ningx
sudo nginx -s reload
7. 修改Nginx默认端口号
server {
listen 8088; # 修改端口号
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
#gzip on;
server {
listen 80;
server_name manage.leyou.com;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
location / {
proxy_pass http://192.168.0.11:9001; #主机地址下的9001端口
proxy_connect_timeout 6000;
proxy_read_timeout 6000;
}
}
server {
listen 80;
server_name api.leyou.com;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
location / {
proxy_pass http://192.168.0.11:10010;
proxy_connect_timeout 6000;
proxy_read_timeout 6000;
}
}
8. 卸载nginx
sudo apt-get remove nginx nginx-common # 卸载删除除了配置文件以外的所有文件。
sudo apt-get purge nginx nginx-common # 卸载所有东东,包括删除配置文件。
sudo apt-get autoremove # 在上面命令结束后执行,主要是卸载删除Nginx的不再被使用的依赖包。
sudo apt-get remove nginx-full nginx-common #卸载删除两个主要的包。
sudo dpkg -S nginx # 查询所有nginx的位置,并逐一删除
sudo service nginx restart # 重启nginx 会显示没有,则卸载成功
注意事项:
-
由于我是使用的ubantu仓库进行的安装,因此安装的是最新的nginx,它与一般的有所不同,首先
nginx.conf
为只读状态,而且与内容有所不同,必须使用sudo gedit nginx.conf
命令打开配置文件,改后的配置文件如下:
user www-data;
worker_processes auto;
pid /run/nginx.pid;
events {
worker_connections 768;
# multi_accept on;
}
http {
server {
listen 80;
server_name manage.leyou.com;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
location / {
proxy_pass http://192.168.0.11:9001; #主机地址下的9001端口
proxy_connect_timeout 6000;
proxy_read_timeout 6000;
}
}
server {
listen 80;
server_name api.leyou.com;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
location / {
proxy_pass http://192.168.0.11:10010;
proxy_connect_timeout 6000;
proxy_read_timeout 6000;
}
}
##
# Basic Settings
##
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
# server_tokens off;
# server_names_hash_bucket_size 64;
# server_name_in_redirect off;
include /etc/nginx/mime.types;
default_type application/octet-stream;
##
# SSL Settings
##
ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
ssl_prefer_server_ciphers on;
##
# Logging Settings
##
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
##
# Gzip Settings
##
gzip on;
gzip_disable "msie6";
# gzip_vary on;
# gzip_proxied any;
# gzip_comp_level 6;
# gzip_buffers 16 8k;
# gzip_http_version 1.1;
# gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
##
# Virtual Host Configs
##
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
#mail {
# # See sample authentication script at:
# # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript
#
# # auth_http localhost/auth.php;
# # pop3_capabilities "TOP" "USER";
# # imap_capabilities "IMAP4rev1" "UIDPLUS";
#
# server {
# listen localhost:110;
# protocol pop3;
# proxy on;
# }
#
# server {
# listen localhost:143;
# protocol imap;
# proxy on;
# }
#}
参考:
- https://www.cnblogs.com/zhaoyingjie/p/6840616.html