0、安装依赖包(必须执行,否则报错)

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

1、在/usr/local/下创建文件nginx文件

mkdir /usr/local/nginx

2、下载nginx

进入nginx目录

cd /usr/local/nginx

下载,去官网获取tar.gz包的链接地址

wget http://nginx.org/download/nginx-1.8.1.tar.gz

3、解压

tar -zxvf nginx-1.8.1.tar.gz

删除下载的安装包

rm -f nginx-1.8.1.tar.gz

4、使用nginx默认配置

进入nginx

cd nginx-1.8.1/

执行以下命令

./configure

5、编译安装

make
make install

6、查找安装路径

whereis nginx

结果

nginx: /usr/local/nginx

查看nginx的版本:

sbin/nginx -V

7、启动nginx

进入以下目录

cd /usr/local/nginx/sbin

启动

./nginx

关闭nginx

./nginx -s stop 

8、查看是否启动成功

ps -ef | grep nginx

 结果

root      1228     1  0 10:58 ?        00:00:00 nginx: master process ./nginx
nobody    1229  1228  0 10:58 ?        00:00:00 nginx: worker process
root      1256 20952  0 10:58 pts/0    00:00:00 grep --color=auto nginx

9、浏览器访问:http://ip/

10、将打包好的dist中的文件放入html中

11、修改nginx的配置文件

cd /usr/local/nginx/conf
vim nginx.conf

修改的配置如下:

 12、修改配置文件后,重启nginx

sbin/nginx -s reload

注意:修改配置文件后一定要执行此命令,不用关闭nginx后再启动nginx。

 如果执行如下命令报错

./nginx -s reload

报错如下:

nginx: [alert] kill(30372, 1) failed (3: No such process)

原因大概是因为重新编译的 nginx 后,新的 nginx 可执行文件没有找到对应的 nginx.conf 目录。所以我们需要手动的指定一下。

/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

13、查看nginx的日志信息

cat ../logs/access.log
cat ../logs/error.log

服务器上日志access.log记录所有客户端请求的信息,error.log错误日志仅仅记录服务器报错信息。

access.log中部分日志如下:

49.222.126.123 - - [23/Nov/2021:11:11:56 +0800] "POST /api/login HTTP/1.1" 499 0 "http://ip/" "Mozilla/5.0 
(Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.121 Safari/537.36 HBPC/11.0.5.301
"

可以看出来,状态码未499。

14、使用curl命令测试是否可以访问网站

curl http://ip:端口

Ps:由于安装linux的时候很多时候是没有安装桌面的,也意味着没有浏览器,因此这个方法也经常用于测试一台服务器是否可以到达一个网站。

15、杀死nginx

查看运行的nginx

ps -ef | grep nginx

结果如下:

[root@hecs-140905 ~]# ps -ef | grep nginx
root       825     1  0 Jun29 ?        00:00:00 nginx: master process ./nginx
nobody     826   825  0 Jun29 ?        00:00:00 nginx: worker process
root      6149 28029  0 20:30 pts/0    00:00:00 grep --color=auto nginx
root     19449 19104  0 Jun28 ?        00:00:00 runsv nginx
root     19452 19449  0 Jun28 ?        00:00:00 svlogd -tt /var/log/gitlab/nginx
root     19468 19449  0 Jun28 ?        00:00:00 nginx: master process /opt/gitlab/embedded/sbin/nginx -p /var/opt/gitlab/nginx
polkitd  19535 19468  0 Jun28 ?        00:00:00 nginx: worker process
polkitd  19536 19468  0 Jun28 ?        00:00:00 nginx: worker process
polkitd  19537 19468  0 Jun28 ?        00:00:05 nginx: worker process
polkitd  19538 19468  0 Jun28 ?        00:00:00 nginx: worker process
polkitd  19539 19468  0 Jun28 ?        00:00:00 nginx: cache manager process
root     19717 19716  0 Jun28 ?        00:00:20 tail --follow=name --retry /var/log/gitlab/puma/puma_stderr.log /var/log/gitlab/puma/current /var/log/gitlab/puma/puma_stdout.log /var/log/gitlab/gitlab-kas/current /var/log/gitlab/sshd/current /var/log/gitlab/gitlab-rails/api_json.log /var/log/gitlab/gitlab-rails/gitlab-rails-db-migrate-2022-06-24-06-27-08.log /var/log/gitlab/gitlab-rails/application.log /var/log/gitlab/gitlab-rails/audit_json.log /var/log/gitlab/gitlab-rails/grpc.log /var/log/gitlab/gitlab-rails/git_json.log /var/log/gitlab/gitlab-rails/auth.log /var/log/gitlab/gitlab-rails/exceptions_json.log /var/log/gitlab/gitlab-rails/sidekiq_client.log /var/log/gitlab/gitlab-rails/production_json.log /var/log/gitlab/gitlab-rails/database_load_balancing.log /var/log/gitlab/gitlab-rails/service_measurement.log /var/log/gitlab/gitlab-rails/production.log /var/log/gitlab/gitlab-rails/application_json.log /var/log/gitlab/gitlab-rails/graphql_json.log /var/log/gitlab/sidekiq/current /var/log/gitlab/redis/current /var/log/gitlab/gitlab-exporter/current /var/log/gitlab/redis-exporter/current /var/log/gitlab/gitaly/current /var/log/gitlab/gitaly/gitaly_hooks.log /var/log/gitlab/gitaly/gitaly_ruby_json.log /var/log/gitlab/gitlab-workhorse/current /var/log/gitlab/postgres-exporter/current /var/log/gitlab/alertmanager/current /var/log/gitlab/postgresql/current /var/log/gitlab/logrotate/current /var/log/gitlab/grafana/current /var/log/gitlab/prometheus/current /var/log/gitlab/nginx/access.log /var/log/gitlab/nginx/current /var/log/gitlab/nginx/error.log /var/log/gitlab/nginx/gitlab_access.log /var/log/gitlab/nginx/gitlab_error.log

通过killall命令

killall nginx

 

posted on 2021-11-21 11:06  周文豪  阅读(91)  评论(0编辑  收藏  举报