一.
gcc 安装
安装nginx需要先将官网下载的源码进行编译,编译依赖gcc环境,如果没有 gcc环境,则需要安装:
yum install gcc-c++
二.
PCRE pcre-devel 安装
PCRE(Perl Compatible Regular Expressions) 是一个Perl库,包括perl兼容的正则表达式库。nginx的 http模块使用 pcre来解析正则表达式,所以需要在 linux上安装 pcre库,pcre-devel是使用 pcre开发的一个二次开发库。nginx也需要此库。命令:
yum install -y pcre pcre-devel
三.
zlib 安装
zlib库提供了很多种压缩和解压缩的方式,nginx使用 zlib对 http包的内容进行 gzip,所以需要在 Centos上安装 zlib库。
yum install -y zlib zlib-devel
四.
OpenSSL 安装
OpenSSL是一个强大的安全套接字层密码库,囊括主要的密码算法、常用的密钥和证书封装管理功能及SSL协议,并提供丰富的应用程序供测试或其它目的使用。
nginx不仅支持 http协议,还支持 https(即在ssl协议上传输http),所以需要在Centos安装 OpenSSL库。
yum install -y openssl openssl-devel
五.使用wget
命令下载
wget -c https://nginx.org/download/nginx-1.14.2.tar.gz
tar -zxvf nginx-1.14.2.tar.gz cd nginx-1.14.2
配置
1.默认配置
./configure
自定义配置(不推荐)
./configure \ --prefix=/usr/local/nginx \ --conf-path=/usr/local/nginx/conf/nginx.conf \ --pid-path=/usr/local/nginx/conf/nginx.pid \ --lock-path=/var/lock/nginx.lock \ --error-log-path=/var/log/nginx/error.log \ --http-log-path=/var/log/nginx/access.log \ --with-http_gzip_static_module \ --http-client-body-temp-path=/var/temp/nginx/client \ --http-proxy-temp-path=/var/temp/nginx/proxy \ --http-fastcgi-temp-path=/var/temp/nginx/fastcgi \ --http-uwsgi-temp-path=/var/temp/nginx/uwsgi \ --http-scgi-temp-path=/var/temp/nginx/scgi
注:将临时文件目录指定为/var/temp/nginx,需要在/var下创建temp及nginx目录
make make install
ssl模块安装
[root@xxxxx nginx-1.14.2]# ./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
make
3.查找安装路径
whereis nginx
4.启动、停止nginx
cd /usr/local/nginx/sbin/ ./nginx ./nginx -s stop ./nginx -s quit ./nginx -s reload
./nginx
-s quit
:此方式停止步骤是待nginx进程处理任务完毕进行停止。
./nginx
-s stop
:此方式相当于先查出nginx进程id再使用kill命令强制杀掉进程。
5.查询nginx进程
ps aux|grep nginx
6.重启 nginx
先停止再启动(推荐):
对nginx进行重启相当于先停止再启动,即先执行停止命令再执行启动命令。如下:
./nginx -s quit ./nginx
重新加载配置文件:
当ngin
x的配置文件 nginx.conf修改后,要想让配置生效需要重启nginx,使用-s
reload
不用先停止 ngin
x再启动 nginx即可将配置信息在 nginx中生效,如下:
./nginx -s reload
启动成功后,在浏览器成功访问。
进入nginx 配置文件目录,编辑修改nginx配置文件
cd /usr/local/nginx/conf vim /usr/local/nginx/conf/nginx.conf
server { listen 80; server_name www.lihuakai.com.cn; #charset koi8-r; #access_log logs/host.access.log main; location / { root /var/www/lihuakai.com.cn; index index.html index.htm index.php; try_files $uri $uri/ /index.php?$query_string; } #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 /var/www/lihuakai.com.cn; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$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; #} }
服务管理
# 启动 /usr/local/nginx/sbin/nginx # 重启 /usr/local/nginx/sbin/nginx -s reload # 关闭进程 /usr/local/nginx/sbin/nginx -s stop # 平滑关闭nginx /usr/local/nginx/sbin/nginx -s quit # 查看nginx的安装状态, /usr/local/nginx/sbin/nginx -V
关闭防火墙,或者添加防火墙规则就可以测试了
firewall-cmd --state #查看防火墙状态 systemctl stop firewalld.service #停止firewall systemctl disable firewalld.service #禁止firewall开机启动
或者编辑配置文件
vi /etc/sysconfig/iptables
添加这样一条开放80端口的规则后保存
-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
重启服务
systemctl restart iptables.service #重启防火墙使配置生效 systemctl enable iptables.service #设置防火墙开机启动