Nginx 的配置及运行需要pcre、zlib等软件包的支持,因此应预先安装这些软件的开发包(devel),以便提供相应的库和头文件,确保 Nginx 的安装顺利完成
[root@localhost~]# yum -y install pcre-devel zlib-develNginx 服务程序默认以 nobody身份运行,建议为其创建专门的用户账号,以便更准确地控制其访问权限,增加灵活性、降低安全风险。例如,创建一个名为 nginx 的用户,不建立宿主文件夹,也禁止登录到Shell环境。 [root@localhost ~]#useradd-M-s/sbin/nologin nginx
配置Nginx的编译选项时,将安装目录设为/usr/loca/nginx,运行用户和组均设为nginx;启用http_stub_status module模块以支持状态统计,便于查看服务器的连接信息。具体选项根据实际需要来定,配置前可参考"/configure -help"给出的说明。 上传nginx-1.15.9.tar.gz至/opt目录
[root@localhost~]# cd/opt [root@localhost opt]# tar xzvf nginx-1.15.9.tar.gz [root@localhost nginx-1.15.9]#√configure\ --prefix=/usr/local/nginx\--user=nginx\--group=nginx\ --with-http_stub status module [root@localhost nginx-1.15.9]# make && make instl ##1.3路径优化##为了使 Nginx 服务器的运行更加方便,可以为主程序 nginx 创建链接文件,以便管理员直接执行"nginx"命令就可以调用Nginx 的主程序
[root@localhost nginx-1.15.9]# In-s/usr/local/nginx/sbin/nginx/usr/local/sbin/ [root@localhost nginx-1.15.9]# Is-l/usr/local/sbin/nginx Irwxrwxrwx 1 root root 27 2月 28 05∶17/usr/local/sbin/nginx->/usr/local/nginx/sbin/nginx与 Apache的主程序 httpd类似,Nginx的主程序也提供了"-t"选项用来对配置文件进行检查,以便找出不当或错误的配置。配置文件nginx.conf 默认位于安装目录下的conf/子目录中。若要检查位于其他位置的配置文件,可使用"-c"选项来指定路径。
[root@localhost nginx-1.15.9]#nginx -t nginx: the configuration file/usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file/usr/local/nginx/conf/nginx.conf test is successful直接运行Nginx 即可启动Nginx 服务器,这种方式将使用默认的配置文件,若要改用其他配置文件,需添加"-c 配置文件路径"选项来指定路径。需要注意的是,若服务器中已装有httpd等其他 Web 服务软件,应采取措施(修改端口、停用或卸载)避免冲突。
[root@localhost nginx-1.15.9]# nginx [root@localhost nginx-1.15.9]#nestat-anptgre|grep nginx N11191/nginx: master tcp 0 00.0.0.0:80通过检查Nginx 程序的监听状态,或者在浏览器中访问此 Web 服务(默认页面将显示"Welcom to nginx!"),可以确认 Nginx 服务是否正常运行。
[root@localhost nginx-1.15.9]# yum-y install lynx [root@localhost nginx-1.15.9]# lynx 127.0.0.1 Welcome to nginx!Welcome to nginx! If you see this page, the nginx web server is successfullyinstalled and working. Further confiauration is required主程序Nginx 支持标准的进程信号,通过 kill 或kill命令发送 HUP信号表示重载配置,QUIT信号表示退出进程,KILL信号表示杀死进程。例如,若使用 killl命令,重载配置、停止服务的操作分别如下所示(通过"s"选项指定信号种类)
[root@localhost~]# kill-s HUP nginx ###选项-s HUP等同于-1重新加载[root@localhost~~]# killall-s QUIT nginx ###选项-s QUIT 等同于-3停止服务当Nginx进程运行时,PID号默认存放在logs/目录下的 nginx.pid 文件中,因此若改用kill命令,也可以根据 nginx.pid 文件中的PID号来进行控制。
[root@localhost~]#chmod 754 /lib/systemd/system/nginx.service [root@localhost ~]# systemct enable nginx.service Created symlink from /et/systemd/system/multi-user.targetwants/nginx.service to /us/lib/systemd/system/nginx.sevice检查通过systemct命令能否正常 启动、停止、重启、重载 Nginx 服务
[root@localhost ~]# neSa-anup gre:ngin [root@localhost ~]# systemctl start nginx [root@localhost~]# netstat -anutp grep nginx 0 00.0.0.0:800.0.0.0:*LISTEN114' [root@localhost~]#由各种配置语句组成,不使用特定的界定标记。全局配置部分包括 Nginx 服务的运行用户、工作进程数、错误日志、PID 存放位置等基本设置
在 Nginx 服务器的主配置文件/ustr/local/nginx/conf/nginx.conf中,包括全局配置、I/O 事件配置和HTTP配置这三大块内容,配置语句的格式为"关键字值∶"(末尾以分号表示结束),以"#"开始的部分表示注释。
由各种配置语句组成,不使用特定的界定标记。全局配置部分包括Nginx 服务的运行用户、工作进程数、错误日志、PID存放位置等基本设置
[root@localhost ~]# vi/ust/local/nginx/conf/nginx.conf #user nobody; ##运行用户 worker processes 1; ##工作进程数 PrivateTmp=true [Install] WantedBy=multi-user.target配置参数解释 [Unit] Description=nginx ####描述 After=network.target ####描述服务类别 [Service] Type=forking###后台运行形式 PIDFile=/usr/local/nginx/logs/nginx.pid###PID文件位置 ExecStart=/usr/local/nginx/sbin/nginx###启动服务 ExecReload=/usr/bin/kill-s HUP SMAINPID ###根据PID重载配置 ExecStop=/usr/bin/kill-s QUIT SMAINPID###根据PID终止进程
PrivateTmp=true [Install] WantedBy=multi-user.target [root@localhost~]# chmod 754/lib/systemd/system/nginx.service [root@localhost~]# systemctl enable nginx.service Created symlink from /ete/systemd/system/multi-user.targetwants/nginx.service to/ur/lib/systemd/system/nginx.service. 检查通过systemctl命令能否正常 启动、停止、重启、重载 Nginx 服务
[root@localhost ~]# netstat-anutp lgrep nginx [root@localhost ~]# systemctl start nginx [root@localhost ~]# netstat-anutp lgrep nginx tcp 0 00.0.0.0:800.0.0.0:*LISTEN 11 [root@localhost ~]#使用"events{}"界定标记,用来指定Nginx 进程的I/O响应模型、每个进程的连接数等设置。对于2.6及以上版本的内核,建议使用epoll 模型以提高性能;每个进程的连接数应根据实际需要来定,一般在10 000以下(默认为1024)
events { use epoll; worker_connections 4096; }若工作进程数为8,每个进程处理4096个连接,则允许 Nginx 正常提供服务的连接数已超过3万个(4096×8=32768),当然具体还要看服务器硬件、网络带宽等物理条件的性能表理
使用"http{"界定标记,包括访问日志、HTTP端口、网页目录、默认字符集、连接保持,以及后面要讲到的虚拟Web主机、PHP解析等一系列设置,其中大部分配置语句都包含在子界定标记"server{}"内
#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 { use epoll; worker_connections 4096; } http{ include mime.types; default type application/octet-stream; log_ format main$remote addr-$remote user【$time local】"$request"'##●去掉前面注释 '$status Sbody bytes sent "Shttp referer"'; ###●去掉前面注释 '"$http user agent""Shttp x forwarded for";###●去掉前面注释 access log logs/access.log main;###e访问日志位置,去掉前面注释 sendfile on###支持文件发送(下载) #keepalive timeout 0;###链接保持时间 keepalive _timeout 65; #gzip on; server{ ###WEB服务的监听配置 listen 80; ###WEB服务的监听端口 server name www.51xit.top;###网站名称(FQDN) charset koi8-r; ###网页的默认字符集 #access log logs/host.access.log main; location/{ ###根目录配置 root html; ###网站根目录的位置 index index.html index.htm; ###默认首页(索引页) /404.html; error page 404 #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${ #nrox pass httn:LL127.0.0.1: #}Nginx内置了HTTPSTUB STATUS状态统计模块,用来反馈当前的Web 访问情况,配置编译参数时可添加-with-http stubstatus module来启用此模块支持,可以使用命令/usr/local/nginx/sbin/nginx-V查看已安装的Nginx是否包含HTTP STUB STATUS模块。 要使用Nginx 的状态统计功能,除了启用内建模块以外,还需要修改nginx.conf配置文件,指定访问位置并添加 stub_status 配置代码。 [root@localhost~]# vim /usr/local/nginx/conf/nginx.conf ####省略#### 在localtion/这个大模块下面 location/status模块 location/{ root html; index index.html index.htm; } location /status{ stub status on; access log off; } ####省略#### [root@localhost nginx]# nginx-t nginx: the configuration file/usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file/usr/local/nginx/conf/nginx.conf test is successful [root@localhost nginx]# systemctl restart nginx
[root@localhost nginx]# lynx 127.0.0.1/status Active connections∶1 ###活动连接数 server accepts handled requests ##已经处理的链接信息 ##上个数字从左到右依次表示∶已处理的连接数、成功的TCP握手次数、已处Reading:0O Writing: 1 Waiting:0
利用虚拟主机,不用为每个要运行的网站提供一台单独的Nginx 服务器或单独运行一组Nginx进程,虚拟主机提供了在同一台服务器,同一组Nginx进程上运行多个网站的功能。跟Apache 一样,Nginx也可以配置多种类型的虚拟主机,分别是基于IP的虚拟主机、基于域名的虚拟主机、基于端口的虚拟主机。 使用 Nginx 搭建虚拟主机服务器时,每个虚拟 Web 站点拥有独立的"serverl)"配置段,各自监听的 IP地址、端口号可以单独指定,当然网站名称也是不同的。 #基于域名的虚拟主机###
修改 windos 客户机的C∶\Windows\System32\driversletc\hosts 文件,加入www.51xit.top和www.52xit.top 这两个域名,它们都指向同一个服务器 IP地址,用于实现不同的域名访问不同的虚拟主机。 8.100.41 www.51xit.top www.52
8、设置mysql密码并登录数据库
[root@localhost mysql]# mysqladmin -u root -p password ##刚开始没密码是空的直接回车,然后输入密码12345,再次确认密码## Enter password: New password: Confirm new password: Warning: Since password will be sent to server in plain text, use ssl connection to ensure password safety. ##密码设置成功## [root@localhost mysql]# mysql -u root -p ##输入密码,即可登录到数据库## . . . . . . . . . . . . . . mysql> exit