CentOS7 编译安装Nginx

首先进入root目录创建pkg文件夹,安装软件包和解压包都会存在此目录下,保持服务器整洁

进入root目录下创建pkg文件夹

准备

# 进入pkg文件夹
cd pkg
# 下载nginx文件,版本去官网选择
wget http://nginx.org/download/nginx-1.16.1.tar.gz
# 解压
tar -zxvf nginx-1.16.1.tar.gz
# 进入nginx解压包
cd nginx-1.16.1

配置编译

下面会用到yum源,可以查看资料
https://www.runoob.com/linux/linux-yum.html

# 安装pcre库
yum -y install pcre-devel
# 安装openssl库
yum -y install openssl-devel

注意:如果没有安装上边两个扩展,在./configure编译的时候会报错,类似
./configure: error: the HTTP rewrite module requires the PCRE library.
You can either disable the module by using --without-http_rewrite_module
option, or install the PCRE library into the system, or build the PCRE library
statically from the source with nginx by using --with-pcre= option.
提示需要PCRE库,安装一下即可,其他报错也是一样解决

# 配置编译
./configure --user=nobody --group=nobody --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_gzip_static_module --with-http_realip_module --with-http_sub_module --with-http_ssl_module
注意: --prefix=[安装目录]

# 执行完成,提示一下信息,说明编译成功,如有error,就就去解决错误
Configuration summary
  + using system PCRE library
  + using system OpenSSL library
  + using system zlib library

  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"

# 编译
make
# 安装
make install

配置启动nginx

# 尝试启动
/usr/local/nginx/sbin/nginx
# 查看是都启动成功
ps aux | grep nginx

/*************其他快捷性配置****************/
# 创建一个nginx软链接
ln -s /usr/local/nginx/sbin/nginx /usr/bin/nginx

# 如需开机自启
vim /etc/rc.d/rc.local
添加:nginx start

常用命令
# 启动nginx
nginx start
# 重启
nginx -s reload
# 停止
nginx -s stop或者是通过kill nginx进程号
# 检测配置文件是否合法
nginx -t
# 查看版本
nginx –V

查看nginx是否可用

浏览器访问:服务器ip
注意:阿里云需要配置”实例安全组“80端口才有权限访问ip

image.png

至此已安装成功

后面文章还有php安装

你可能感兴趣的:(CentOS7 编译安装Nginx)