在安装nginx前首先要确认系统中安装了gcc、pcre-devel、zlib-devel、openssl-devel
Linux下检查是否安装过某软件包:
- 以rpm包安装的,可以用 rpm -qa 看到,如果要查找某软件包是否安装,用 rpm -qa | grep “软件或者包的名字”
- 以deb包安装的,可以用 dpkg -l 看到。如果是查找指定软件包,用 dpkg -l | grep “软件或者包的名字”
- 以yum方法安装的,可以用 yum list installed 查找,如果是查找指定包,用 yum list installed |
grep “软件名或者包名”
示例:
yum list installed | grep "gcc"
输出结果:
)
已安装
如果没安装 需要安装一下
安装命令:
yum -y install gcc pcre-devel zlib-devel openssl openssl-devel
nginx下载地址:https://nginx.org/download/
下载“nginx-1.9.9.tar.gz”,移动到/usr/local/nginx/下
## 解压命令
tar -zxvf nginx-1.9.9.tar.gz -C /usr/local/nginx/
##进入nginx目录
cd /usr/local/nginx/nginx-1.9.9
## 配置
./configure --prefix=/usr/local/nginx# makemakemake install
配置完成输出结果:
开始执行make、make install命令
测试是否安装成功
# cd到刚才配置的安装目录/usr/loca/nginx/
./sbin/nginx -t
安装成功提示:
)
启动nginx
cd /usr/local/nginx/sbin
./nginx //启动nginx
重启nginx
./nginx -s reload
浏览器测试:
输入ip 端口
可能会出现403错误:
403错误需要配置 nginx.conf
在server里添加
autoindex on;
完整nginx.conf 文件
#user nobody;
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 80;
#开启检索
autoindex on;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
#文件所在位置
root /usr/image;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
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;
# }
#}
}