多主机部署lnmp
server | IP |
---|---|
nginx | 172.20.27.10 |
php-fpm | 172.20.27.20 |
mysql | 172.20.27.30 |
编译安装nginx
1.下载nginx
[root@nginx ~]# wet http://nginx.org/download/nginx-1.17.0.tar.gz
2.解压
[root@nginx ~]# tar -xf nginx-1.17.0.tar.gz
3.检查编译环境和编译所需要的工具
[root@nginx nginx-1.17.0]# ./configure --prefix=/apps/nginx --user=nginx --group=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
4.生成模块并复制模块到指定目录
[root@nginx nginx-1.17.0]# make && make install
5.创建nginx用户
[root@nginx nginx-1.17.0]# useradd -u 2000 nginx
6.将执行文件关联到sbin下
[root@localhost nginx-1.17.0]# ln -s /apps/nginx/sbin/nginx /sbin/nginx
7.配置服务脚本
使用yum安装的服务脚本进行修改
[root@localhost ~]# vim /usr/lib/systemd/system/nginx.service
[Unit]
Description=The nginx HTTP and reverse proxy server
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
PIDFile=/apps/nginx/logs/nginx.pid #修改为nginx编译安装的目录
# Nginx will fail to start if /run/nginx.pid already exists but has the wrong
# SELinux context. This might happen when running `nginx -t` from the cmdline.
# https://bugzilla.redhat.com/show_bug.cgi?id=1268621
ExecStartPre=/usr/bin/rm -f /run/nginx.pid
ExecStartPre=/apps/nginx/sbin/nginx -t #修改nginx的路径
ExecStart=/apps/nginx/sbin/nginx -c /apps/nginx/conf/nginx.conf #修改nginx路径,并添加配置文件
ExecReload=/bin/kill -s HUP $MAINPID
KillSignal=SIGQUIT
TimeoutStopSec=5
KillMode=process
PrivateTmp=true
[Install]
WantedBy=multi-user.target
8.修改配置文件
[root@nginx nginx-1.17.0]# sed -in 's/#user.*/user nginx;/p' /apps/nginx/conf/nginx.conf
[root@nginx nginx-1.17.0]# sed -in 's@#pid.*@pid /apps/nginx/logs/nginx.pid;@p' /apps/nginx/conf/nginx.conf
9.启动服务
[root@localhost ~]# systemctl start nginx
[root@localhost ~]# ss -tnl | grep 80
LISTEN 0 128 *:80 *:*
编译安装php
1.解压源码包
[root@php ~]# tar xf php-7.3.5.tar.bz2
2.检查编译环境和编译所需的工具
[root@php php-7.3.5]# ./configure --prefix=/app/php --enable-mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-openssl --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d --enable-mbstring --enable-xml --enable-sockets --enable-fpm --enable-maintainer-zts --disable-fileinfo
3.生成模块并将模块复制到指定目录
[root@php php-7.3.5]# make && make install
4.复制启动配置文件并修改
[root@php php-7.3.5]# cp php.ini-production /etc/php.ini
[root@php php-7.3.5]# sed -i '/;date.tim/s@.*@data.timezone = "Asia/Shanghai"@' /etc/php.ini
5.复制服务器脚本,并配置为开机启动
[root@php php-7.3.5]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
[root@php php-7.3.5]# chmod +x /etc/init.d/php-fpm
[root@php php-7.3.5]# chkconfig --add /etc/init.d/php-fpm
6.复制php配置文件
[root@php php-7.3.5]# cp /app/php/etc/php-fpm.conf.default /app/php/etc/php-fpm.conf
[root@php php-7.3.5]# cp /app/php/etc/php-fpm.d/www.conf.default /app/php/etc/php-fpm.d/www.conf
二进制安装MySQL
1.创建MySQL用户
[root@mysql ~]# useradd -r -s /sbin/nologin mysql
2.创建相关的目录,并修改权限
[root@mysql ~]# midir /data/mysql
[root@mysql ~]# mkdir /data/mysql
[root@mysql ~]# chmod 700 /data/mysql
[root@mysql ~]# chown -R mysql.mysql /data/mysql
3.解压二进制安装包
[root@mysql ~]# tar xf mariadb-10.2.23-linux-x86_64.tar.gz -C /usr/local
4.为二进制安装目录创建软连接并修改权限
[root@mysql ~]# ln -s /usr/local/mariadb-10.2.23-linux-x86_64 /usr/local/mysql
[root@mysql ~]# chown -R root.root /usr/local/mysql
5.复制配置文件到指定目录,并修改配置文件
[root@mysql ~]# cp /usr/local/mysql/support-files/my-huge.cnf /etc/mysql/my.cnf
[root@mysql ~]# vim /etc/mysql/my.cnf
[mysqld]
datadir=/data/mysql
6.复制服务启动脚本到指定目录,设置为开机启动
[root@mysql ~]# cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
[root@mysql ~]# chkconfig --add mysqld
7.初始化MySQL
[root@mysql mysql]# ./scripts/mysql_install_db --user=mysql --datadir=/data/mysql
8.启动MySQL服务
[root@mysql mysql]# service mysqld start
Starting mysqld (via systemctl): [ OK ]
9.授权测试用户
[root@mysql ~]# /usr/local/mysql/bin/mysql -e "GRANT ALL PRIVILEGES ON *.* TO 'root'@'172.20.27.30' IDENTIFIED BY '111111';"
配置LAMP
1.配置nginx
[root@nginx ~]# mkdir /apps/nginx/conf/server
[root@nginx ~]# vim /apps/nginx/conf/server/mylinuxops.conf
server {
server_name www.mylinuxops.com;
listen 80;
location / {
root /data/www;
index index.html;
}
location ~ \.php$ {
root /data/www/php;
fastcgi_pass 172.20.27.20:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
[root@nginx ~]# vim /apps/nginx/conf/nginx.conf
http{
......
include /apps/nginx/conf/server/*.conf;
}
启动服务
[root@nginx ~]# nginx -s reload
2.配置php
创建nginx用户
[root@php ~]# useradd -u 2000 nginx
修改php-fpm配置文件
[root@php ~]# vim /app/php/etc/php-fpm.d/www.conf
user = nginx
group = nginx
listen = 172.20.27.20:9000
;listen.allowed_clients = 127.0.0.1
启动服务
[root@php ~]# service php-fpm start
Starting php-fpm done
测试
在nginx服务器上创建html测试页
[root@nginx ~]# mkdir /data/www
[root@nginx ~]# echo mylinuxops.com > /data/www/index.html
在php-fpm服务器上创建php测试页
[root@php ~]# mkdir /data/www/php
[root@php ~]# vim /data/www/php/index.php
<?php
$dsn='mysql:host=172.20.27.30;dbname=test';
$username='root'; $passwd='111111';
$dbh=new PDO($dsn,$username,$passwd);
var_dump($dbh);
phpinfo();
?>
测试访问
转载于:https://blog.51cto.com/11886307/2403950