LINUX安装NGINX笔记
发布时间:2018-06-13, 16:50:13 分类:Linux | 编辑 off 网址 | 辅助
图集1/3
正文 1692字数 1,588,600阅读
安装依赖
yum -y install gcc zlib zlib-devel pcre-devel openssl openssl-devel
Run code
Cut to clipboard
下载nginx
//创建一个文件夹
cd /usr/local
mkdir nginx
cd nginx
//下载tar包
wget http://nginx.org/download/nginx-1.13.7.tar.gz
tar -xvf nginx-1.13.7.tar.gz
Run code
Cut to clipboard
安装nginx
//进入nginx目录
cd nginx-1.13.7
//执行命令
./configure
//执行make命令
make
//执行make install命令
make install
Run code
Cut to clipboard
配置防火墙
Failed to restart iptables.service: Unit not found.
解决CentOS7关闭/开启防火墙出现Unit iptables.service failed to load: No such file or directory.
firewall-cmd --zone=public --add-port=80/tcp --permanent
#(--permanent永久生效,没有此参数重启后失效)
Run code
Cut to clipboard
重新载入
firewall-cmd --reload
查看
firewall-cmd --zone=public --query-port=80/tcp
删除
firewall-cmd --zone=public --remove-port=80/tcp --permanent
Run code
Cut to clipboard
Nginx虚拟域名配置
//编辑nginx.conf
sudo vim /usr/local/nginx/conf/nginx.conf
//增加行
include vhost/*.conf
//保存退出
//在/usr/local/nginx/conf目录新建vhost文件夹
mkdir vhost
//创建每个域名的配置
sudo vim jimisun.com.conf
//节点中增加入响应的配置 端口转发 或者访问文件系统
Run code
Cut to clipboard
Nginx启动
//进入nginx安装目录
cd sbin
sudo ./nginx
Run code
Cut to clipboard
//启动命令
./nginx
//停止命令
./nginx -s stop
或者 : nginx -s quit
//重启命令
./nginx -s reload
Run code
Cut to clipboard
LINUX安装NGINX
nginx禁止使用ip地址直接访问
打开文件/etc/nginx/nginx.conf
将原server中的两个default_server去掉
换成
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
return 444;
}
Run code
Cut to clipboard
linux_nginx环境配置
linux_nginx反向代理
linux nginx配置新项目加域名
server{
server_name default sofun.net.cn iwstf.com snapshot.wiki;
rewrite ^(.*) http://lizhenqiu.com$1 permanent;
}
Run code
Cut to clipboard
(支付宝)给作者钱财以资鼓励 (微信)→
有过 5 条评论 »
//进入nginx目录 cd nginx
看到如下显示nginx.conf syntax is ok
nginx.conf test is successful
说明配置文件正确
wget -O wordpress.zip http://www.linuxde.net/download.aspx?id=1080 Centos出现-bash: unzip: command not found的解决办法 利用unzip命令解压缩的时候,出现-bash: unzip: command not found的错误。 unzip——命令没有找到,其原因肯定是没有安装unzip。 利用一句命令就可以解决了。 命令是: yum install -y unzip zip 安装成功后就可以使用unzip命令
Linux下ftp服务可以通过搭建vsftpd服务来实现
执行 systemctl start vsftpd.service 启动vsftp服务,然后可以通过命令: systemctl status vsftpd.service 查看ftp服务的运行状态
systemctl stop vsftpd.service
FTP是一种文件传输协议,一般是为了方便数据共享的。包括一个FTP服务器和多个FTP客户端。FTP客户端通过FTP协议在服务器上下载资源。
SFTP协议是在FTP的基础上对数据进行加密,使得传输的数据相对来说更安全。但是这种安全是以牺牲效率为代价的。
SFTP的传输效率比FTP要低。
查看了所运行的端口
>
netstat -anp
解决办法:
进入防火墙配置文件看看了
vim /etc/sysconfig/iptables
// 看看有没有443的文件,没有的话在改文件中添加下面这行
>-A INPUT -m state --state NEW -m tcp -p tcp --dport 443 -j ACCEPT
再次注意,
>-A INPUT -j REJECT –reject-with icmp-host-prohibited
添加的新的那句需要在这句的前面,否则443端口也有问题
原因是这条规则的作用是拒绝所有-j REJECT 在iptables帮助文档里面有一下说明This is used to send back an error packet in response to the matched packet
就是表明拒绝你,并返回一个错误连接信息。请求没到nginx那,肯定没有状态码返回,同时你的浏览器返回的只能是无法连接。
如果返回404,403等等,那说明是nginx的配置等因素;
/usr/local/nginx/sbin/nginx -s reload nginx: [error] invalid PID number "" in "/usr/local/nginx/logs/nginx.pid" 解决方法: /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf 使用nginx -c的参数指定nginx.conf文件的位置 [root@localhost nginx]# cd logs/ [root@localhost logs]# ll 总用量 12 -rw-r--r-- 1 root root 1246 12月 9 18:10 access.log -rw-r--r-- 1 root root 516 12月 10 15:39 error.log -rw-r--r-- 1 root root 5 12月 10 15:38 nginx.pid 看nginx.pid文件已经有了
1.1 Nginx如果未开启SSL模块,配置Https时提示错误
nginx: [emerg] the "ssl" parameter requires ngx_http_ssl_module in /usr/local/nginx/conf/nginx.conf:37
原因也很简单,nginx缺少http_ssl_module模块,编译安装的时候带上--with-http_ssl_module配置就行了,但是现在的情况是我的nginx已经安装过了,怎么添加模块,其实也很简单,往下看: 做个说明:我的nginx的安装目录是/usr/local/nginx这个目录,我的源码包在/usr/local/src/nginx-1.6.2目录
1.2 Nginx开启SSL模块
切换到源码包:
cd /usr/local/src/nginx-1.11.3
查看nginx原有的模块
/usr/local/nginx/sbin/nginx -V
在configure arguments:后面显示的原有的configure参数如下:
--prefix=/usr/local/nginx --with-http_stub_status_module
那么我们的新配置信息就应该这样写:
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
运行上面的命令即可,等配置完
配置完成后,运行命令
make
这里不要进行make install,否则就是覆盖安装
然后备份原有已安装好的nginx
cp /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.bak
然后将刚刚编译好的nginx覆盖掉原有的nginx(这个时候nginx要停止状态)
cp ./objs/nginx /usr/local/nginx/sbin/
然后启动nginx,仍可以通过命令查看是否已经加入成功
/usr/local/nginx/sbin/nginx -V
Nginx 配置Http和Https共存
server { listen 80 default backlog=2048; listen 443 ssl; server_name wosign.com; root /var/www/html; ssl_certificate /usr/local/Tengine/sslcrt/ wosign.com.crt; ssl_certificate_key /usr/local/Tengine/sslcrt/ wosign.com .Key; }
把ssl on;这行去掉,ssl写在443端口后面。这样http和https的链接都可以用
Nginx 配置SSL安全证书重启避免输入密码
可以用私钥来做这件事。生成一个解密的key文件,替代原来key文件。
openssl rsa -in server.key -out server.key.unsecure
Nginx SSL性能调优
ssl_protocols TLSv1 TLSv1.1 TLSv1.2; ssl_ciphers ECDHE-RSA-AES256-SHA384:AES256-SHA256:RC4:HIGH:!MD5:!aNULL:!eNULL:!NULL:!DH:!EDH:!AESGCM; ssl_prefer_server_ciphers on; ssl_session_cache shared:SSL:10m; ssl_session_timeout 10m;
Nginx 配置Http跳转Https
server { listen 80; server_name www.域名.com; rewrite ^(.*) https://$server_name$1 permanent; } server { listen 443; server_name www.域名.com; root /home/www; ssl on; ssl_certificate /etc/nginx/certs/server.crt; ssl_certificate_key /etc/nginx/certs/server.key; }
CentOS7中执行
service iptables start/stop
会报错Failed to start iptables.service: Unit iptables.service failed to load: No such file or directory.
在CentOS 7或RHEL 7或Fedora中防火墙由firewalld来管理,
如果要添加范围例外端口 如 1000-2000
语法命令如下:启用区域端口和协议组合
firewall-cmd [--zone=<zone>] --add-port=<port>[-<port>]/<protocol> [--timeout=<seconds>]
此举将启用端口和协议的组合。端口可以是一个单独的端口 <port> 或者是一个端口范围 <port>-<port> 。协议可以是 tcp 或 udp。
实际命令如下:
添加
firewall-cmd --zone=public --add-port=80/tcp --permanent (--permanent永久生效,没有此参数重启后失效)
firewall-cmd --zone=public --add-port=1000-2000/tcp --permanent
重新载入
firewall-cmd --reload
查看
firewall-cmd --zone=public --query-port=80/tcp
删除
firewall-cmd --zone=public --remove-port=80/tcp --permanent
当然你可以还原传统的管理方式。
执行一下命令:
systemctl stop firewalld
systemctl mask firewalld
并且安装iptables-services:
yum install iptables-services
设置开机启动:
systemctl enable iptables
systemctl stop iptables
systemctl start iptables
systemctl restart iptables
systemctl reload iptables
保存设置:
service iptables save
OK,再试一下应该就好使了
开放某个端口 在/etc/sysconfig/iptables里添加
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 8080 -j ACCEPT
输入 service iptables restart
Redirecting to /bin/systemctl restart iptables.service
Failed to restart iptables.service: Unit not found.
被完美解决!!!
“ 查看 firewall-cmd --zone= public --query-port=80/tcp ” 这个语句输入后报错 “ usage: see firewall-cmd man page firewall-cmd: error: unrecognized arguments: public ” 将语句修改为 “ firewall-cmd --query-port=80/tcp ” 后可以使用
解决:查看端口
netstat -ntlp
找到80端口的PID
kill 6751