LNMP搭建4:Nginx启动脚本和配置文件
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LNMP搭建4:Nginx启动脚本和配置文件相关的知识,希望对你有一定的参考价值。
nginx没有像apachetl那样的启动脚本,我们需要手动做一个
[[email protected] html]# vim /etc/init.d/nginx
内容如下:http://www.apelearn.com/study_v2/chapter18.html
#!/bin/bash
# chkconfig: - 30 21
# description: http service.
# Source Function Library
. /etc/init.d/functions
# Nginx Settings
NGINX_SBIN="/usr/local/nginx/sbin/nginx"
NGINX_CONF="/usr/local/nginx/conf/nginx.conf"
NGINX_PID="/usr/local/nginx/logs/nginx.pid"
RETVAL=0
prog="Nginx"
start() {
echo -n $"Starting $prog: "
mkdir -p /dev/shm/nginx_temp
daemon $NGINX_SBIN -c $NGINX_CONF
RETVAL=$?
echo
return $RETVAL
}
stop() {
echo -n $"Stopping $prog: "
killproc -p $NGINX_PID $NGINX_SBIN -TERM
rm -rf /dev/shm/nginx_temp
RETVAL=$?
echo
return $RETVAL
}
reload() {
echo -n $"Reloading $prog: "
killproc -p $NGINX_PID $NGINX_SBIN -HUP
RETVAL=$?
echo
return $RETVAL
}
restart() {
stop
start
}
configtest() {
$NGINX_SBIN -c $NGINX_CONF -t
return 0
}
case "$1" in
start)
start
;;
stop)
stop
;;
reload)
reload
;;
restart)
restart
;;
configtest)
configtest
;;
*)
echo $"Usage: $0 {start|stop|reload|restart|configtest}"
RETVAL=1
esac
exit $RETVAL
更改权限,加入启动列表,开机启动
[[email protected] html]# chmod 755 /etc/init.d/nginx
[[email protected] html]# chkconfig --add nginx
[[email protected] html]# chkconfig nginx on
测试一下启动,停止,重启,重新加载配置,检测语法等功能:
[[email protected] html]# service nginx start
正在启动 Nginx: [确定]
[[email protected] html]# service nginx stop
停止 Nginx: [确定]
[[email protected] html]# service nginx start
正在启动 Nginx: [确定]
[[email protected] html]# service nginx restart
停止 Nginx: [确定]
正在启动 Nginx: [确定]
[[email protected] vhosts]# service nginx reload
重新载入 Nginx: [确定]
[[email protected] html]# service nginx configtest
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自带的配置文件很乱,需要重新整理一下,清空原有内容,写入如下内容:
[[email protected] nginx]# > /usr/local/nginx/conf/nginx.conf
[[email protected] nginx]# cat /usr/local/nginx/conf/nginx.conf
[[email protected] nginx]# vim /usr/local/nginx/conf/nginx.conf
user nobody nobody;
worker_processes 2;
error_log /usr/local/nginx/logs/nginx_error.log crit;
pid /usr/local/nginx/logs/nginx.pid;
worker_rlimit_nofile 51200;
events {
use epoll;
worker_connections 6000;
}
http {
include mime.types;
default_type application/octet-stream;
server_names_hash_bucket_size 3526;
server_names_hash_max_size 4096;
log_format combined_realip ‘$remote_addr $http_x_forwarded_for [$time_local]‘
‘$host "$request_uri" $status‘
‘"$http_referer" "$http_user_agent"‘;
sendfile on;
tcp_nopush on;
keepalive_timeout 30;
client_header_timeout 3m;
client_body_timeout 3m;
send_timeout 3m;
connection_pool_size 256;
client_header_buffer_size 1k;
large_client_header_buffers 8 4k;
request_pool_size 4k;
output_buffers 4 32k;
postpone_output 1460;
client_max_body_size 10m;
client_body_buffer_size 256k;
client_body_temp_path /usr/local/nginx/client_body_temp;
proxy_temp_path /usr/local/nginx/proxy_temp;
fastcgi_temp_path /usr/local/nginx/fastcgi_temp;
fastcgi_intercept_errors on;
tcp_nodelay on;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 8k;
gzip_comp_level 5;
gzip_http_version 1.1;
gzip_types text/plain application/x-javascript text/css text/htm application/xml;
include vhosts/*.conf;
}
配置默认虚拟主机:在/usr/local/nginx/conf/下创建vhosts/目录,并在该目录下创建默认虚拟主机配置文件default.conf
[[email protected] nginx]# cd conf/
[[email protected] conf]# mkdir vhosts
[[email protected] conf]# cd vhosts/
[[email protected] vhosts]# vim default.conf
server {
listen 80 default_server; #或者default
server_name localhost;
index index.html index.htm index.php;
root /tmp/1233;
deny all;
}
[[email protected] vhosts]# mkdir /tmp/1233
检查无误后重新加载nginx的配置文件:
[[email protected] vhosts]# /usr/local/nginx/sbin/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
[[email protected] vhosts]# /etc/init.d/nginx reload
重新载入 Nginx: [确定]
设置完默认虚拟主机之后访问都被禁止了:403
使用curl命令测试也是403
[[email protected] conf]# curl -x127.0.0.1:80 www.test.com
<html>
<head><title>403 Forbidden</title></head>
<body bgcolor="white">
<center><h1>403 Forbidden</h1></center>
<hr><center>nginx/1.6.2</center>
</body>
</html>
<html>
<head><title>403 Forbidden</title></head>
<body bgcolor="white">
<center><h1>403 Forbidden</h1></center>
<hr><center>nginx/1.6.2</center>
</body>
</html>
默认虚拟主机只是加强服务器的安全性,我们需要设置一个可用的虚拟主机,需要创建一个配置文件
[[email protected] vhosts]# vim www.test.com.conf
server {
listen 80;
server_name www.test.com;
index index.html index.htm index.php;
root /data/www;
location ~ \.php$ {
include fastcgi_params;
#fastcgi_pass unix:/tmp/php-fcgi.sock;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /data/www$fastcgi_script_name;
}
}
检查无误后重新加载配置文件:
[[email protected] vhosts]# /usr/local/nginx/sbin/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
[[email protected] vhosts]# /etc/init.d/nginx reload
重新载入 Nginx: [确定]
在本地主机hosts文件中加入域名www.test.com到192.168.147.132的解析:
在浏览器中输入域名www.test.com回车后显示404,这是因为我们的网站根目录/data/www还没有安装网站。
使用curl测试也是一样
[[email protected] vhosts]# curl -x127.0.0.1:80 www.test.com
<html>
<head><title>404 Not Found</title></head>
<body bgcolor="white">
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.6.2</center>
</body>
</html>
我们参考LAMP部分安装discuz论坛:http://rachy.blog.51cto.com/11428504/1891094
第五步使用:
[[email protected] www]# chown -R php-fpm:php-fpm config data uc_client/data uc_server/data
注意第六、七、八、九步与LAMP的不同!!!
如果我们在/data/www目录下装上discuz论坛,再次访问www.test.com域名就能看到我们的论坛
也可以直接使用curl命令测试:
[[email protected] www]# curl -x127.0.0.1:80 www.test.com/forum.php -I
HTTP/1.1 200 OK
Server: nginx/1.6.2
Date: Thu, 12 Jan 2017 22:11:18 GMT
Content-Type: text/html; charset=gbk
Connection: keep-alive
X-Powered-By: PHP/5.4.37
Set-Cookie: nThc_2132_saltkey=nPB4xyT4; expires=Sat, 11-Feb-2017 22:11:18 GMT; path=/; httponly
Set-Cookie: nThc_2132_lastvisit=1484255478; expires=Sat, 11-Feb-2017 22:11:18 GMT; path=/
Set-Cookie: nThc_2132_sid=h7cw47; expires=Fri, 13-Jan-2017 22:11:18 GMT; path=/
Set-Cookie: nThc_2132_lastact=1484259078%09forum.php%09; expires=Fri, 13-Jan-2017 22:11:18 GMT; path=/
Set-Cookie: nThc_2132_onlineusernum=2; expires=Thu, 12-Jan-2017 22:16:18 GMT; path=/
Set-Cookie: nThc_2132_sid=h7cw47; expires=Fri, 13-Jan-2017 22:11:18 GMT; path=/
以上是关于LNMP搭建4:Nginx启动脚本和配置文件的主要内容,如果未能解决你的问题,请参考以下文章