一、 Nginx 源码编译安装

1.1 建立www用户

useradd www -s /sbin/nologin -M
#-s 指定shell,禁止登陆系统
#-M 表示不创建家目录

1.2 安装编译依赖

yum install -y pcre-devel openssl openssl-devel zlib-devel gcc gcc-c++
#通过yum安装源代码编译器依赖

1.3 编译安装

./configure --prefix=/apps/nginx \
--with-http_ssl_module \
--with-http_v2_module \
--with-http_realip_module \
--with-http_stub_status_module \
--with-http_gzip_static_module \
--with-pcre \
--with-stream \
--with-stream_ssl_module \
--with-stream_realip_module
#指定安装目录 

--with-http_ssl_module
#支持SSL(HTTPS)

--with-http_v2_module
#支持HTTP2

--with-http_stub_status_module
#统计功能模块,可以读取nginx服务当前处理的连接数等信息。(配合zabbix使用)

make
#预编译,生成可二进制可执行安装文件
make install
#运行二进制服务程序安装包
make && make install
#预编译后直接开始安装,比较省事。

1.4 为nginx指定www用户运行

vim /usr/local/nginx/conf/nginx.conf
打开nginx配置文件

user www;
#修改为www用户

1.5 使用systemctl管理nginx

创建nginx.service文件,保存到/usr/lib/systemd/system/目录中,文件内容如下:

[Unit]
Description=nginx service
After=network.target

