失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > 阿里云ECS(Centos)中Nginx安装及配置

阿里云ECS(Centos)中Nginx安装及配置

时间:2024-02-06 16:13:21

相关推荐

阿里云ECS(Centos)中Nginx安装及配置

Nginx是一个高性能的http和反向代理web服务器,本篇在记录阿里云ECS中安装及配置Nginx的过程。Nginx作为系统关键服务,下面流程全部在root用户下完成。

系统:CentOS 8.0 64位Nginx:1.18.0版本

1 安装nginx依赖

Nginx的安装通过源码包编译完成,需要c++支持库,同时在Nginx运行过程中对于一些压缩、路由正则表达式、https支持等相关特性需要其他一些支持库。一般情况下,安装gcc gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel这些依赖可满足基本要求,安装指令如下:

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

上面指令是完整安装,如果服务器已安装部分依赖,可忽略,具体可执行yum info 依赖包名称查看是否已安装。如下图,本机只安装了部分未安装的依赖。

安装完成后,指令结果会列出已安装和更新的项,如下:

2 安装Nginx

2.1 下载解压源码包

从 Nginx官方 下载最新稳定版本源码,当前为 1.18.0 ,如下图。

下载及解压指令如下(解压目标目录自行指定):

wget /download/nginx-1.18.0.tar.gztar -zxvf nginx-1.18.0.tar.gz

2.2 安装配置检查

解压完成后,进入到解压目录执行安装检查,运行如下指令:

./configure

检查完成后,默认安装配置信息如上图所示。

2.3 安装

在解压目录下执行下面指令,默认会安装到/usr/local/目录下

make && make install

2.4 配置别名

此步骤可选,目的是为了后续运行nginx操作指令方便。别名配置方式和当前使用的shell类型相关,这里使用zsh,所以在用户目录的.zshrc文件中加入如下别名nginx替代nginx执行文件的完整路径。

vim ~/.zshrcsource ~/.zshrc

2.5 安装结果验证

安装完成后,会生成nginx默认配置文件/usr/local/nginx/conf/nginx.conf,内容如下:

#user nobody;worker_processes 1;#error_log logs/error.log;#error_log logs/error.log notice;#error_log logs/error.log info;#pid logs/nginx.pid;events {worker_connections 1024;}http {include mime.types;default_type application/octet-stream;#log_format main '$remote_addr - $remote_user [$time_local] "$request" '# '$status $body_bytes_sent "$http_referer" '# '"$http_user_agent" "$http_x_forwarded_for"';#access_log logs/access.log main;sendfile on;#tcp_nopushon;#keepalive_timeout 0;keepalive_timeout 65;#gzip on;server {listen 80;server_name localhost;#charset koi8-r;#access_log logs/host.access.log main;location / {root html;index index.html index.htm;}#error_page 404 /404.html;# redirect server error pages to the static page /50x.html#error_page 500 502 503 504 /50x.html;location = /50x.html {root html;}# proxy the PHP scripts to Apache listening on 127.0.0.1:80##location ~ \.php$ {# proxy_pass http://127.0.0.1;#}# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000##location ~ \.php$ {# root html;# fastcgi_pass 127.0.0.1:9000;# fastcgi_index index.php;# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;# include fastcgi_params;#}# deny access to .htaccess files, if Apache's document root# concurs with nginx's one##location ~ /\.ht {# deny all;#}}# another virtual host using mix of IP-, name-, and port-based configuration##server {# listen 8000;# listen somename:8080;# server_name somename alias another.alias;# location / {# root html;# index index.html index.htm;# }#}# HTTPS server##server {# listen 443 ssl;# server_name localhost;# ssl_certificatecert.pem;# ssl_certificate_key cert.key;# ssl_session_cache shared:SSL:1m;# ssl_session_timeout 5m;# ssl_ciphers HIGH:!aNULL:!MD5;# ssl_prefer_server_ciphers on;# location / {# root html;# index index.html index.htm;# }#}}

可执行如下命令nginx配置检查,如果配置文件有错误会有相应提示。

nginx -t

如上图,nginx配置正确,下面指令启动nginx、查看nginx进程、并从本机测试可访问性(默认开启80端口,所以使用localhost访问即可),如下图:

nginxps -ef|grep nginxcurl localhost

上图中使用curl命令访问本机可正常返回页面信息,nginx安装完成。后续可进行其他域名相关的绑定配置。

3 阿里云ECS配置

如上面步骤nginx已成功安装,且在本机可访问。但是由于阿里云的安全机制,默认情况下无法使用公网IP进行访问,也就是无法在其他主机通过非域名方式访问。解决方式如下。

3.1 添加安全组

默认情况下,阿里云系统会给每个ESC主机关联一个默认的安全组(每个ECS实例也至少要关联一个安全组),该安全组默认开启了TCP协议中的223389端口,用于支持sshmysql操作,如下图。所以安装Nginx后,默认使用的80端口并未开放,所以无法访问。

这里建立一个新的安全组用于开放nginx使用的端口,如下图,入方向开放80443两个端口。(如果nginx使用了其他端口,可类似添加其他端口的规则)。

3.2 ECS实例加入新安全组

将安装nginx的ECS实例加入到刚才创建的安全组中,如下图。

3.3 测试公网IP访问

完成上面步骤后,在浏览器中,直接输入ECS的公网IP,可成功访问,如下图:

3.4 域名访问

国内主机不允许使用未备案的域名进行站点访问,所以,如果在nginx中将站点绑定到域名,无法正常访问,如下图。这种情况可对域名进行备案,或者将域名绑定到国外IP的主机。

如果觉得《阿里云ECS(Centos)中Nginx安装及配置》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。