添加源
rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
安装Nginx
sudo yum install -y nginx
直到出现 Complete!
启动
sudo systemctl start nginx.service
启动后在浏览器上访问,网页出现以下内容说明安装成功。
Nginx常用命令
查看版本:nginx -v
查看nginx安装目录:ps -ef | grep nginx
检查配置文件:nginx -t
启动:systemctl start nginx.service
停止:systemctl stop nginx.service
重启:systemctl restart nginx.service
设置开机自启动:systemctl enable nginx.service
停止开机自启动:systemctl disable nginx.service
查看当前状态:systemctl status nginx.service
查看所有已启动的服务:systemctl list-units --type=service
搭配php 配置多站点
server {
listen 80;
server_name example.com;
root /var/www/example.com;
location / {
index index.php index.html index.htm;
}
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
server {
listen 80;
server_name test.com;
root /var/www/test.com;
location / {
index index.php index.html index.htm;
}
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
server {
listen 8000;
server_name api.com;
root project/public/;
location / {
index index.php index.html error/index.html;
try_files $uri $uri/ /index.php?$query_string;
autoindex off;
if (!-e $request_filename){
rewrite ^(.*)$ /index.php?s=$1 last; break;
}
}
location ~ \.php(.*)$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_split_path_info ^((?U).+\.php)(/?.+)$;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
include fastcgi_params;
}
}
server {
listen 8085;
server_name 192.168.1.36;
root /data/wwwroot/fengye_app_api/public/;
access_log /var/log/nginx/fengye_app_api.access.log main;
error_log /var/log/nginx/fengye_app_api.error.log info;
location / {
index index.php index.html error/index.html;
try_files $uri $uri/ /index.php?$query_string;
autoindex off;
if (!-e $request_filename){
rewrite ^(.*)$ /index.php?s=$1 last; break;
}
}
location ~ \.php(.*)$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_split_path_info ^((?U).+\.php)(/?.+)$;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
include fastcgi_params;
}
#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 /usr/share/nginx/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;
#}
}
报错: bind() to 0.0.0.0:8085 failed (13: Permission denied)
修改 /etc/selinux/config :SELINUX=enforcing改为SELINUX=disabled 之后重启服务器
还不行
查看http允许访问的端口:
semanage port -l | grep http_port_t
http_port_t tcp 80, 81, 443, 488, 8008, 8009, 8443, 9000
将需要使用的端口8085加入到端口列表中
semanage port -a -t http_port_t -p tcp 8085