[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true

[Install]
WantedBy=multi-user.target

或使用EOF命令:

cat << EOF > /usr/lib/systemd/system/nginx.service
[Unit]
Description=nginx service
After=network.target

[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true

[Install]
WantedBy=multi-user.target
EOF

文件创建完毕后,重新加载配置。

systemctl daemon-reload    #重新加载systemnctl配置

1.6 添加模块/重新编译安装

进入nginx源码包目录
/configure --add-module=模块目录路径
#增加新的模块,重新编译。

make
#进行编译#make后不要执行make install,若这样操作会覆盖现有安装。

进入objs目录
cp nginx /usr/local/nginx/sbin/nginx
#编译完成后,新编译好的nginx文件被保存在objs目录中,将新生成的nginx文件去覆盖旧版本,即可完成版本升级或模块安装。

1.7 防火墙配置

firewall-cmd --permanent --add-service=http
firewall-cmd --permanent --add-service=https
firewall-cmd --reload
#在firewall防火墙里,放行http、https流量。

1.8 启动服务

systemctl start nginx    #启动nginx
systemctl restart nginx    #重启nginx
systemctl stop nginx    #停止nginx
systemctl enable nginx    #开机启动
systemctl disable nginx    #取消开机启动
systemctl is-enabled nginx    #查看是否开机启动
systemctl status nginx    #查看服务状态

1.9 配置虚拟主机

建立vhosts文件夹,用于存放虚拟主机配置文件。

mkdir /usr/local/nginx/vhosts

编辑nginx主配置文件

在http块中添加包含虚拟主机配置文件

include /usr/local/nginx/vhosts/*.conf;

在vhosts文件夹中创建虚拟主机配置文件,必须以*.conf结尾

cd /usr/local/nginx/vhosts/
vim abc.com.conf

虚拟主机配置文件格式

server{
    listen 80;
    #监听80端口
    server_name abc.com;
    #主机名
    
    location /{
        root /var/www/abc.com;
        #网站文件目录
        
        index index.php;
        #主页文件名称
    }
    
    #以下配置php支持
    location ~ \.php$ {
        root           /var/www/abc.com;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;      
    }
}

二、MySQL源码编译安装

2.1 卸载MariaDB

CentOS中默认安装了MariaDB,在安装MySQL之前先进行卸载。
MariaDB删除之后/etc/my.cnf也会被删除。

rpm -qa | grep mariadb
#检索已安装的MariaDB包名

yum remove mariadb-libs-5.5.60-1.el7_5.x86_64
#使用yum命令卸载MariaDB

2.2 建立mysql用户

useradd mysql -s /sbin/nologin -M
#-s 指定shell,禁止登陆系统
#-M 表示不创建家目录

2.3 安装编译依赖

MySQL5.7使用CMake代替了configure。

yum install -y gcc gcc-c++ pcre pcre-devel openssl openssl-devel zlib zlib-devel cmake ncurses ncurses-devel bison bison-devel perl perl-devel autoconf
#安装编译器依赖

2.4 编译安装

cmake -DWITH_BOOST=/opt/src/mysql-5.7.28/boost
#开始预编译,指定了boost库的路径。这是最简化的预编译方式,仅仅指定了boost库的路径(同时这也是必须的)。未指定的参数,系统将使用默认值来进行编译。

cmake -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_BOOST=/opt/src/mysql-5.7.28/boost
#编译参数:默认字符utf-8
#

make
#预编译,生成可二进制可执行安装文件
make install
#运行二进制服务程序安装包
make -j `nproc` && make install
#预编译后直接开始安装,比较省事。

2.5 MySQL的默认目录

1.程序目录:/usr/local/mysql    #默认
2.数据目录:/var/lib/mysql    #默认
3.日志目录:/var/log/mariadb/mariadb.log    #CentOS7默认,如若修改,需在my.cnf中同步修改

2.6 MySQL配置文件my.cnf

cat << EOF > /etc/my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd

[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

#
# include all files from the config directory
#
EOF

2.7 建立日志、PID文件目录

mkdir -p /var/log/mysql  /var/run/mysql
#建立日志目录和pid文件目录
chown -R mysql:mysql /var/log/mysql/ /var/run/mysql/
#修改日志文件目录和pid文件目录和pid文件的属组。

2.8 环境变量

MySQL的许多命令都存放在bin目录下,同时MySQL还有许多库文件,存放在lib目录下。而这些命令和库是MySQL运行中不可缺少的。

vim /etc/profile 
#CentOS 7的环境变量保存在/etc/profile文件中,我们需要在文件的末尾追加2个条目,分别为bin目录的位置和lib目录的位置。

方法1:
export PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH
#增加环境变量

方法2:
echo "export PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH" >> /etc/profile
#使用输出重定向,在/etc/profile末尾添加MySQL环境变量。

source /etc/profile
#source命令用于重新加载环境变量。

方法3:
ln -s /usr/local/mysql /usr/bin/
#直接软连接到/usr/bin下,可以不用添加环境变量。每个需要的命令都要做软连接,此处省略。

2.9 初始化MySQL

mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/var/lib/mysql
#--initialize 初始化mysql,若使用--initialize-insecure参数,则表示没有初始密码。
#--user=<指定MySQL数据库用户>
#--basedir=<指定MySQL主程序目录>
#--datadir=<MySQL数据库文件目录>

2.10 链接sock文件

ln -s /var/lib/mysql/mysql.sock /tmp/mysql.sock
#可使用find / -name mysql.sock命令进行搜索,然后链接到/tmp/mysql.sock中。
#或直接在my.cnf中修改sock文件位置为/tmp/mysql.sock

2.11 加入init.d,并开机自启动

cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
#复制server文件到init.d
chkconfig --add mysqld
#增加mysqld服务
#chkconfig命令用于检查,设置系统的各种服务。这是Red Hat公司遵循GPL规则所开发的程序,它可查询操作系统在每一个执行等级中会执行哪些系统服务,其中包括各类常驻服务。
#--add  增加所指定的系统服务,让chkconfig指令得以管理它,并同时在系统启动的叙述文件内增加相关数据。
#--del  删除服务
#--list  查看当前服务状态
chkconfig mysqld on  #开启mysqld服务。    

2.12 使用systemctl管理

复制mysql.service文件到/usr/lib/systemctl/system/目录中,内容如下:

find / -name mysql.service
#用find命令查找mysql.service文件
#此文件在执行过chkconfig --add mysqld命令才会后生成,如果搜索不到,需要执行2.8步骤,但不必执行chkconfig mysqld on命令,因为可以使用systemctl进行管理。

cp /run/systemd/generator.late/mysql.service /usr/lib/systemd/system/ 
#把搜索到的文件复制到/usr/lib/systemd/system/目录下

systemctl daemon-reload
#重载systemctl配置

2.13 启动服务

systemctl start mysqld    #启动mysql
systemctl restart mysqld    #重启mysql
systemctl stop mysqld    #停止mysql
systemctl enable mysqld    #开机启动
systemctl disable mysqld    #取消开机启动
systemctl is-enabled mysqld    #查看是否开机启动
systemctl status mysqld    #查看服务状态

三、PHP源码编译安装

3.1 建立www用户

useradd www -s /sbin/nologin -M
#-s 指定shell,禁止登陆系统
#-M 表示不创建家目录

3.2 安装编译依赖

yum install -y epel-release
#安装epel源
yum groupinstall -y 'Development Tools'
yum install -y libxml2 libxml2-devel sqlite-devel oniguruma-devel libcurl libcurl-devel readline readline-devel gd gd-devel cmake3 openssl-devel

3.3 cmake安装或升级

#如果已经安装,则需要先卸载。
yum remove cmake -y

#从GitHub下载或将源码包上传
git地址:https://github.com/Kitware/CMake/releases

#或直接运行下面的命令:
wget https://github.com/Kitware/CMake/releases/download/v3.19.0-rc1/cmake-3.19.0-rc1.tar.gz

#依赖openssl-devel
yum install openssl-devel

#编译安装
tar -zxvf cmake-3.19.0-rc1.tar.gz
cd cmake-3.19.0-rc1
./configure
make -j `nproc` && make install

#查看版本号
cmake --version

#加入环境变量
echo "export PATH=/usr/loacl/bin:$PATH" >> /etc/profile && source /etc/profile

3.3.1 使用yum安装cmake3

或直接使用yum进行cmake3的安装
yum install -y cmake3

如使用此方式安装,需要使用“cmake3”命令进行操作。

3.4 libzip安装或升级

需要 CMake 3.0.2 版本或更高.

#如果已经安装,则需要先卸载。
yum remove -y libzip libzip-devel

#从官网下载或将源码包上传
官网:https://libzip.org/download/

#或直接运行下面的命令:
wget https://libzip.org/download/libzip-1.5.1.tar.gz

#安装依赖,如提示cmake版本过久见上文更新cmake。
yum install cmake

#编译安装
tar -zxvf libzip-1.5.1.tar.gz
cd libzip-1.5.1
mkdir build
cd build
cmake ..
make -j `nproc` && make install

#添加libzip环境变量
echo "export PKG_CONFIG_PATH='/usr/local/lib64/pkgconfig/'" >> /etc/profile && source /etc/profile
#如果不添加libzip到环境变量,预编译PHP时会提示找不到libzip库

3.5 PHP编译参数

查看所有编译参数:./configure --help

./configure --prefix=/usr/local/php --enable-fpm --enable-inline-optimization --disable-debug --disable-rpath --enable-shared --enable-opcache --with-mysqli --with-mysql-sock --enable-pdo --with-pdo-mysql --with-gettext --enable-mbstring --with-iconv --with-mhash --with-openssl --enable-bcmath --enable-soap --enable-pcntl --enable-shmop --enable-sysvmsg --enable-sysvsem --enable-sysvshm --enable-sockets --with-curl --with-zlib --with-readline --without-sqlite3 --without-pdo-sqlite --with-pear --with-libdir=/lib/x86_64-linux-gnu --enable-xml --enable-gd --with-zip
#不同版本的写法有区别:
#php7.3 --enable-zip --with-gd
#php7.4 --with-zip --enable-gd

3.6 编译安装

./configure --enable-fpm --with-pdo-mysql --with-mysqli
#这里我们需要增加php-fpm和mysql数据库支持。启用mysqli扩展技术不仅可以调用MySQL的存储过程、处理MySQL事务,而且还可以使访问数据库工作变得更加稳定。

make
#预编译,生成可二进制可执行安装文件
make install
#运行二进制服务程序安装包
make -j `nproc` && make install
#预编译后直接开始安装,比较省事。

3.7 编译报错

如果预编译完成但是make报错,可使用make distclean清除后重新编译。

3.7.1 其他问题

#报错:configure: error: off_t undefined; check your library configuration
#报错:./php-fpm: error while loading shared libraries: libonig.so.5: cannot open shared object file: No such file or directory

# 添加搜索路径到配置文件
echo '/usr/local/lib64
/usr/local/lib
/usr/lib
/usr/lib64'>>/etc/ld.so.conf
# 更新配置
ldconfig -v

3.8 复制配置文件

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

修改php-fpm.conf配置文件的最后一行为:
vim /usr/local/php/etc/php-fpm.conf
include=/usr/local/php/etc/php-fpm.d/*.conf


由模板复制一份www.conf配置文件。 www.conf这是php-fpm进程服务的扩展配置文件
cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf
#复制一份www.conf配置文件


需要修改 www.conf 配置文件,确保 php-fpm 模块使用 www-data 用户和 www-data 用户组的身份运行。
vim /usr/local/php/etc/php-fpm.d/www.conf
找到以下内容并修改:

; Unix user/group of processes
; Note: The user is mandatory. If the group is not set, the default user's group
;       will be used.
23 user = www
24 group = www

搜索php.ini配置文件,并复制到/usr/local/php/lib/目录下
find / -name php.ini*
php.ini有2个版本,一个开发环境,一个生产环境。

cp /opt/php-7.4.0/php.ini-development /usr/local/php/lib/php.ini
#复制php.ini配置文件

3.9 添加环境变量

vim /etc/profile    #打开环境变量配置文件
export PATH=$PATH:/usr/local/php/bin  #在末尾加入
source /etc/profile  #重新载入环境变量

或直接执行如下命令:

echo "export PATH=$PATH:/usr/local/php/bin" >> /etc/profile && source /etc/profile

3.10 启动 php-fpm 服务

/usr/local/php/sbin/php-fpm

3.11 使用systemctl管理php-fpm

建立php-fpm.service文件,内容如下。

[Unit]
Description=php
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/php/sbin/php-fpm
ExecStop=/bin/pkill -9 php-fpm
ExecReload=/bin/kill -USR2 $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target

放置到/usr/lib/systemd/system/目录中

cp php-fpm.service /usr/lib/systemd/system/

或使用下面的命令:

cat << EOF > /usr/lib/systemd/system/php-fpm.service
[Unit]
Description=php
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/php/sbin/php-fpm
ExecStop=/bin/pkill -9 php-fpm
ExecReload=/bin/kill -USR2 $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
EOF

systemctl配置重载命令:

systemctl daemon-reload

3.12 启动服务

systemctl start php-fpm    #启动php-fpm
systemctl restart php-fpm    #重启php-fpm
systemctl stop php-fpm    #停止php-fpm
systemctl enable php-fpm    #开机启动
systemctl disable php-fpm    #取消开机启动
systemctl is-enabled php-fpm    #查看是否开机启动
systemctl status php-fpm    #查看服务状态

3.13 配置nginx的php支持

打开Nginx配置文件,修改如下位置:

vim /usr/local/nginx/conf/nginx.conf
#打开nginx配置文件

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
    root           /var/www/html;    #修改为网站目录
    fastcgi_pass   127.0.0.1:9000;
    fastcgi_index  index.php;
#   fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
    include        fastcgi_params;
}

使用socket通信

nginx与php安装在同一台服务器上时,可以使用socket进行通信,节省开销。

php配置
修改配置文件:/php/etc/php-fpm.d/www.conf
listen = 127.0.0.1:9000
修改为↓
listen = /dev/shm/php-fpm.sock
重启php服务
#将sock文件放在/dev/shm是因为这个目录运行在内存中,速度更快。

nginx配置
fastcgi_pass   127.0.0.1:9000;
修改为↓
fastcgi_pass   unix:/dev/shm/php-fpm.sock;
重启nginx服务

3.14 测试nginx的php支持

echo "<?php phpinfo(); ?>" > /var/www/html/index.php

将此行内容保存为index.php,通过浏览器访问。

3.15 配置mysql字符编码

在my.cnf中的[mysqld]下增加如下参数:

[mysqld]
init_connect='SET collation_connection = utf8_unicode_ci'
init_connect='SET NAMES utf8'
character-set-server=utf8
collation-server=utf8_unicode_ci
skip-character-set-client-handshake

重启数据库服务
systemctl restart mysqld

查看数据库是否支持utf8字符编码
show variables like "%character%";show variables like "%collation%";

3.16 使用测试文件连接mysql

<?php
$link=mysqli_connect("localhost:3306","root","12345","mysql");
if(! $link){
    echo "连接失败" .PHP_EOL;
    echo "Debugging errno:" .mysqli_connect_errno() .PHP_EOL;
    echo "Debugging error:" .mysqli_connect_errno() .PHP_EOL;
    exit;  
}
else{
    echo "连接成功" .PHP_EOL;
    echo "Host information" .mysqli_get_host_info($link) .PHP_EOL;
}
mysqli_close($link);
?>

标签: none

添加新评论