下载并配置nginx
先下载依赖包
sudo apt-get install gcc pcre-devel zlib-devel openssl opensll-devel
下载时出现问题可以先下载sudo apt-get install libpcre3-dev
gpt说的↑
同理几个库可以sudo apt-get install libssl-dev
sudo apt-get install zlib1g-dev
如果出现问题可以更新一下apt-get
sudo apt-get update
下载nginx压缩包,这里直接选择版本,也可以打开官网选择最新且稳定的版本安装(前几天下的时候应该是1.24.0)nginx: download
apt-get install wget
wget https://nginx.org/download/nginx-1.24.0.tar.gz
在下载界面打开终端(打开界面右键“在终端中打开open in terminal”)进行解压
tar -zxvf nginx-1.24.0.tar.gz
而后进入nginx目录,应当跟压缩包在一个目录下
进入目录在终端打开,进行初始化,这里我的地址是下图所示,地址部分自行进行更改
./configure --prefix=/usr/local/nginx
等待一会后如果不报错直接进行make,然后需要等很久再进行 make install
如果出现报错,应当是缺某部分库造成的,根据提示下载不同的库即可
而后进入/usr/local/nginx/sbin 启动文件位置
运行./nginx 启动,在浏览器中输入自己服务器的ip,如果正常运作的话会显示
这里ip的查看可以运行ifconfig,查看配置(同样这个命令没有的话根据提示下载即可)
其中查看nginx状态可以运行 nginx -t
至此nginx初步配置完成
为网站增加https协议
首先申请证书
详情参考linux本地虚拟机使用https证书 - CeasonCing - 博客园 (cnblogs.com)
查看完配置后即可退出,后续具体配置于本文(注意在填Common Name时填写自己的服务器ip,即上述中提到的地址)
申请证书后,终端打开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_nopush on;
#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 Xxx.xxx#这里是我服务器的ip,复制时记得改成自己的
ssl_certificate /usr/local/nginx/ssl/server.crt;#证书的地址,如果之前的步骤一致的话这里地址应该也一样
ssl_certificate_key /usr/local/nginx/ssl/server.key;#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;
}
}
}
保存:ctrl+0,退出Ctrl+x
这里的内容自己改了很久才勉强改出来,但是其实还不太清楚里面的原理,都是这里抄一点那里抄一点拼凑出来的,能跑感觉有一定运气成分()
然后重新进行nginx的运行
附上几个nginx操作
运行:./nginx
停止 nginx -s stop或者killall nginx(更彻底)
重启service nginx restart
再运行后打开网站为
应该是成功了。
折磨死我了。。。