环境
主机 | IP |
---|---|
nginx | 192.168.159.100 |
mysql | 192.168.159.139 |
php | 192.168.159.200 |
Nginx部署
配置好yum源
创建nginx用户
[root@100 ~]# useradd -r -M -s /sbin/nologin nginx
安装依赖包和一些常用工具
[root@100 ~]# yum -y install pcre-devel openssl openssl-devel gd-devel gcc gcc-c++ make
安装开发工具包
[root@100 ~]# yum -y groups mark install 'Development Tools'
创建日志存放目录
[root@100 ~]# mkdir -p /var/log/nginx
[root@100 ~]# chown -R nginx.nginx /var/log/nginx
编译安装nginx
[root@100 src]# tar -zxf nginx-1.22.0.tar.gz
[root@100 src]# cd nginx-1.22.0/
[root@100 nginx-1.22.0]# ./configure --prefix=/usr/local/nginx --user=nginx \
> --group=nginx \
> --with-debug \
> --with-http_ssl_module \
> --with-http_realip_module \
> --with-http_image_filter_module \
> --with-http_gunzip_module \
> --with-http_gzip_static_module \
> --with-http_stub_status_module \
> --http-log-path=/var/log/nginx/access.log \
> --error-log-path=/var/log/nginx/error.log
[root@100 nginx-1.22.0]# make && make install
配置环境变量
[root@100 nginx-1.22.0]# echo 'export PATH=/usr/local/nginx/sbin:$PATH' > /etc/profile.d/nginx.sh
[root@100 nginx-1.22.0]# source /etc/profile.d/nginx.sh
启动nginx查看端口
[root@100 nginx-1.22.0]# nginx
[root@100 nginx-1.22.0]# ss -antl
LISTEN 0 128 0.0.0.0:80 0.0.0.0:*
停止nginx,将nginx.conf和mime.types复制到/opt目录下
[root@100 nginx-1.22.0]# nginx -s stop
[root@100 nginx-1.22.0]# cd conf/
[root@100 conf]# cp nginx.conf /opt/
[root@100 conf]# cp mime.types /opt/
[root@100 conf]# nginx -c /opt/nginx.conf
[root@100 conf]# nginx -c /opt/nginx.conf -s stop
二进制安装Mysql
创建用户
[root@139 ~]# useradd -r -M -s /sbin/nologin mysql
安装依赖
[root@139 ~]# yum -y install ncurses-devel openssl-devel openssl cmake mariadb-devel ncurses*
二进制安装mysql
[root@139 ~]# cd /usr/local/
[root@139 local]# tar -zxf mysql-5.7.38-linux-glibc2.12-x86_64.tar.gz
[root@139 local]# ln -s mysql-5.7.38-linux-glibc2.12-x86_64/ mysql
[root@139 local]# chown -R mysql.mysql mysql*
[root@139 local]# ll -d mysql*
lrwxrwxrwx. 1 mysql mysql 36 Oct 11 13:02 mysql -> mysql-5.7.38-linux-glibc2.12-x86_64/
drwxr-xr-x. 9 mysql mysql 129 Oct 11 13:02 mysql-5.7.38-linux-glibc2.12-x86_64
配置环境变量
[root@139 ~]# mkdir /opt/data
[root@139 ~]# chown -R mysql.mysql /opt/data/
[root@139 ~]# echo 'export PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
[root@139 ~]# source /etc/profile.d/mysql.sh
[root@139 ~]# ln -s /opt/mysql/include /usr/include/mysql
[root@139 ~]# echo '/opt/mysql/lib' > /etc/ld.so.conf.d/mysql.conf
[root@139 ~]# vim /etc/man_db.conf
MANDATORY_MANPATH /usr/local/mysql/man
初始化数据库
[root@139 ~]# mysqld --initialize-insecure --user=mysql --datadir=/opt/data
生成配置文件
[root@139 ~]# vim /etc/my.cnf
[mysqld]
basedir = /usr/local/mysql
datadir = /opt/data
socket = /tmp/mysql.sock
port = 3306
pid-file = /opt/data/mysql.pid
user = mysql
skip-name-resolve
配置服务启动脚本
[root@139 ~]# cp -a /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
[root@139 ~]# vim /etc/init.d/mysqld
basedir=/usr/local/mysql
datadir=/opt/data
[root@139 ~]# vim /usr/lib/systemd/system/mysqld.service
[Unit]
Description=mysqld server daemon
After=network.target sshd-keygen.target
[Service]
Type=forking
ExecStart=/etc/init.d/mysqld start
ExecStop=/etc/init.d/mysqld stop
ExecReload=/bin/kill -HUP $MAINPID
[Install]
WantedBy=multi-user.target
[root@139 ~]# systemctl daemon-reload
[root@139 ~]# systemctl start mysqld
[root@139 ~]# ss -antl
LISTEN 0 80 *:3306 *:*
修改数据库密码
[root@139 ~]# mysql -uroot
mysql> set password = password('123456');
源码部署PHP
安装依赖
[root@200 ~]# yum -y install openssl openssl-devel bzip2 bzip2-devel libcurl libcurl-devel libicu-devel libjpeg libjpeg-devel libpng libpng-devel openldap-devel pcre-devel freetype freetype-devel gmp gmp-devel libxml2-devel gcc gcc-c++ make sqlite-devel libzip-devel
[root@200 ~]# yum -y install http://mirror.centos.org/centos/8-stream/PowerTools/x86_64/os/Packages/oniguruma-devel-6.8.2-2.el8.x86_64.rpm
编译安装PHP
[root@200 ~]# cd /usr/src/
[root@200 src]# tar -xf php-7.4.30.tar.xz
[root@200 src]# cd php-7.4.30/
[root@200 php-7.4.30]# ./configure --prefix=/usr/local/php7 \
--with-config-file-path=/etc \
--enable-fpm \
--enable-inline-optimization \
--disable-debug \
--disable-rpath \
--enable-shared \
--enable-soap \
--with-openssl \
--enable-bcmath \
--with-iconv \
--with-bz2 \
--enable-calendar \
--with-curl \
--enable-exif \
--enable-ftp \
--enable-gd \
--with-jpeg \
--with-zlib-dir \
--with-freetype \
--with-gettext \
--enable-json \
--enable-mbstring \
--enable-pdo \
--with-mysqli=mysqlnd \
--with-pdo-mysql=mysqlnd \
--with-readline \
--enable-shmop \
--enable-simplexml \
--enable-sockets \
--with-zip \
--enable-mysqlnd-compression-support \
--with-pear \
--enable-pcntl
[root@200 php-7.4.30]# make && make install
配置编译环境
[root@200 php-7.4.30]# echo "export PATH=$PATH:/usr/local/php7/bin" > /etc/profile.d/php.sh
[root@200 php-7.4.30]# source /etc/profile.d/php.sh
[root@200 php-7.4.30]# ln -s /usr/local/php7/include/ /usr/include/php
[root@200 php-7.4.30]# echo "/usr/local/php7/lib" > /etc/ld.so.conf.d/php.conf
配置服务启动脚本
[root@200 php-7.4.30]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
[root@200 php-7.4.30]# chmod +x /etc/init.d/php-fpm
[root@200 php-7.4.30]# cp /usr/local/php7/etc/php-fpm.conf.default /usr/local/php7/etc/php-fpm.conf
[root@200 php-7.4.30]# cp /usr/local/php7/etc/php-fpm.d/www.conf.default /usr/local/php7/etc/php-fpm.d/www.conf
[root@200 php-7.4.30]# vim /usr/lib/systemd/system/php-fpm.service
[Unit]
Description=php-fpm server daemon
After=network.target sshd-keygen.target
[Service]
Type=forking
ExecStart=/etc/init.d/php-fpm start
ExecStop=/etc/init.d/php-fpm stop
ExecReload=/bin/kill -HUP $MAINPID
[Install]
WantedBy=multi-user.target
[root@200 php-7.4.30]# systemctl daemon-reload
[root@200 php-7.4.30]# systemctl start php-fpm.service
[root@200 php-7.4.30]# systemctl enable php-fpm.service
配置php
[root@200 ~]# vim /usr/local/php7/etc/php-fpm.d/www.conf
listen = 0.0.0.0:9000
listen.allowed_clients = 192.168.159.100
配置测试页面
[root@200 php-7.4.30]# vim /var/www/html/index.php
<?php
phpinfo();
?>
[root@200 php-7.4.30]# chown -R apache.apache /var/www/html/
[root@200 php-7.4.30]# systemctl restart php-fpm
[root@200 php-7.4.30]# systemctl enable php-fpm
整合nginx和php
[root@100 ~]# vim /usr/local/nginx/conf/nginx.conf
location / {
root html;
index index.html index.htm index.php;
}
location ~ \.php$ {
root /var/www/html;
fastcgi_pass 192.168.159.200:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www/html/$fastcgi_script_name;
include fastcgi_params;
}
测试nginx配置文件
[root@100 ~]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@100 ~]# nginx
[root@100 ~]# ss -antl
LISTEN 0 128 0.0.0.0:80 0.0.0.0:*