unbuntu14.04 nginx开机自动启动

在 /etc/init.d/目录下新建一个nginx脚本文件

执行指令 vi /etc/init.d/nginx  写好脚本后,:wq保存退出

给文件增加可执行权限 chmod +x nginx 

切换到  /etc/init.d/下

 创建软连接  

sysv-rc-conf nginx on

重启系统后 nginx将自动启动

以下为nginx脚步内容 脚本头部的变量可能因系统的不同需要修

 

#! /bin/sh
# Author: rui ding
# Modified: Geoffrey Grosenbach http://www.linuxidc.com
# Modified: Clement NEDELCU
# Reproduced with express authorization from its contributors
set -e
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DESC="nginx daemon"
NAME=nginx
DAEMON=/usr/local/nginx/sbin/$NAME
SCRIPTNAME=/etc/init.d/$NAME


# If the daemon file is not found, terminate the script.
test -x $DAEMON || exit 0

d_start() {
        $DAEMON || echo -n " already running"
}

d_stop() {
        $DAEMON –s quit || echo -n " not running"
}

d_reload() {
        $DAEMON –s reload || echo -n " could not reload"
}

case "$1" in
    start)
echo -n "Starting $DESC: $NAME"
d_start
echo "."
;;
stop)
echo -n "Stopping $DESC: $NAME"
d_stop
echo "."
;;
reload)
echo -n "Reloading $DESC configuration..."
d_reload
echo "reloaded."
;;
restart)
echo -n "Restarting $DESC: $NAME"
d_stop
# Sleep for two seconds before starting again, this should give the
# Nginx daemon some time to perform a graceful stop.
sleep 2
d_start
echo "."
;;
*)
echo "Usage: $SCRIPTNAME {start|stop|restart|reload}" >&2
exit 3
;;
esac
exit 0

 

<think>我们正在回答用户关于在Ubuntu系统上部署Nginx和PHP的配置指南问题。 根据用户提供的引用,特别是引用[1]和引用[3]中提到了在Ubuntu上安装Nginx和PHP(通过php-fpm)的步骤。 我们将按照以下步骤进行: 1. 安装Nginx 2. 安装PHP和php-fpm 3. 配置Nginx以处理PHP请求 4. 测试配置 注意:用户没有指定Ubuntu版本,但引用中提到了14.04和16.04,我们将以较新的Ubuntu版本(如20.04或22.04)为例,但步骤类似。 步骤1:安装Nginx 步骤2:安装PHP和php-fpm(这里我们以PHP7.4为例,用户可以根据需要选择版本) 步骤3:配置Nginx服务器块(server block)来处理PHP 步骤4:重启Nginx服务 步骤5:测试PHP处理 另外,引用[2]中提到了创建Nginx用户和组,但通常安装Nginx时会自动创建。如果需要指定运行用户,可以在nginx.conf中配置。 下面详细说明:</think>### Ubuntu 部署 Nginx 与 PHP 配置指南 以下步骤基于 Ubuntu 22.04 LTS(适用于 20.04+),使用 Nginx + PHP-FPM 架构: --- #### **1. 安装 Nginx 和 PHP** ```bash # 更新系统 sudo apt update && sudo apt upgrade -y # 安装 Nginx sudo apt install nginx -y # 安装 PHP-FPM 及常用扩展(以 PHP 8.1 为例) sudo apt install php-fpm php-mysql php-mbstring php-xml php-curl php-zip -y ``` --- #### **2. 配置 PHP-FPM** - 编辑 PHP-FPM 配置文件(根据 PHP 版本调整路径): ```bash sudo nano /etc/php/8.1/fpm/pool.d/www.conf ``` - 确保以下参数: ```ini user = www-data group = www-data listen = /run/php/php8.1-fpm.sock # 使用 Unix Socket 提升性能 ``` - 重启 PHP-FPM: ```bash sudo systemctl restart php8.1-fpm ``` --- #### **3. 配置 Nginx 处理 PHP 请求** - 创建网站配置文件(以 `example.com` 为例): ```bash sudo nano /etc/nginx/sites-available/example.com ``` - 写入以下配置(关键部分已注释): ```nginx server { listen 80; server_name example.com; root /var/www/html; # 网站根目录 index index.php index.html index.htm; location / { try_files $uri $uri/ =404; } # 处理 PHP 请求 location ~ \.php$ { include snippets/fastcgi-php.conf; # 包含 FastCGI 配置 fastcgi_pass unix:/run/php/php8.1-fpm.sock; # 指向 PHP-FPM Socket fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } # 禁止访问 .htaccess 等敏感文件 location ~ /\.(?!well-known).* { deny all; } } ``` - 启用配置并测试语法: ```bash sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/ sudo nginx -t # 检查配置是否正确 sudo systemctl reload nginx ``` --- #### **4. 测试 PHP 功能** - 创建测试文件: ```bash echo "<?php phpinfo(); ?>" | sudo tee /var/www/html/info.php ``` - 浏览器访问:`http://your_server_ip/info.php` → 显示 PHP 信息页即表示成功 [^1][^3]。 --- #### **5. 安全加固** - **权限设置**: ```bash sudo chown -R www-data:www-data /var/www/html # 确保 Nginx 有权限访问 sudo find /var/www/html -type d -exec chmod 755 {} \; # 目录权限 sudo find /var/www/html -type f -exec chmod 644 {} \; # 文件权限 ``` - **防火墙**: ```bash sudo ufw allow 'Nginx Full' # 放行 HTTP/HTTPS ``` --- #### **常见问题解决** - **502 Bad Gateway**: 检查 PHP-FPM 是否运行:`sudo systemctl status php8.1-fpm` 确认 `fastcgi_pass` 路径与 `www.conf` 中的 `listen` 一致 [^2]。 - **文件上传限制**: 修改 `/etc/php/8.1/fpm/php.ini`: ```ini upload_max_filesize = 64M post_max_size = 64M ``` > 提示:若需部署 Laravel 等框架,需额外配置 `root` 指向 `public/` 目录并启用 URL 重写 [^3]。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值