nginx(编译安装)-自定义安装路径
1.前期准备
安装编译需要的gcc和gcc-c++
yum install -y gcc gcc-c++
nginx依赖
pcre-devel、openssl-devel、zlib-devel
yum install -y pcre pcre-devel openssl openssl-devel zlib zlib-devel
创建用户nginx,以该用户的身份执行nginx
useradd -s /bin/false -M nginx
下载nginx源码包并解压到当前目录
cd /tools
wget http://nginx.org/download/nginx-1.12.0.tar.gz
tar -zxf nginx-1.12.0.tar.gz
2.nginx编译安装
生成Makefile文件
cd nginx-1.12.0
./configure --user=nginx --group=nginx --prefix=/application/nginx-1.12.0/ --with-http_v2_module --with-http_ssl_module --with-http_sub_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre
编译源代码并安装
make && make install
3.后期结尾
添加环境变量
创建nginx命令软链接到环境变量
ln -s /application/nginx/sbin/* /usr/local/sbin/
4.设置开机启动(centos7源码安装的手动建立nginx.service服务文件)
在系统服务目录里创建nginx.service文件
vi /lib/systemd/system/nginx.service
[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
ExecStart=/www/lnmp/nginx/sbin/nginx -c /www/lnmp/nginx/conf/nginx.conf
ExecReload=/www/lnmp/nginx/sbin/nginx -s reload
ExecStop=/www/lnmp/nginx/sbin/nginx -s quit
PrivateTmp=true
[Install]
WantedBy=multi-user.target
在系统服务目录里创建php-fpm.service文件
vi /lib/systemd/system/php-fpm.service
[Unit]
After=network.target
[Service]
Type=simple
PIDFile=/usr/local/php72/var/run/php-fpm.pid
ExecStart=/usr/local/php72/sbin/php-fpm --nodaemonize --fpm-config /usr/local/php72/etc/php-fpm.conf
ExecReload=/bin/kill -USR2 $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
在系统服务目录里创建redis.service文件
vi /lib/systemd/system/redis.service
[Unit]
Description=The redis-server Process Manager
After=syslog.target network.target
[Service]
Type=simple
PIDFile=/var/run/redis_6379.pid
ExecStart=/usr/local/redis/bin/redis-server /usr/local/redis/etc/redis.conf
ExecReload=/bin/kill -USR2 $MAINPID
ExecStop=/bin/kill -SIGINT $MAINPID
[Install]
WantedBy=multi-user.target
开启成功,将服务加入开机自启
systemctl enable nginx.service #注意后面不能跟空格
systemctl enable php-fpm.service
systemctl start nginx.service #启动nginx服务
systemctl enable nginx.service #设置开机自启动
systemctl disable nginx.service #停止开机自启动
systemctl status nginx.service #查看服务当前状态
systemctl restart nginx.service #重新启动服务
systemctl list-units --type=service #查看所有已启动的服务
[Unit]:服务的说明
Description:描述服务
After:描述服务类别
[Service]服务运行参数的设置
Type=forking是后台运行的形式
ExecStart为服务的具体运行命令
ExecReload为重启命令
ExecStop为停止命令
PrivateTmp=True表示给服务分配独立的临时空间
注意:[Service]的启动、重启、停止命令全部要求使用绝对路径
[Install]运行级别下服务安装的相关设置,可设置为多用户,即系统运行级别为3
PHP7.2(编译安装)-自定义安装路径
#######新建www用户和www组
[root@typecodes ~]# groupadd -r www && useradd -r -g www -s /bin/false -d /usr/local/php72 -M php
#####安装编译php7时需要的依赖包
[root@typecodes ~]# yum -y install libxml2 libxml2-devel openssl openssl-devel curl-devel libjpeg-devel libpng-devel freetype-devel libmcrypt-devel
./configure --prefix=/usr/local/php72 --exec-prefix=/usr/local/php72 --bindir=/usr/local/php72/bin --sbindir=/usr/local/php72/sbin --includedir=/usr/local/php72/include --libdir=/usr/local/php72/lib/php --mandir=/usr/local/php72/php/man --with-config-file-path=/usr/local/php72/etc --with-mysql-sock=/var/lib/mysql/mysql.sock --with-mhash --with-openssl --with-mysqli=shared,mysqlnd --with-pdo-mysql=shared,mysqlnd --with-gd --with-iconv --with-zlib --enable-zip --enable-inline-optimization --disable-debug --disable-rpath --enable-shared --enable-xml --enable-bcmath --enable-shmop --enable-sysvsem --enable-mbregex --enable-mbstring --enable-ftp --enable-pcntl --enable-sockets --with-xmlrpc --enable-soap --without-pear --with-gettext --enable-session --with-curl --with-jpeg-dir --with-freetype-dir --enable-opcache --enable-fpm --with-fpm-user=www --with-fpm-group=www --without-gdbm --disable-fileinfo
如果需要安装ts版本则在编译时加上
--enable-maintainer-zts
make && make install
#######直接使用编译后未经优化处理的配置
cp /usr/local/src/php-7.1.5/php.ini-production /usr/local/php7/etc/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
cp /usr/local/src/php-7.1.5/sapi/fpm/php-fpm.service /lib/systemd/system/php-fpm.service
1. 配置php-fpm.conf
php-fpm.conf是php-fpm进程服务的配置文件:
######Pid file的默认前缀是/usr/local/php7/var
pid = run/php-fpm.pid
error_log = /var/log/php-fpm/error.log
include=/usr/local/php7/etc/php-fpm.d/*.conf
2. 配置www.conf(在php-fpm.d目录下)
######设置用户和用户组
user = www
group = www
######根据nginx.conf中的配置fastcgi_pass unix:/dev/shm/php-fpm.sock;设置PHP监听
; listen = 127.0.0.1:9000 #####不建议使用
listen = /dev/shm/php-fpm.sock
listen.owner = www
listen.group = www
listen.mode = 0777
listen.allowed_clients = /dev/shm/php-fpm.sock
######使用静态进程数max_children=内存/512
pm = static
pm.max_children = 100
pm.max_requests = 0
pm.status_path = /fpmStatus
######开启慢日志
slowlog = /var/log/php-fpm/$pool-slow.log
request_slowlog_timeout = 30s
request_terminate_timeout = 0
3. 配置php.ini
######避免PHP信息暴露在http头中
expose_php = Off
######常用配置
error_reporting = E_ALL & ~E_NOTICE
display_errors = Off
log_errors = On
html_errors = Off
error_log = /var/log/php/php-error.log
ignore_repeated_errors = On
ignore_repeated_source = On
post_max_size = 32M
memory_limit = 512M
max_execution_time = 300
file_uploads = On
upload_tmp_dir = /tmp/www
upload_max_filesize = 8M
session.gc_maxlifetime = 14400
######设置PHP的扩展
extension_dir = "/usr/local/php7/lib/php/extensions/no-debug-non-zts-20160303"
extension=mysqli.so
extension=pdo_mysql.so
extension=opcache.so
extension=redis.so
extension=swoole.so
######设置PHP的时区
date.timezone = PRC
######开启opcache
[opcache]
opcache.enable=1
######设置PHP脚本允许访问的目录(需要根据实际情况配置)
;open_basedir = /usr/share/nginx/html;
4 测试php-fpm配置,设置开机自启动
######测试配置
/usr/local/php7/sbin/php-fpm -t
######修改文件权限
chmod 745 /lib/systemd/system/php-fpm.service
######设置为开机启动
systemctl enable php-fpm.service
######启动php-fpm
systemctl start php-fpm.service
######环境变量
ln -s /usr/local/php72/bin/* /usr/local/sbin/
5 安装swoole扩展
swoole github地址:https://github.com/swoole/swoole-src
启用协程Redis客户端 需要安装 hiredis。https://github.com/redis/hiredis
make
make install
ldconfig
编译swoole
phpize
./configure --enable-coroutine --enable-async-redis
make
make install
可能遇到的问题
php-m 发现swoole消失或者是通过php --ri swoole没有显示async redis client
vi ~/.bash_profile
在最后一行添加 export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib
source ~/.bash_profile
环境: docker 拉取的 全新 centos 镜像
```
[root@1acd98156444 swoole]# php -m
PHP Warning: PHP Startup: Unable to load dynamic library 'swoole.so' (tried: /usr/local/php/lib/php/extensions/no-debug-non-zts-20170718/swoole.so (libhiredis.so.0.13: cannot open shared object file: No such file or directory), /usr/local/php/lib/php/extensions/no-debug-non-zts-20170718/swoole.so.so (/usr/local/php/lib/php/extensions/no-debug-non-zts-20170718/swoole.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0
```
解决方案:
1. 编辑 /etc/ld.so.conf 文件,添加 /usr/local/lib
2. 保存之后,执行 ldconfig 命令