1.nginx安装所须要的工具vi、wget、gccphp
vi工具是Linux系统自带的无需安装,用于修改配置文件时使用html
wget是文件下载工具,若是可以把安装包下载到本地再上传到目标服务器也能够,安装不安装就不那么必要了nginx
yum install -y wget #安装wget工具
gcc源码安装过程当中编译的必要工具c++
yum install -y make cmake gcc gcc-c++ #安装gcc工具
2.下载nginx安装包正则表达式
wget http://nginx.org/download/nginx-1.9.8.tar.gz #读者可自行把安装名去掉到download网页自行选择版本
3.安装依赖包vim
在安装以前可先查看是否安装了如下安装包浏览器
nginx安装所须要的依赖包pcre pcre-devel zlib zlib-devel openssl openssl-devel服务器
yum list installed | grep ‘依赖包’session
yum install -y pcre pcre-devel #安装pcre和pcre-devel的依赖包 yum install -y zlib zlib-devel #安装zlib和zlib-devel的依赖包 yum install -y openssl openssl-devel #安装openssl和openssl-devel的依赖包
4.解压安装包nginx-1.9.8.tar.gz到/usr/local目录下app
tar -zxvf nginx-1.9.8.tar.gz -C /usr/local
5.进行configure配置
cd /usr/local/nginx-1.9.8 #切换到nginx-1.9.8目录下
./configure --prefix=/usr/local/nginx #设置软件安装路径
6.编译并安装
make && make install
7.启动nginx
cd /usr/local/nginx/sbin
./nginx
若是发生如下状况,说明80端口已经被占用
[root@vm07 sbin]# ./nginx nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use) nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use) nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use) nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use) nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use) nginx: [emerg] still could not bind()
能够考虑将80端口占用的程序kill掉,或者修改配置文件/usr/local/nginx/conf下的nginx.conf的配置文件
把80端口改成别的端口,例如81端口,从新启动nginx程序
8.启动nginx,启动完以后检查nginx是否已经正常启动,看到以下信息则说明正常启动
[root@vm07 sbin]# ps -ef | grep nginx root 7702 1 0 15:09 ? 00:00:00 nginx: master process ./nginx nobody 7703 7702 0 15:09 ? 00:00:00 nginx: worker process root 7705 2709 0 15:09 pts/0 00:00:00 grep --color=auto nginx
要关闭nginx可以使用
./nginx -s stop #中止nginx服务
./nginx -s reload #从新热启动nginx服务
9.配置防火墙
firewall-cmd --zone=public --add-port=80/tcp --permanent
firewall-cmd --reload
10.测试nginx
打开浏览器,地址栏输入服务器的ip地址:端口号,回车
11.学习nginx的配置文件
vim /usr/local/nginx/conf/nginx.conf
#user nobody; #开启进程数 <=CPU数 worker_processes 1; #错误日志保存位置 #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #进程号保存文件 #pid logs/nginx.pid; #每一个进程最大链接数(最大链接=链接数x进程数)每一个worker容许同时产生多少个连接,默认1024 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压缩 #gzip on; server { #监听端口,默认是80端口 listen 80; #监听域名 server_name localhost; #charset koi8-r; #nginx访问日志放在logs/host.access.log下,而且使用main格式(还能够自定义格式) #access_log logs/host.access.log main; #若是没有location更明确的匹配访问路径的话,访问请求都会被该location处理。 location / { #root指定nginx的根目录为/usr/local/nginx/html root html; #默认访问文件,欢迎页先去html目录下找index.html,若是找不到再去找index.htm index index.html index.htm; } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # #错误页面及其返回地址,错误码为500、50二、50三、504都会返回50.html错误页面。 error_page 500 502 503 504 /50x.html; #location后面是"="的话,说明是精确匹配 location = /50x.html { root html; } # proxy the PHP scripts to Apache listening on 127.0.0.1:80 # #location ~ \.php$ { # proxy_pass http://127.0.0.1; #} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # #location ~ \.php$ { # root html; # fastcgi_pass 127.0.0.1:9000; # fastcgi_index index.php; # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; # include fastcgi_params; #} # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #location ~ /\.ht { # deny all; #} } # 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; # } #} }
配置文件里能够添加多个server,server监听的端口不一样,能够根据须要让nginx代理多个端口,当访问某个端口的时候,指定去作某些事情。我这里添加了一个server,这个server监听的端口为1234,server_name我指定为了test.com,也就是域名为test.com,当访问1234端口时会自动导航到/usr/local/nginx/tester/tester111.html页面,以下所示。
#user nobody; #开启进程数 <=CPU数 worker_processes 1; #错误日志保存位置 #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #进程号保存文件 #pid logs/nginx.pid; #每一个进程最大链接数(最大链接=链接数x进程数)每一个worker容许同时产生多少个连接,默认1024 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压缩 #gzip on; server { #监听端口 listen 80; #监听域名 server_name localhost; #charset koi8-r; #nginx访问日志放在logs/host.access.log下,而且使用main格式(还能够自定义格式) #access_log logs/host.access.log main; #若是没有location更明确的匹配访问路径的话,访问请求都会被该location处理。 location / { #root指定nginx的根目录为/usr/local/nginx/html root html; #默认访问文件,欢迎页先去html目录下找index.html,若是找不到再去找index.htm index index.html index.htm; } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # #错误页面及其返回地址,错误码为500、50二、50三、504都会返回50.html错误页面。 error_page 500 502 503 504 /50x.html; #location后面是"="的话,说明是精确匹配 location = /50x.html { root html; } server { listen 1234; server_name test.com; location / { #正则表达式匹配uri方式:在/usr/local/nginx/tester下 创建一个tester111.html 而后使用正则匹配 root tester; index tester111.html; } } } }