当前位置: 首页 > Linux

nginx安装配置

时间:2023-04-06 05:32:29 Linux

nginx安装1.安装前sudoyuminstallyum-utils2.在服务器根目录下创建/etc/yum.repos.d/nginx.repo文件,编辑如下:[nginx-stable]name=nginxstablerepobaseurl=http://nginx.org/packages/centos/$releasever/$basearch/gpgcheck=1enabled=1gpgkey=https://nginx.org/keys/nginx_signing.keymodule_hotfixes=true[nginx-mainline]name=nginxmainlinerepobaseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/gpgcheck=1enabled=0gpgkey=https://nginx.org/keys/nginx_signing.keymodule_hotfixes=true3.sudoyuminstallnginx这样我们就完成了nginx的安装。这三步是官网要求的,没有理由;这样,我们安装nginx的配置就配置在/etc/nginx/default.conf2.nginx配置我们刚刚生成的nginx默认配置如下usernginx;//defaultuserworker_processes1;//cpukernelerror_log/var/log/nginx/error.logwarn;//打印错误日志路径pid/var/run/nginx.pid;//nginx运行启动后,这个flag会生成一个文件,记录nginx主进程的ID号。events{//设置最大连接数worker_connections1024;}http{include/etc/nginx/mime.types;//设置各种类型default_typeapplication/octet-stream;log_formatmain'$remote_addr-$remote_user[$time_local]"$request"''$status$body_bytes_sent"$http_referer"''"$http_user_agent""$http_x_forwarded_for"';//打印日志的方式,$符号标识变量access_log/var/log/nginx/access.logmain;//打印访问日志sendfileon;//指定是否使用sendfile系统调用传输文件。sendfile系统调用直接在两个文件描述符之间传递数据(完全在内核操作),从而避免了内核缓冲区和用户缓冲区之间的数据复制,操作效率非常高,称为零复制keepalive_timeout65;//默认连接时间include/etc/nginx/conf.d/*.conf;//默认配置包含在.conf文件中}上面最后一行我们也知道了,我们来看看默认配置详细说明:server{listen80;//默认监听80端口server_namelocalhost;access_log/var/log/nginx/host.access.logmain;//打印日志位置location/{root/usr/share/nginx/html;//配置访问文件夹indexindex.htmlindex.htm;//配置访问path}}部署下载xshell和winscp,xshell是连接服务器的工具,winscp是上传文件到服务器,但是我遇到了问题;首先我在阿里云服务器上购买了一台服务器,然后按照上面的操作安装搭建,但是我在nginx上改了配置文件并没有生效,因为我也是第一次接触nginx,对这方面不是很熟悉。我明白了,所以我直接用localhost访问(我真的是nt,既然上传到服务器,当然是用服务器访问),但是我还是停留在welcometonginx!这个很邪恶,然后在网上搜了各种资料。不是配置问题,而是我们在配置文件中写的端口指向80,但是阿里云需要设置安全组。设置好后,在URL中输入服务器地址,就可以看到我们改完后的index.html了。也算是一种挫折,但是我把congf中的root指向根文件夹位置下的index.html/{root/root;//配置访问文件夹indexindex.htmlindex.htm;//配置访问路径}然后打开网页显示403这到底是什么东西?没关系。我有百度。然后百度了一下,发现是权限问题。只需要将配置中的用户改为用户nginx即可;然后重启nginx(只要修改配置文件就重启),这样文件就可以正常显示了;常用命令systemctlstartnginx//启动nginxsystemctlstatusnginx.service//查看nginx状态systemctldisablefirewalld.service//关闭防火墙ps-ef|grepnginx//查看进程历史!(序列号)//使用历史命令systemctlrestartnginx.service//重启nginx