云昴

【建站教程】第一章 搭建Nginx

| 【专业·学习】建站教程

参考教程

https://www.cnblogs.com/liujuncm5/p/6713784.html https://segmentfault.com/a/1190000013328653

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

安装nginx

cd /usr/local/src 该文件夹做为安装包存放点
wget http://nginx.org/download/nginx-1.13.8.tar.gz 可在官网获得最新
tar -zxvf nginx-1.13.8.tar.gz
cd nginx-1.13.8

添加nginx用户及用户组(可以省略)

groupadd nginx
useradd -r -g nginx nginx

编译nginx

./configure
--prefix=/usr/local/nginx
--sbin-path=/usr/local/nginx/sbin/nginx
--conf-path=/usr/local/nginx/nginx.conf
--pid-path=/usr/local/nginx/nginx.pid
--user=nginx(上步省略,可去掉)
--group=nginx(上步省略,可去掉)
--with-http_ssl_module
--with-http_flv_module
--with-http_mp4_module
--with-http_stub_status_module
--with-http_gzip_static_module
--http-client-body-temp-path=/var/tmp/nginx/client/
--http-proxy-temp-path=/var/tmp/nginx/proxy/
--http-fastcgi-temp-path=/var/tmp/nginx/fcgi/
--http-uwsgi-temp-path=/var/tmp/nginx/uwsgi/
--http-scgi-temp-path=/var/tmp/nginx/scgi/

编译问题中可能缺少依赖包,根据提示自行安装即可,编译成功进行安装

make && make install

配置nginx

在server加入一行

include       /usr/local/nginx/vhost/*.conf;

使得不同网站单独进行配置

添加环境变量

vi /etc/profile

最后加入

  • export PATH=$PATH:/usr/local/nginx/sbin

执行

source /etc/profile

安装小结

安装包存放点: /usr/local/src/

nginx配置文件: /usr/local/nginx/nginx.conf

项目配置目录: /usr/local/nginx/conf.d/

nginx日志目录: /usr/local/nginx/logs/

nginxpid文件: /usr/local/nginx/nginx.pid

nginx启动文件: /usr/local/nginx/sbin/nginx

启动nginx

nginx(配置完环境变量可使用)
/usr/local/nginx/sbin/nginx

停止nginx

nginx -s stop(配置完环境变量可使用)
/usr/local/nginx/sbin/ngin -s stop

重启nginx

nginx -s reload(配置完环境变量可使用)
/usr/local/nginx/sbin/nginx -s reload

systemctl相关命令

开启nginx服务 systemctl start nginx.service
停止nginx服务 systemctl stop nginx.service
重启nginx服务 systemctl restart nginx.service
查看nginx服务 systemctl status nginx.service
加入开机自启 systemctl enable nginx.service
退出开机自启 systemctl disable nginx.service
刷新服务配置 systemctl daemon-reload
查看已开启服务 systemctl list-unit –type=service

云昴