Nginx在Linux上的部署详解


nginx依赖包安装

先安装依赖包

1.gcc安装:安装 nginx 需要先将官网下载的源码进行编译,编译依赖 gcc 环境,如果没有 gcc 环境,则需要安装
2.PCRE(Perl Compatible Regular Expressions) 是一个Perl库,包括 perl 兼容的正则表达式库。
nginx 的 http 模块使用 pcre 来解析正则表达式,所以需要在 linux 上安装 pcre 库,pcre-devel 是使用 pcre 开发的一个二次开发库。nginx也需要此库
3.zlib库提供了很多种压缩和解压缩的方式, nginx 使用 zlib 对 http 包的内容进行 gzip ,所以需要在 Centos 上安装 zlib 库。
4.OpenSSL 是一个强大的安全套接字层密码库,囊括主要的密码算法、常用的密钥和证书封装管理功能及 SSL 协议,并提供丰富的应用程序供测试或其它目的使用。
nginx 不仅支持 http 协议,还支持 https(即在ssl协议上传输http),所以需要在 Centos 安装 OpenSSL 库。

yum install -y gcc-c++ yum install -y pcre pcre-devel yum install -y zlib zlib-devel yum install -y openssl openssl-devel

nginx下载与安装

直接下载.tar.gz安装包,下载地址:https://nginx.org/en/download.html
把下载的包放到/usr/local/nginx目录,然后解压安装到nginx-1.12.0当前目录,make编译安装

[root@yoyo sbin]# cd ~ [root@yoyo ~]# cd /usr/local/ [root@yoyo local]# mkdir nginx [root@yoyo local]# cd nginx [root@yoyo nginx]# wget -c https://nginx.org/download/nginx-1.12.0.tar.gz [root@yoyo nginx]# tar -zxvf nginx-1.12.0.tar.gz [root@yoyo nginx]# cd nginx-1.12.0 # 安装到当前目录 [root@yoyo nginx]# ./configure [root@yoyo nginx]# make [root@yoyo nginx]# make install

到此为止环境已经安装好,接下来启动nginx服务

[root@yoyo nginx]# cd /usr/local/nginx/sbin/ [root@yoyo nginx]# ./nginx

启动服务后,nginx默认是在80端口启动的,在浏览器输入http://47.104.x.x:80/ (80端口默认可以省略),能正常访问到页面,说明服务启动成功

 

相关指令

先cd到/usr/local/nginx/sbin/

1.启动服务

./nginx

2.停止服务,此方式停止步骤是待nginx进程处理任务完毕进行停止。

./nginx -s stop

3.退出服务,此方式相当于先查出nginx进程id再使用kill命令强制杀掉进程。

./nginx -s quit

4.重新加载,当 ngin x的配置文件 nginx.conf 修改后,要想让配置生效需要重启 nginx,
使用-s reload不用先停止 ngin x再启动 nginx 即可将配置信息在 nginx 中生效

./nginx -s reload

5.查询nginx进程

ps aux|grep nginx

开机自启动

在系统服务目录里创建nginx.service文件

vim /lib/systemd/system/nginx.service

内容如下
[Unit]:服务的说明
Description:描述服务
After:描述服务类别
[Service]服务运行参数的设置
Type=forking是后台运行的形式
ExecStart为服务的具体运行命令
ExecReload为重启命令
ExecStop为停止命令
PrivateTmp=True表示给服务分配独立的临时空间
注意:[Service]的启动、重启、停止命令全部要求使用绝对路径
[Install]运行级别下服务安装的相关设置,可设置为多用户,即系统运行级别为3

:wq保存退出。

[Unit] Description=nginx After=network.target  [Service] Type=forking ExecStart=/usr/local/nginx/sbin/nginx ExecReload=/usr/local/nginx/sbin/nginx -s reload ExecStop=/usr/local/nginx/sbin/nginx -s quit PrivateTmp=true  [Install] WantedBy=multi-user.target

1.设置开机自启动

systemctl enable nginx.service

2.停止开机自启动

systemctl disable nginx.service

3.启动服务

systemctl start nginx.service 

4.重新启动服务

systemctl restart nginx.service 

5.查看所有已启动的服务

systemctl list-units --type=service

修改nginx启动端口

如果80端口之前已经使用过了,可以修改nginx的服务端口,先cd到/usr/local/nginx/conf目录
为了保险起见,编辑前先备份下原来的文件:cp nginx.conf nginx.conf.bak

[root@yoyo ~]# cd /usr/local/nginx/conf [root@yoyo conf]# ll total 60 -rw-r--r-- 1 root root 1077 Jan 8 14:16 fastcgi.conf -rw-r--r-- 1 root root 1077 Jan 8 14:16 fastcgi.conf.default -rw-r--r-- 1 root root 1007 Jan 8 14:16 fastcgi_params -rw-r--r-- 1 root root 1007 Jan 8 14:16 fastcgi_params.default -rw-r--r-- 1 root root 2837 Jan 8 14:16 koi-utf -rw-r--r-- 1 root root 2223 Jan 8 14:16 koi-win -rw-r--r-- 1 root root 3957 Jan 8 14:16 mime.types -rw-r--r-- 1 root root 3957 Jan 8 14:16 mime.types.default -rw-r--r-- 1 root root 2656 Jan 8 14:16 nginx.conf -rw-r--r-- 1 root root 2656 Jan 8 14:16 nginx.conf.default -rw-r--r-- 1 root root 636 Jan 8 14:16 scgi_params -rw-r--r-- 1 root root 636 Jan 8 14:16 scgi_params.default -rw-r--r-- 1 root root 664 Jan 8 14:16 uwsgi_params -rw-r--r-- 1 root root 664 Jan 8 14:16 uwsgi_params.default -rw-r--r-- 1 root root 3610 Jan 8 14:16 win-utf [root@yoyo conf]# cp nginx.conf nginx.conf.bak [root@yoyo conf]# vim nginx.conf

vim打开后,找到服务端口listen 80这段,输入键盘上i键后编辑,改成81

    server { listen 80; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / { root html; index index.html index.htm; }

编辑完成后按Esc键,输入:wq保存退出

修改后重新加载下配置文件

[root@yoyo sbin]# cd /usr/local/nginx/sbin/ [root@yoyo sbin]# ./nginx -s reload

接着去阿里云ECS服务后台-安全组-新增规则-添加81端口,在浏览器上就能访问了


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM