linux服务器安装nginx和php
更多文章...

整理备忘。

环境:centos/debian + ngnix + php


首先,本地电脑安装putty软件,官网下载最新版。

云服务器采用密钥登录。利用云服务器管理平台生成并下载的.pem密钥文件,用puttygen程序生成.ppk文件。在putty的auth中导入该文件。这样就可以用putty管理服务器了。

上传和下载文件

用putty带的pscp.exe工具:
"pscp 本地目录或文件 root@服务器IP:路径"

可加入"-r"选项拷贝子目录。

可使用"*"代表所有源文件。

下载文件:把源和目的路径调换一下即可。

安装ngnix
CentOS:
yum -y install nginx
debian:
apt-get update
apt-get -y install nginx

安装好nginx之后,配置ngnix.conf文件,以及配置多站点。(我是在本地电脑上修改然后再上传)

nginx缺省配置:/etc/nginx/nginx.conf
include /etc/nginx/conf.d/*.conf; #读取虚拟站点的配置文件,实现多站点

server{
    listen       80 default_server;
    server_name  _;
    root         /usr/share/nginx/html;

    location ~ \.php$ {  #php的配置,交给本机的fastcgi处理
       fastcgi_pass   127.0.0.1:9000;
       fastcgi_index  index.php;
       fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
       include        fastcgi_params;
    }
}


然后为每个站点都制作一个.conf文件,比如siteA.conf,上传到/etc/nginx/conf.d/
server {
  listen    80;            
  server_name www.siteA.com siteA.com; 
  root /usr/share/nginx/DIR_A;       
  index index.htm index.php; 

  location ~ \.php$ {
    fastcgi_pass   127.0.0.1:9000;
    #debian下需要改成如下:
    #fastcgi_pass   unix:/run/php/php8.2-fpm.sock;  

    fastcgi_index  index.php;
    fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
    include        fastcgi_params;
  }
}


配置改变后,重启nginx:
nginx -s reload (热重启,尝试启动新进程后再切换)
service nginx restart (停掉进程后启动新的进程)

ngnix测试: /usr/sbin/nginx -t

查看进程: ps -ef | grep php-fpm

查看监听端口: netstat -lntp

设置系统启动时自动启动nginx:
systemctl enable nginx


如果要支持https,先申请证书,免费的可以用Let's Encrypt,用邮箱即可申请证书。利用cerbort来实现。
CentOS安装cerbot
yum install -y epel-release
yum install -y certbot

申请证书:
certbot certonly --webroot -w /usr/share/nginx/xxx(网站文件目录) -d 域名 -d www.域名 -m 申请所用邮箱 --agree-tos
免费证书需要三个月重新申请,我是手工重新执行上面的命令。应该有办法自动更新。

然后网站的conf文件中增加
server{
  listen 443 ssl;
  server_name  域名1 域名2;
  root /usr/share/nginx/DIR_A;       
  index index.htm index.php; 
  #ssl  on; 
  ssl_certificate "/etc/letsencrypt/live/域名/fullchain.pem";
  ssl_certificate_key "/etc/letsencrypt/live/域名/privkey.pem";

  #php的设置同80端口
}
上传到conf.d目录中。重启nginx。

debian 安装cerbot
sudo apt update && sudo apt -y install certbot python3-certbot-nginx
申请证书:
certbot --nginx -d 域名 -d www.域名
执行之后自动修改conf文件,现443端口的访问。

配置文件中:
关闭error log:
error_log /dev/null crit;
关闭访问日志:
access_log off;
可以在单独站点配置文件中设置日志。


php安装

debian下安装:
apt install php版本号-fpm (版本号可以不写,系统会指定一个版本)

安装之后需要修改网站的conf文件:
fastcgi_pass unix:/run/php/php版本号-fpm.sock;

安装扩展:apt install php版本号-extension_name


以下是我以前在centos下用的本机编译源码进行安装的方法,可以成功



groupadd -r nginx && useradd -r -g nginx -s /sbin/nologin

php编译

cd /usr/local/src/

wget -c http://cn2.php.net/distributions/php-7.3.16.tar.gz

tar -xzvf php-7.3.16.tar.gz

cd php[tab]

yum -y install libxml2 libxml2-devel

./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --enable-fpm --with-fpm-user=nginx --with-fpm-group=nginx --enable-inline-optimization --disable-debug --enable-calendar --enable-session

make && make install

编译不成功可能需要增加虚拟内存再重新编译:
dd if=/dev/zero of=swapfile bs=1024 count=1M
mkswap swapfile
swapon swapfile



查看安装目录:whereis php

进入安装目录:
cd /usr/local/php
cd etc
cp php-fpm.conf.default php-fpm.conf
cd php-fpm.d
cp www.conf.default www.conf

手工启动php:
/usr/local/php/sbin/php-fpm




附: rar文件解压缩:
安装rar 
centos: yum install rar
debian: apt-get install rar

如果安装不上参考:
https://blog.csdn.net/YEYUANGEN/article/details/111823712

压缩:rar a xxx.rar 文件或目录
解压缩:unrar x xxx.rar
其他:
拷贝目录 cp -r 源目录 目标父目录
强制删除目录:rm -rf




© time.org.cn