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 编译安装
- [[email protected] sbin]# cd ~
- [[email protected] ~]# cd /usr/local/
- [[email protected] local]# mkdir nginx
- [[email protected] local]# cd nginx
- [[email protected] nginx]# wget -c https://nginx.org/download/nginx-1.12.0.tar.gz
- [[email protected] nginx]# tar -zxvf nginx-1.12.0.tar.gz
- [[email protected] nginx]# cd nginx-1.12.0
- # 安装到当前目录
- [[email protected] nginx]# ./configure
- [[email protected] nginx]# make
- [[email protected] nginx]# make install
到此为止环境已经安装好, 接下来启动 nginx 服务
- [[email protected] nginx]# cd /usr/local/nginx/sbin/
- [[email protected] 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
- [[email protected] ~]# cd /usr/local/nginx/conf
- [[email protected] 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
- [[email protected] conf]# cp nginx.conf nginx.conf.bak
- [[email protected] 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 保存退出
修改后重新加载下配置文件
- [[email protected] sbin]# cd /usr/local/nginx/sbin/
- [[email protected] sbin]# ./nginx -s reload
接着去阿里云 ECS 服务后台 - 安全组 - 新增规则 - 添加 81 端口, 在浏览器上就能访问了
来源: http://www.bubuko.com/infodetail-3402193.html