Lnmp编译安装(2020)

  之前写过几篇有关安装lnmp环境的文章,现在回顾感觉有些比较老了或者并不是好的操作,于是更新这篇

1. 安装Nginx

  1) 依赖安装

yum install gcc gcc-c++ openssl openssl-devel cyrus-sasl-md5

  2)下载nginx

wget http://nginx.org/download/nginx-1.12.0.tar.gz

  3)解压并进入安装目录

tar zxvf nginx-1.12.0.tar.gz
cd nginx-1.12.0

  4) 编译安装

./configure --prefix=/usr/local/nginx --with-http_ssl_module

make && make install -j2

  5) 启动并查看服务

/usr/local/nginx/sbin/nginx


ps -ef | grep nginx
扫描二维码关注公众号,回复: 9739622 查看本文章

  6)加入开机自启动

vim  /etc/rc.local
添加代码如下:
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf &

  7)加入全局环境变量

vim /etc/profile

export=$PATH:/usr/local/nginx/sbin

保存退出


立即生效: source /etc/profile

  8) 验证环境变量

nginx -v

nginx version: nginx/1.12.0

2. 安装PHP

   1) 安装依赖包

yum -y install bzip2

  2)下载PHP

wget https://www.php.net/distributions/php-7.2.28.tar.bz2

tar txvf php-7.2.28.tar.bz2

cd php-7.2.28

  3) 创建www用户

groupadd  www

useradd -g www -s /sbin/nologin www


# 查看www用户
id www

  4)编译安装

./configure --prefix=/usr/local/php7 --with-config-file-path=/usr/local/php7 --enable-sockets --enable-fpm --enable-cli --enable-mbstring --enable-pcntl --enable-soap --enable-opcache --disable-fileinfo --disable-rpath --with-mysqli --with-pdo-mysql --with-iconv-dir --with-openssl --with-fpm-user=www --with-fpm-group=www --with-curl --with-mhash --with-gd --with-jpeg-dir --with-png-dir --with-freetype-dir --enable-zip --with-zlib --enable-simplexml --with-libxml-dir

make && make install

  说明:

--prefix=/usr/local/php7 # 配置安装目录
--with-config-file-path=/usr/local/php7 # 配置文件 php.ini 的路径
--enable-sockets # 开启 socket 
--enable-fpm # 启用 fpm 扩展
--enable-cli # 启用 命令行模式 (从 php 4.3.0 之后这个模块默认开启所以可以不用再加此命令)
--enable-mbstring # 启用 mbstring 库
--enable-pcntl # 启用 pcntl (仅 CLI / CGI)
--enable-soap # 启用 soap 
--enable-opcache # 开启 opcache 缓存
--disable-fileinfo # 禁用 fileinfo (由于 5.3+ 之后已经不再持续维护了,但默认是开启的,所以还是禁止了吧)(1G以下内存服务器直接关了吧)
--disable-rpath  #禁用在搜索路径中传递其他运行库。
--with-mysqli # 启用 mysqli 扩展
--with-pdo-mysql # 启用 pdo 扩展
--with-iconv-dir # 启用 XMLRPC-EPI 字符编码转换 扩展
--with-openssl # 启用 openssl 扩展 (需要 openssl openssl-devel)
--with-fpm-user=www #设定 fpm 所属的用户 
--with-fpm-group=www #设定 fpm 所属的组别
--with-curl # 启用 curl 扩展
--with-mhash # 开启 mhash 基于离散数学原理的不可逆向的php加密方式扩展库
# GD
--with-gd # 启用 GD 图片操作 扩展
--with-jpeg-dir # 开启对 jpeg 图片的支持 (需要 libjpeg)
--with-png-dir # 开启对 png 图片支持 (需要 libpng)
--with-freetype-dir # 开启 freetype 
# 压缩
--enable-zip # 启用 zip
--with-zlib # 启用对 zlib 支持 
# xml
--enable-simplexml # 启用对 simplexml 支持
--with-libxml-dir # 启用对 libxml2 支持

  5)设置配置文件

cp /opt/php-7.2.28/php.ini-production /usr/local/php7/php.ini

cp /usr/local/php7/etc/php-fpm.conf.default  /usr/local/php7/etc/php-fpm.conf

cp  /usr/local/php7/etc/php-fpm.d/www.conf.default    /usr/local/php7/etc/php-fpm.d/www.conf

  php-fpm.conf修改如下:

[global]
; Pid file
; Note: the default prefix is /usr/local/php7/var
; Default Value: none
# 取消下面的注释
pid = run/php-fpm.pid

  6) 配置简单启动命令

# 复制启动脚本到 init.d 目录
$ cp php-7.2.19/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
# 增加执行权限
$ chmod +x /etc/init.d/php-fpm

  php-fpm操作命令:

/usr/local/php7/sbin/php-fpm.  # 启动
pkill. -9 php-fpm #停止

/etc/init.d/php-fpm start. # 启动
/etc/init.d/php-fpm stop  # 停止
/etc/init.d/php-fpm status # 状态
/etc/init.d/php-fpm restart # 重启

  7)添加自启动

/sbin/chkconfig php-fpm on #再次重启即可完成自启动

  8)php-fpm服务化

# 在 centos 7 之后我们可以使用 systemctl 更好的管理系统服务
# 所以我们也要让 php-fpm 支持
# 因为 php 7.2 源码包里面含有 systemctl 所需要的脚本文件
# 我们只要复制过去即可,我们来开始配置
# 进入下载的 php源码包
$ cd php-7.2.19/sapi/fpm
# 复制其中的 php-fpm.service 到 /usr/lib/systemd/system/
$ cp php-fpm.service /usr/lib/systemd/system/
# 再次使用 systemctl enable php-fpm 进行配置自启动
$ systemctl enable php-fpm
# 重启测试一下看看自己服务器的 php-fpm 是否成功运行
systemctl enable xxxxxx # 配置自启动
systemctl stop xxxxx # 停止服务
systemctl start xxxx # 开启服务
systemctl status xxxx # 查看状态

  9)nginx支持配置

$ vi /usr/local/nginx/conf/nginx.conf
...
http {
...
    server {
... 
# 把 [1] 换成 [2]
# 让 nginx 支持 php 文件
#[1]
                location / {
            root   html;
                   index  index.html index.htm;
        }
#[2]
        location / {
            root   html;
                   index  index.php index.html index.htm;
        }
...
# 把 [1] 换成 [2]
# 配置监听 php 端口
# 注意: 要把 /scripts 换成 $document_root
#[1]
        #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;
        #}
#[2]
        location ~ \.php$ {
            root           html;
               fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }
...
}

  

猜你喜欢

转载自www.cnblogs.com/xingxia/p/lnmp2020.html