本教程适合于在安装环境:Centos7-64位下安装nginx
安装nginx流程
准备工作
选择一个地方进行安装(选择合适的地方即可,下面为我的例子)。
mkdir home/oldpan/nginx
cd home/oldpan/nginx
安装gcc-c++编译环境,已经安装可以跳过。
yum install gcc-c++
下载相关组件。
[root@localhost nginx]# wget http://nginx.org/download/nginx-1.12.2.tar.gz ...省略相关信息 [root@localhost nginx]# wget http://www.openssl.org/source/openssl-fips-2.0.10.tar.gz ...省略相关信息 [root@localhost nginx]# wget http://zlib.net/zlib-1.2.11.tar.gz ...省略相关信息 [root@localhost nginx]# wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.40.tar.gz ...省略相关信息
安装步骤
安装openssl
[root@localhost nginx]# tar zxvf openssl-fips-2.0.10.tar.gz ...省略相关信息 [root@localhost nginx]# cd openssl-fips-2.0.10 [root@localhost openssl-fips-2.0.10]# ./config && make && make install ...省略相关信息
安装zlib、安装nginx与openssl类似,使用tar
命令解压相关压缩包,然后cd进入解压后的文件夹内进行编译,需要注意的是如果你要使用ssl证书的话,在编译nginx的时候除了./configure && make && make install
之外要加上--with-http_ssl_module
,也就是执行./configure && make && make install --with-http_ssl_module
。
配置启动Nginx
一般nginx的默认安装位置为/usr/local/nginx/
,使用whereis nginx
命令查看。
首先进行软连接:
ln -s /usr/local/nginx/sbin/nginx /usr/bin/nginx
nginx文件正确配置位置:
- nginx path prefix: “/usr/local/nginx”
- nginx binary file: “/usr/local/nginx/sbin/nginx”
- nginx modules path: “/usr/local/nginx/modules”
- nginx configuration prefix: “/usr/local/nginx/conf”
- nginx configuration file: “/usr/local/nginx/conf/nginx.conf”
- nginx pid file: “/usr/local/nginx/logs/nginx.pid”
- nginx error log file: “/usr/local/nginx/logs/error.log”
- nginx http access log file:”/usr/local/nginx/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”
启动nginx,执行下面的命令即可:
/usr/local/nginx/sbin/nginx
就可以看到nginx的欢迎页面。
如果正常启动,可以通过netstat -lnp
发现80端口使用者为nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 21127/nginx: master
第一次用服务器很有可能没开启防火墙,从而导致无法通过浏览器输入ip访问nginx欢迎页面,那么执行service iptables stop
关闭防火墙。
Nginx相关
nginx常用命令
1 /usr/local/nginx/sbin/nginx -t
测试当前配置文件的路径以及配置文件是否有效。
[root@localhost ~]# /usr/local/nginx/sbin/nginx -t nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
2 /usr/local/nginx/sbin/nginx -s reload
重新加载配置文件。
3 /usr/local/nginx/sbin/nginx -V
查看当前nginx版本以及相关组件。
[root@localhost ~]# /usr/local/nginx/sbin/nginx -V nginx version: nginx/1.12.2 built by gcc 4.8.5 20150623 (Red Hat 4.8.5-16) (GCC) built with OpenSSL 1.0.2k-fips 26 Jan 2017 TLS SNI support enabled configure arguments: --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
4 /usr/local/nginx/sbin/nginx -s stop(quit)
nginx停止。
nginx常见错误:
1 nginx: [emerg] no "events" section in configuration
在conf文件中添加:
events {
worker_connections 1024; ## Default: 1024
}
2 nginx: [emerg] "server" directive is not allowed here in /usr/local/nginx/conf/nginx.conf:45
换成这个形式:
http
{
server{}
}
3 Resource interpreted as Stylesheet but transferred with MIME type text/plain
将下面两句
include mime.types;
default_type application/octet-stream;
加到conf文件的http块中中,如果不设置css文件就会被当作text/plain类型发送到浏览器,另外记得注意要删除cookie来观察网站是否正确加载css
参考链接:
nginx配置certbot:https://juejin.im/post/5a31cbf76fb9a0450b6664ee
nginx启动SSL:https://www.cnblogs.com/piscesLoveCc/p/6120875.html
nginx,css文件权限问题:https://bbs.seafile.com/t/topic/6071/3
nginx,css,mime,https://blog.csdn.net/m0_37904728/article/details/78745243