LNMP编译安装

    科技2024-10-29  13

    LNMP代表的就是:Linux系统下Nginx+MySQL+PHP这种网站服务器架构 nginx 先把包放到opt目录下

    root@localhost opt]# cd /opt root@localhost opt]# tar xzvf nginx-1.15.9.tar.gz [root@localhost nginx-1.15.9]# useradd -M -s /bin/nologin nginx [root@localhost opt]# yum -y install gcc gcc-c++ make pcre-devel zlib-devel [root@localhost opt]# cd nginx-1.15.9/ ./configure \ --prefix=/usr/local/nginx \ --user=nginx \ --group=nginx \ --with-http_stub_status_module

    编译以及路径优化

    make && make install ##为了使 Nginx 服务器的运行更加方便, 可以为主程序 nginx 创建链接文件, 以便管理员直接执行 "nginx" 命令就可以调用Nginx的主程序 [root@localhost nginx-1.15.9]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/ [root@localhost nginx-1.15.9]# ls -l /usr/local/sbin/nginx [root@localhost nginx-1.15.9]# nginx -t ### 检查配置文件 [root@localhost nginx-1.15.9]# nginx ## 启动 Nginx [root@localhost nginx-1.15.9]# netstat -anpt |grep nginx ## 过滤Nginx的进程 [root@localhost ~]# yum -y install psmisc ###最小安装没有killall令需要安装 [root@localhost ~]# killall -s HUP nginx ## 重载Nginx配置文件(相当于刷新) [root@localhost ~]# killall -s QUIT nginx ## 退出 Nginx

    为了使Nginx服务的启动、停止、重载等操作更加方便,可以编写基于CentOs 7.6的Nginx服务控制文件使用systemctl工具来进行管理CentOS7.6系统的管理习惯

    [root@localhost ~]# vim /lib/systemd/system/nginx.service [Unit] Description=nginx ###描述 After=network.target ####描述服务类别 [Service] Type=forking ###后台运行形式 PIDFile=/usr/local/nginx/logs/nginx.pid ###PID文件位置 ExecStart=/usr/local/nginx/sbin/nginx ###启动服务 ExecReload=/usr/bin/kill -s HUP $MAINPID ###根据PID重载配置 ExecStop=/usr/bin/kill -s QUIT $MAINPID ###根据PID终止进程 PrivateTmp=true [Install] WantedBy=multi-user.target [root@localhost ~]# chmod 754 /lib/systemd/system/nginx.service [root@localhost ~]# systemctl enable nginx.service [root@localhost ~]# systemctl start nginx ## 这样我们就可以用这种方法来开启 Nginx 了

    MySQL

    [root@localhost ~]# yum -y install \ > ncurses \ ##字符终端的包,方便终端操作## > ncurses-devel \ > bison \ ##函数库## > cmake ##没有configure脚本文件,使用cmake## [root@localhost ~]# useradd -s /sbin/nologin mysql ##创建程序用户##

    将所需压缩包传入/opt目录下,cmake配置和编译安装

    [root@localhost ~]# cd /opt [root@localhost opt]# tar xf mysql-boost-5.7.20.tar.gz [root@localhost opt]# cd mysql-5.7.20/ [root@localhost mysql-5.7.20]# cmake \ > -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \ ##指定安装位置## > -DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock \ ##sock;通讯文件,连接数据库,通讯协议的载体## > -DSYSCONFDIR=/etc \ ##配置目录指向etc## > -DSYSTEMD_PID_DIR=/usr/local/mysql \ > -DDEFAULT_CHARSET=utf8 \ ##此行和下一行为字符集相关## > -DDEFAULT_COLLATION=utf8_general_ci \ > -DWITH_INNOBASE_STORAGE_ENGINE=1 \ ##此行和下三行为存储引擎## > -DWITH_ARCHIVE_STORAGE_ENGINE=1 \ > -DWITH_BLACKHOLE_STORAGE_ENGINE=1 \ > -DWITH_PERFSCHEMA_STORAGE_ENGINE=1 \ > -DMYSQL_DATADIR=/usr/local/mysql/data \ ##指定存放位置## > -DWITH_BOOST=boost \ ##指定boost位置## > -DWITH_SYSTEMD=1 ##守护进程## [root@localhost mysql-5.7.20]# make && make install

    设置/usr/local/mysql目录的权限以及修改配置文件

    [root@localhost mysql-5.7.20]# chown -R mysql:mysql /usr/local/mysql [root@localhost mysql-5.7.20]# cd /etc [root@localhost etc]# vim my.cnf ##将原文删除,添加以下内容## [client] port = 3306 default-character-set=utf8 socket = /usr/local/mysql/mysql.sock [mysql] port = 3306 default-character-set=utf8 socket = /usr/local/mysql/mysql.sock [mysqld] user = mysql basedir = /usr/local/mysql datadir = /usr/local/mysql/data port = 3306 character_set_server=utf8 pid-file = /usr/local/mysql/mysqld.pid socket = /usr/local/mysql/mysql.sock server-id = 1 sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_AUTO_VALUE_ON_ZERO,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,PIPES_AS_CONCAT,ANSI_QUOTES

    设置环境变量以及初始化

    [root@localhost ~]# echo 'PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH' >> /etc/profile [root@localhost ~]# echo 'export PATH' >> /etc/profile [root@localhost ~]# source /etc/profile [root@localhost ~]# cd /usr/local/mysql/ [root@localhost mysql]# bin/mysqld \ --initialize-insecure \ --user=mysql \ --basedir=/usr/local/mysql \ --datadir=/usr/local/mysql/data

    编辑启动脚本

    [root@localhost mysql]# cp usr/lib/systemd/system/mysqld.service /usr/lib/systemd/system/ [root@localhost mysql]# systemctl enable mysqld [root@localhost mysql]# systemctl start mysqld

    设置MySQLroot用户密码

    [root@localhost mysql]# mysqladmin -u root -p password "As123" '//给root用户设置密码' '//mysqladmin -u用户名 -p旧密码 password 新密码' Enter password: '//原本密码为空,直接回车即可' Warning: Using a password on the command line interface can be insecure. [root@localhost mysql]# mysql -u root -p Enter password: '//输入刚设置的密码As123' ...省略内容 mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | test | +--------------------+ 4 rows in set (0.00 sec) mysql> exit Bye

    PHP 的编译安装

    [root@localhost ~]# yum -y install \ libjpeg \ libjpeg-devel \ libpng libpng-devel \ freetype freetype-devel \ libxml2 \ libxml2-devel \ zlib zlib-devel \ curl curl-devel \ openssl openssl-devel

    将安装包传入/opt目录下

    [root@localhost opt]# cd /opt [root@localhost opt]# tar xf php-7.1.10.tar.bz2 [root@localhost opt]# cd php-7.1.10/ [root@localhost php-7.1.10/]# ./configure \ --prefix=/usr/local/php \ --with-mysql-sock=/usr/local/mysql/mysql.sock \ --with-mysqli \ --with-zlib \ --with-curl \ --with-gd \ --with-jpeg-dir \ --with-png-dir \ --with-freetype-dir \ --with-openssl \ --enable-fpm \ --enable-mbstring \ --enable-xml \ --enable-session \ --enable-ftp \ --enable-pdo \ --enable-tokenizer \ --enable-zip [root@localhost php-7.1.10/]# make && make install

    配置PHP的配置文件 php有三个配置文件 php.ini核心配置文件 php-fpm.conf进程服务配置文件www.conf 扩展配置文件

    [root@localhost php-7.1.10/]# cp php.ini-development /usr/local/php/lib/php.ini [root@localhost php-7.1.10/]# vi /usr/local/php/lib/php.ini ##找到以下两行的位置,修改这两行## mysqli.default_socket = /usr/local/mysql/mysql.sock date.timezone = Asia/Shanghai ##找到后,前方的;注释要去掉## [root@localhost php-7.1.10]# /usr/local/php/bin/php -m ##验证安装的模块##

    配置及优化FPM 模块

    [root@localhost php-7.1.10]# cd /usr/local/php/etc/ [root@localhost etc]# cp php-fpm.conf.default php-fpm.conf [root@localhost etc]# cd /usr/local/php/etc/php-fpm.d/ [root@localhost php-fpm.d]# cp www.conf.default www.conf [root@localhost php-fpm.d]# cd /usr/local/php/etc/ [root@localhost etc]# vi php-fpm.conf pid = run/php-fpm.pid ##将;去掉## ;user = nginx ;group = nginx

    路径优化

    [root@localhost etc]# /usr/local/php/sbin/php-fpm -c /usr/local/php/lib/php.ini [root@localhost etc]# netstat -natp |grep 9000 tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 99975/php-fpm: mast [root@localhost etc]# ln -s /usr/local/php/bin/* /usr/local/bin/ [root@localhost etc]# ps aux | grep -c "php-fpm"

    编辑nginx配置文件让nginx支持PHP功能

    [root@localhost etc]# vi /usr/local/nginx/conf/nginx.conf location ~ \.php$ { root html; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name; include fastcgi_params; } ##将;去掉,用/usr/local/nginx/html替换原来的路径## [root@localhost etc]# vi /usr/local/nginx/html/index.php <?php phpinfo(); ?> [root@localhost etc]# killall -s HUP nginx ##重启nginx##

    网页测试下 测试数据库能否正常工作

    [root@localhost ~]# mysql -u root -p ##输入密码As123## CREATE DATABASE bbs; GRANT all ON bbs.* TO 'bbsadm'@'%' IDENTIFIED BY 'admin123'; GRANT all ON bbs.* TO 'bbsadm'@'localhost' IDENTIFIED BY 'admin123'; flush privileges; ##最后exit退出## [root@localhost ~]# vi /usr/local/nginx/html/index.php ##将之前的内容删除## <?php $link=mysqli_connect('20.0.0.25','bbsadm','admin123'); if($link) echo "<h1>Success!</h1>"; else echo "Fail!"; ?> [root@localhost ~]# killall -s HUP nginx ##重启##

    网页测试一下 OK,LNMP就搭好了

    Processed: 0.025, SQL: 8