今天试着在centos7中安装nginx-1.16.1,由于是在docker中的centos7容器中安装,因nginx需要编译安装,所以需要先安装一些依赖的编译库,我一口气把需要编译依赖都安装了:

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

解压nginx压缩包

tar -zxvf nginx-1.16.1.tar.gz

进入nginx目录,配置nginx

./configure --prefix=/usr/local/nginx --pid-path=/usr/local/nginx/run --user=nginx --group=nginx --with-http_ssl_module --with-http_flv_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-http_image_filter_module --with-http_v2_module --with-debug

–with-http_v2_module 支持http2
执行编译并安装

make && make install

我给nginx配置的用户和组名为’nginx’,因此得创建名为’nginx’的用户和组

useradd -s /sbin/nologin -M nginx

通过id命令可以查看刚加入的用户和组

id nginx


启动nginx

/usr/local/nginx/sbin/nginx

查看端口查看是否启动,默认端口是:80,我配置的nginx的端口为:9000

netstat -tln


nginx默认配置文件路径:

/usr/local/nginx/conf/nginx.conf

nginx的生重启及验证配置文件命令

#重启
/usr/local/nginx/sbin/nginx -s reload

#停止
/usr/local/nginx/sbin/nginx -s stop

#测试配置文件
/usr/local/nginx/sbin/nginx -t

Nginx报错:nginx: [error] invalid PID number “” in “/run/nginx.pid” 解决方法

#需要先执行
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
#然后重启
/usr/local/nginx/sbin/nginx -s reload

设置开机启动,创建开机启动服务
vim /lib/systemd/system/nginx.service

[Unit]
Description=nginx #服务名称
After=network.target #服务类别,默认名

[Service]
Type=forking #后台运行的形式
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf #启动命令
ExecReload=/usr/local/nginx/sbin/nginx -s reload #重命令
ExecStop=/usr/local/nginx/sbin/nginx -s stop #停止命令
PrivateTmp=true  #表示给服务分配独立的临时空间

[Install]
WantedBy=root #指定启动用户

开启自动启动服务

systemctl enable nginx.service

其它相关命令

systemctl start nginx.service (启动nginx服务)
systemctl stop nginx.service (停止nginx服务)
systemctl restart nginx.service (重新启动服务)

systemctl enable nginx.service (设置开机自启动)
systemctl disable nginx.service (停止开机自启动)
systemctl status nginx.service (查看服务当前状态)

发表评论