首先在Nginx官网下载稳定版本的Nginx安装包,并将安装包上传到Linux。
使用 tar -zxvf nginx-1.16.0.tar.gz
将压缩包解压。
接下来先安装Nginx所需的依赖:
yum install gcc-c++
yum install -y pcre pcre-devel
注:pcre-devel是使用pcre开发的一个二次开发库。nginx也需要此库。yum install -y zlib zlib-devel
yum install -y openssl openssl-devel
然后进入刚刚解压的文件夹,执行以下命令:
[root@localhost nginx-1.16.0]# ./configure \
> --prefix=/usr/local/nginx \
> --pid-path=/var/run/nginx/nginx.pid \
> --lock-path=/var/lock/nginx.lock \
> --error-log-path=/var/log/nginx/error.log \
> --http-log-path=/var/log/nginx/access.log \
> --with-http_gzip_static_module \
> --http-client-body-temp-path=/var/temp/nginx/client \
> --http-proxy-temp-path=/var/temp/nginx/proxy \
> --http-fastcgi-temp-path=/var/temp/nginx/fastcgi \
> --http-uwsgi-temp-path=/var/temp/nginx/uwsgi \
> --http-scgi-temp-path=/var/temp/nginx/scgi
可以看到文件夹下多出一个Makefile,然后依次执行make指令和make install指令:
[root@localhost ~]# make
[root@localhost ~]# make install
在启动Nginx之前,先创建临时文件夹:
[root@localhost ~]# mkdir /var/temp
[root@localhost ~]# mkdir /var/temp/nginx
进入Nginx的安装目录:/usr/local/nginx,可以看到有conf、sbin、html三个文件夹,其中sbin中存放着的就是Nginx的可执行文件:
[root@localhost ~]# cd /usr/local/nginx
[root@localhost ~]# cd sbin
[root@localhost ~]# ./nginx
快速停止:查找到Nginx的进程id然后使用kill命令强制杀死进程。
[root@localhost ~]# cd /usr/local/nginx/sbin
[root@localhost ~]# ./nginx -s stop
完整停止:等待Nginx进程处理完任务再进行停止。
[root@localhost ~]# cd /usr/local/nginx/sbin
[root@localhost ~]# ./nginx -s quit
先停止再启动
[root@localhost ~]# cd /usr/local/nginx/sbin
[root@localhost ~]# ./nginx -s quit
[root@localhost ~]# ./nginx
重新加载配置文件
[root@localhost ~]# cd /usr/local/nginx/sbin
[root@localhost ~]# ./nginx -s reload
Nginx的默认端口是80,需要在iptables中将80端口设为开放,打开iptables文件并加入以下信息:
打开iptables文件:[root@localhost nginx]# vim /etc/sysconfig/iptables
在其中加入:-A INPUT -p tcp -m tcp --dport 80 -j ACCEPT
如下:
保存退出。
重启iptables:
[root@localhost ~]# service iptables restart
然后在主机浏览器通过ip+端口访问Nginx,比如:192.168.94.192:80,看到如下信息,说明Nginx配置成功:
这里使用编写shell脚本的方式设置:
[root@localhost ~]# vim /etc/init.d/nginx
输入如下脚本:
#!/bin/bash
# nginx Startup script for the Nginx HTTP Server
# it is v.0.0.2 version.
# chkconfig: - 85 15
# description: Nginx is a high-performance web and proxy server.
# It has a lot of features, but it's not for everyone.
# processname: nginx
# pidfile: /var/run/nginx.pid
# config: /usr/local/nginx/conf/nginx.conf
nginxd=/usr/local/nginx/sbin/nginx
nginx_config=/usr/local/nginx/conf/nginx.conf
nginx_pid=/var/run/nginx.pid
RETVAL=0
prog="nginx"
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ ${NETWORKING} = "no" ] && exit 0
[ -x $nginxd ] || exit 0
# Start nginx daemons functions.
start() {
if [ -e $nginx_pid ];then
echo "nginx already running...."
exit 1
fi
echo -n $"Starting $prog: "
daemon $nginxd -c ${nginx_config}
RETVAL=$?
echo
[ $RETVAL = 0 ] && touch /var/lock/subsys/nginx
return $RETVAL
}
# Stop nginx daemons functions.
stop() {
echo -n $"Stopping $prog: "
killproc $nginxd
RETVAL=$?
echo
[ $RETVAL = 0 ] && rm -f /var/lock/subsys/nginx /var/run/nginx.pid
}
# reload nginx service functions.
reload() {
echo -n $"Reloading $prog: "
#kill -HUP `cat ${nginx_pid}`
killproc $nginxd -HUP
RETVAL=$?
echo
}
# See how we were called.
case "$1" in
start)
start
;;
stop)
stop
;;
reload)
reload
;;
restart)
stop
start
;;
status)
status $prog
RETVAL=$?
;;
*)
echo $"Usage: $prog {start|stop|restart|reload|status|help}"
exit 1
esac
exit $RETVAL
保存并退出。
设置文件的访问权限:
chmod a+x /etc/init.d/nginx
(a+x ==> all user can execute 所有用户可执行)
加入到rc.local中:
[root@localhost ~]# vim /etc/rc.local
加入一行:/etc/init.d/nginx start
,下次重启会生效。
[root@localhost ~]# yum -y install vsftpd
安装完后,有/etc/vsftpd/vsftpd.conf 文件,是vsftp的配置文件。
此用户就是用来登录ftp服务器用的。
[root@localhost ~]# useradd ftpuser
添加密码:
[root@localhost ~]# passwd ftpuser
因为ftp默认的端口为21,而centos默认是没有开启的,所以要修改iptables文件
[root@localhost ~]# vim /etc/sysconfig/iptables
在22 -j ACCEPT 下面另起一行输入跟那行差不多的,只是把22换成21,然后:wq保存。
重启iptables:
[root@localhost ~]# service iptables restart
执行以下命令查看状态:
[root@localhost ~]# getsebool -a | grep ftp
allow_ftpd_anon_write --> off
allow_ftpd_full_access --> off
allow_ftpd_use_cifs --> off
allow_ftpd_use_nfs --> off
ftp_home_dir --> off
ftpd_connect_db --> off
ftpd_use_passive_mode --> off
httpd_enable_ftp_server --> off
tftp_anon_write --> off
执行上面命令,返回的结果中allow_ftpd_full_access和ftp_home_dir都是off,代表没有开启外网的访问
[root@localhost ~]# setsebool -P allow_ftpd_full_access on
[root@localhost ~]# setsebool -P ftp_home_dir on
[root@localhost ~]# vim /etc/vsftpd/vsftpd.conf
[root@localhost ~]# chkconfig vsftpd on
将Nginx作为项目图片服务器,需要配置nginx.conf文件:
使用java代码测试,需要依赖Apache的commons-net依赖。
package com.yangming.controller;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.junit.Test;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
public class FtpTest {
@Test
public void testFTPClient() throws IOException {
//创建一个FTP客户端
FTPClient ftpClient = new FTPClient();
//创建连接
ftpClient.connect("192.168.94.129",21);
//登录
ftpClient.login("ftpuser","ftpuser");
//创建一个文件输入流,读取一张图片文件
FileInputStream inputStream = new FileInputStream(new File(
"C:\\Users\\Administor\\Pictures\\Saved Pictures\\timg.jpg"));
//指定文件上传的路径
ftpClient.changeWorkingDirectory("/home/ftpuser/www/images");
//指定上传文件的格式,默认文本格式,需要改成二进制格式
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
//上传文件
ftpClient.storeFile("test.jpg",inputStream);
//退出登录
ftpClient.logout();
}
}
此时访问192.168.94.129/images/test.jpg就可以看到上传的图片了。
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有