稷然如此

  • 首页
  • 文章分类
    • AI
    • Android
    • Java
    • Shell
    • Vue
    • C#
    • Python
    • 数据库
    • 组件
    • 其他
    • Game
  • 常用命令
    • Docker
    • Git
    • Linux
  • 操作系统
    • CentOS
    • Ubuntu
    • Windows
    • Kylin
  • 工具
    • IntelliJ IDEA
    • Visual Studio Code
稷然如此
不积跬步,无以至千里
  1. 首页
  2. 文章分类
  3. 组件
  4. 正文

CentOS 启动 Nginx

2023年7月11日 787点热度 0人点赞

1.下载

http://nginx.org/
选择的nginx-1.23.2版本

2.解压

上传centos并解压,本次解压目录在/home/install_package/nginx-1.23.2
tar -zxvf nginx-1.23.2.tar.gz

3.安装必要环境

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

4.在/opt创建目录

mkdir /opt/nginx

5.创建目录

在/opt/nginx目录下创建目录
mkdir /opt/nginx/nginx-1.23.2
mkdir /opt/nginx/temp
mkdir /opt/nginx/lock
mkdir /opt/nginx/logs

6.配置编译

自定义配置,需要手动完成第5步
cd /home/install_package/nginx-1.23.2
./configure \
--prefix=/opt/nginx/nginx-1.23.2 \
--with-http_stub_status_module --with-http_ssl_module \
--conf-path=/opt/nginx/nginx-1.23.2/conf/nginx.conf \
--pid-path=/opt/nginx/nginx-1.23.2/conf/nginx.pid \
--lock-path=/opt/nginx/lock/nginx.lock \
--error-log-path=/opt/nginx/logs/error.log \
--http-log-path=/opt/nginx/logs/access.log \
--with-http_gzip_static_module \
--http-client-body-temp-path=/opt/nginx/temp/client \
--http-proxy-temp-path=/opt/nginx/temp/proxy \
--http-fastcgi-temp-path=/opt/nginx/temp/fastcgi \
--http-uwsgi-temp-path=/opt/nginx/temp/uwsgi \
--http-scgi-temp-path=/opt/nginx/temp/scgi

7.编译并安装

cd /home/install_package/nginx-1.23.2
make && make install

8.创建ssl目录

进入/opt/nginx/nginx-1.23.2目录,创建ssl证书目录:mkdir cert

9.编辑配置文件

编辑confg/nginx.conf:
server {
	listen				443 ssl;
	server_name			www.xxx.com;
	ssl on;
	ssl_certificate			cert/server.pem;
	ssl_certificate_key		cert/server.key;
	ssl_session_timeout		5m;
	ssl_protocols			TLSv1 TLSv1.1 TLSv1.2;
	ssl_ciphers			ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
	ssl_prefer_server_ciphers	on;

	#charset koi8-r;
	#access_log logs/host.access.log  main;
	
	location / {
		tcp_nodelay		on;
		proxy_set_header	Host			$host;
		proxy_set_header	X-Real-IP		$remote_addr;
		proxy_set_header	X-Forwarded-For		$proxy_add_x_forwarded_for;
		proxy_pass		http://127.0.0.1:8080;
	}
}

server {
	listen				443 ssl;
	server_name 			app.xxx.com;
	ssl_certificate			cert/server-app.pem;
	ssl_certificate_key		cert/server-app.key;
	ssl_session_timeout		5m;
	ssl_protocols			TLSv1 TLSv1.1 TLSv1.2;
	ssl_ciphers			ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
	ssl_prefer_server_ciphers	on;

	#charset koi8-r;

	#access_log  logs/host.access.log  main;

	location / {
		tcp_nodelay		on;
		proxy_set_header	Host			$host;
		proxy_set_header	X-Real_IP		$remote_addr;
		proxy_set_header	X-Forwarded-For		$proxy_add_x_forwarded_for;
		# 以上三行目的是将代理服务器收到的用户信息传到真是服务器上
		proxy_pass		http://xxx:ooo;
	}
}

10.手动启动

cd /opt/nginx/nginx-1.23.2/sbin
./nginx -c /usr/local/nginx/conf/nginx.conf

11.手动停止

此方式停止步骤是待nginx进程处理任务完毕进行停止。
./nginx -s stop

12.手动退出

此方式相当于先查出nginx进程id再使用kill命令强制杀掉进程。
./nginx -s quit

13.手动重新加载配置

./nginx -s reload

14.常见问题处理

启动时报80端口被占用:nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
解决办法:安装net-tool 包:
yum install net-tools

15.手动重启

推荐使用quit
./nginx -s quit
./nginx

16.开机自启动

1.进入init.d目录
cd /etc/init.d
2.创建服务文件
vim nginx
3.编写脚本
#!/bin/bash
#chkconfig: 2345 63 37
#description: nginx service
#processname: nginx-1.23.2
export NGINX_HOME=/opt/nginx/nginx1.23.2
case $1 in
			start)
					$NGINX_HOME/sbin/nginx
					echo "nginx is started"
					;;
			stop)
					$NGINX_HOME/sbin/nginx -s quit
					echo "nginx is stopped"
					;;
			restart)
					$NGINX_HOME/sbin/nginx -s quit
					echo "nginx is stopped"
					sleep 1
					$NGINX_HOME/sbin/nginx
					echo "nginx is started"
					;;
			*)
			echo "start|stop|restart"
			;;
esac
exit 0
4.修改文件权限:
chmod 777 nginx
5.添加或删除服务
添加:
chkconfig --add nginx
删除:
chkconfig --del nginx
6.启动、停止或重启服务
启动:
service nginx start
停止:
service nginx stop
重启:
service nginx restart
7.设置开启或关闭开机启动
开启:
chkconfig nginx on
关闭:
chkconfig nginx off
8.查看es进程,验证是否已启动
ps -ef | grep nginx
标签: CentOS Nginx
最后更新:2023年7月11日

Akim

犇 骉 Java、C#、Python、Go、Android、MiniProgram、Bootstrap、Vue2

点赞
< 上一篇
下一篇 >
文章目录
  • 1.下载
  • 2.解压
  • 3.安装必要环境
  • 4.在/opt创建目录
  • 5.创建目录
  • 6.配置编译
  • 7.编译并安装
  • 8.创建ssl目录
  • 9.编辑配置文件
  • 10.手动启动
  • 11.手动停止
  • 12.手动退出
  • 13.手动重新加载配置
  • 14.常见问题处理
  • 15.手动重启
  • 16.开机自启动

Copyright © 2025 aianran.com All Rights Reserved.

免责申明 | 隐私政策 | 服务条款 | 关于我们

黔ICP备2023008200号-1

贵公网安备 52010202003594号