Linux 安装nginx
环境安装步骤
下载
下载地址:http://nginx.org/download/,下载nginx-1.9.9
上传服务器解压
将下载的nignx源码包上传到linux服务器上,解压
xx@x-x-x-xx:~$tar xvf nginx-1.9.9.tar.gz
安装
安装编译工具及库文件
yum -y install make zlib zlib-devel gcc-c++ libtool openssl openssl-devel
app@x-x-x-xx:~$sudo yum -y install make zlib zlib-devel gcc-c++ libtool openssl openssl-devel
Loaded plugins: product-id, search-disabled-repos, subscription-manager
This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
Repodata is over 2 weeks old. Install yum-cron? Or run: yum makecache fast
……
Installed:
gcc-c++.x86_64 0:4.8.5-44.el7 libtool.x86_64 0:2.4.2-22.el7_3
Dependency Installed:
autoconf.noarch 0:2.69-11.el7 automake.noarch 0:1.13.4-3.el7 libstdc++-devel.x86_64 0:4.8.5-44.el7 m4.x86_64 0:1.4.16-10.el7 perl-Data-Dumper.x86_64 0:2.145-3.el7 perl-Test-Harness.noarch 0:3.28-3.el7
Updated:
make.x86_64 1:3.82-24.el7 openssl.x86_64 1:1.0.2k-19.el7 openssl-devel.x86_64 1:1.0.2k-19.el7 zlib.x86_64 0:1.2.7-18.el7 zlib-devel.x86_64 0:1.2.7-18.el7
Dependency Updated:
cpp.x86_64 0:4.8.5-44.el7 gcc.x86_64 0:4.8.5-44.el7 libgcc.x86_64 0:4.8.5-44.el7 libgomp.x86_64 0:4.8.5-44.el7 libstdc++.x86_64 0:4.8.5-44.el7 openssl-libs.x86_64 1:1.0.2k-19.el7
Complete!
配置
进入nginx解压后的根目录,配置
app@x-x-x-xx:~/nginx-1.9.9$./configure --prefix=/home/app/nginx-1.9.9
checking for OS
+ Linux 3.10.0-327.el7.x86_64 x86_64
checking for C compiler ... found
+ using GNU C compiler
+ gcc version: 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC)
……
nginx path prefix: "/home/app/nginx-1.9.9"
nginx binary file: "/home/app/nginx-1.9.9/sbin/nginx"
nginx configuration prefix: "/home/app/nginx-1.9.9/conf"
nginx configuration file: "/home/app/nginx-1.9.9/conf/nginx.conf"
nginx pid file: "/home/app/nginx-1.9.9/logs/nginx.pid"
nginx error log file: "/home/app/nginx-1.9.9/logs/error.log"
nginx http access log file: "/home/app/nginx-1.9.9/logs/access.log"
nginx http client request body temporary files: "client_body_temp"
nginx http proxy temporary files: "proxy_temp"
nginx http fastcgi temporary files: "fastcgi_temp"
nginx http uwsgi temporary files: "uwsgi_temp"
nginx http scgi temporary files: "scgi_temp"
编译安装
make && make install
xx@x-x-x-x:~/nginx-1.9.9$make && make install
这里编译安装碰到一个异常,原因是/home/app/nginx-1.9.9/logs中的logs目录没有,创建logs目录问题解决
make[1]: Entering directory `/home/app/nginx-1.9.9'
sed -e "s|%%PREFIX%%|/home/app/nginx-1.9.9|" \
-e "s|%%PID_PATH%%|/home/app/nginx-1.9.9/logs/nginx.pid|" \
-e "s|%%CONF_PATH%%|/home/app/nginx-1.9.9/conf/nginx.conf|" \
-e "s|%%ERROR_LOG_PATH%%|/home/app/nginx-1.9.9/logs/error.log|" \
< man/nginx.8 > objs/nginx.8
make[1]: Leaving directory `/home/app/nginx-1.9.9'
make -f objs/Makefile install
make[1]: Entering directory `/home/app/nginx-1.9.9'
test -d '/home/app/nginx-1.9.9' || mkdir -p '/home/app/nginx-1.9.9'
test -d '/home/app/nginx-1.9.9/sbin' || mkdir -p '/home/app/nginx-1.9.9/sbin'
test ! -f '/home/app/nginx-1.9.9/sbin/nginx' || mv '/home/app/nginx-1.9.9/sbin/nginx' '/home/app/nginx-1.9.9/sbin/nginx.old'
cp objs/nginx '/home/app/nginx-1.9.9/sbin/nginx'
test -d '/home/app/nginx-1.9.9/conf' || mkdir -p '/home/app/nginx-1.9.9/conf'
cp conf/koi-win '/home/app/nginx-1.9.9/conf'
cp: ‘conf/koi-win’ and ‘/home/app/nginx-1.9.9/conf/koi-win’ are the same file
make[1]: *** [install] Error 1
make[1]: Leaving directory `/home/app/nginx-1.9.9'
make: *** [install] Error 2
检查配置
- 正确配置
xx@x-x-x-x:cd /home/app/nginx-1.9.9
xx@x-x-x-x: ls
conf html logs sbin
xx@x-x-x-x: cd sbin/
xx@x-x-x-x:~/nginx-1.9.9/sbin$./nginx -t
nginx: the configuration file /home/app/nginx-1.9.9/conf/nginx.conf syntax is ok
nginx: configuration file /home/app/nginx-1.9.9/conf/nginx.conf test is successful
- 错误配置,该异常和预编码安装异常是同一个,创建logs目录即解决
xx@x-x-x-x:~/nginx-1.9.9/sbin$./nginx -t
nginx: [alert] could not open error log file: open() "/home/app/nginx-1.9.9/logs/error.log" failed (2: No such file or directory)
nginx: the configuration file /home/app/nginx-1.9.9/conf/nginx.conf syntax is ok
2020/11/17 16:35:24 [emerg] 23911#0: open() "/home/app/nginx-1.9.9/logs/nginx.pid" failed (2: No such file or directory)
nginx: configuration file /home/app/nginx-1.9.9/conf/nginx.conf test failed
启动nginx
没有日志输出说明启动成功
xx@x-x-x-x:~/nginx-1.9.9/sbin$pwd
/home/app/nginx-1.9.9/sbin
xx@x-x-x-x:~/nginx-1.9.9/sbin$./nginx
- 异常
nginx 默认的端口是80,如果端口被占用了需要在
/home/app/nginx-1.9.9/conf/ 下修改nginx.conf文件中server 下listen 为自己想要的端口,这里修改为8888
server {
listen 8888;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
访问验证
访问 http://xx.x.x.x:8888
常用命令
重新载入配置文件
xx@x-x-x-x:~/nginx-1.9.9$./sbin/nginx -s reload -c ./conf/nginx.conf
xx@x-x-x-x:~/nginx-1.9.9$
重启 Nginx
xx@x-x-x-x:~/nginx-1.9.9$./sbin/nginx -s reopen
xx@x-x-x-x:~/nginx-1.9.9$
停止 Nginx
xx@x-x-x-x:~/nginx-1.9.9$./sbin/nginx -s stop
xx@x-x-x-x:~/nginx-1.9.9$