LNMP源码编译


一、Nginx部署

1.1 获取源码

[root@centos7 ~]# wget https://nginx.org/download/nginx-1.18.0.tar.gz
[root@centos7 ~]# tar xf nginx-1.18.0.tar.gz

1.2 创建用户

[root@centos7 ~]# useradd -s /sbin/nologin -M nginx
[root@centos7 ~]# id nginx
uid=1000(nginx) gid=1000(nginx) groups=1000(nginx)

1.3 解决依赖

[root@centos7 ~]# yum install -y gcc-c++ pcre pcre-devel openssl openssl-devel zlib zlib-devel

1.4 格式优化

[root@centos7 ~]# cd nginx-1.18.0/
[root@centos7 ~/nginx-1.18.0]# mkdir ~/.vim
[root@centos7 ~/nginx-1.18.0]# cp -a contrib/vim/* ~/.vim

1.5 编译安装

[root@centos7 ~/nginx-1.18.0]# ./configure --prefix=/usr/local/nginx-1.18.0 --user=nginx --with-stream --with-stream_ssl_module --with-http_ssl_module --with-http_stub_status_module
[root@centos7 ~/nginx-1.18.0]# make && make install

1.6 配置管理

  1.6.1 创建软链;

[root@centos7 ~/nginx-1.18.0]# ln -s /usr/local/nginx-1.18.0/ /opt/nginx
[root@centos7 ~/nginx-1.18.0]# cd /opt/
[root@centos7 /opt]# ll -h
total 0
drwx--x--x. 4 root root 28 Dec  4 18:12 containerd
lrwxrwxrwx  1 root root 18 Mar 17 23:54 nginx -> /usr/local/nginx-1.18.0/
drwxr-xr-x  6 root root 54 Mar 17 23:49 nginx-1.18.0

  1.6.2 环境变量;

[root@centos7 /opt]# echo 'export PATH=$PATH:/opt/nginx/sbin' >> /etc/profile
[root@centos7 /opt]# source /etc/profile

  1.6.3 进程管理;

[root@centos7 /opt]# vim /usr/lib/systemd/system/nginx.service
[Unit]
Description=nginx
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
ExecStart=/opt/nginx/sbin/nginx
ExecReload=/opt/nginx/sbin/nginx -s reload
ExecStop=/opt/nginx/sbin/nginx -s stop

[Install]
WantedBy=multi-user.target

  1.6.4 开机自启;

[root@centos7 /opt]# systemctl enable nginx
[root@centos7 /opt]# systemctl start nginx

  1.6.5 查看进程;

[root@centos7 /opt]# ps -ef |grep nginx
root       6176      1  0 00:02 ?        00:00:00 nginx: master process /opt/nginx/sbin/nginx
nginx      6177   6176  0 00:02 ?        00:00:00 nginx: worker process
root       6197   1665  0 00:02 pts/0    00:00:00 grep --color=auto nginx

  1.6.6 日志配置;

log_format  main  '$remote_addr - $remote_user [$time_iso8601] "$request" '
                  '$status $body_bytes_sent "$http_referer" '
                  '"$http_user_agent" "$http_x_forwarded_for" $upstream_response_time $request_time';
access_log  logs/access.log  main;

  1.6.7 虚拟主机;

  一台服务器可以配置多个域名,而且每个域名的访问都是相对独立的,演示www.niuyx.cc、bbs.niuyx.cc、blog.niuyx.cc,

  单台服务器如果配置多个域名,建议一个域名一个配置文件,include nginx/conf/vhosts/*.conf

[root@centos7 ~]# tail -1 /etc/hosts
10.100.1.100 www.niuyx.cc bbs.niuyx.cc blog.niuyx.cc

  www域名配置:

server {
        listen       80 default_server;
        server_name  www.niuyx.cc;
        access_log   logs/www.access.log  main;
        location / {
            root     html/www;
            index    index.html index.htm;
        }
}

  Nginx配置默认主机,使用default_server表示默认的主机,配置在listen后面,表示访问的域名没有配置的话,就跳转至默认主机。

  bbs域名配置:

server {
        listen       80;
        server_name  bbs.niuyx.cc;
        access_log   logs/bbs.access.log  main;
        location / {
            root     html/bbs;
            index    index.html index.htm;
        }
}

  blog域名配置:

server {
        listen       80;
        server_name  blog.niuyx.cc;
        access_log   logs/blog.access.log  main;
        location / {
            root     html/blog;
            index    index.html index.htm;
        }
}

  创建虚拟主机的目录和文件:

[root@centos7 /opt/nginx/html]# pwd
/opt/nginx/html
[root@centos7 /opt/nginx/html]# for d in www bbs blog;do mkdir $d && echo $d.niuyx.cc >> $d/index.html;done

[root@centos7 /opt/nginx/html]# tree .
.
├── 50x.html
├── bbs
│   └── index.html
├── blog
│   └── index.html
├── index.html
└── www
    └── index.html

3 directories, 5 files

二、MySQL编译

  是rpm包不够快,还是官方二进制不好用?

  源码编译MySQL有什么用?没什么用;图什么?闲得蛋疼!

2.1 获取源码

[root@centos7 ~]# wget https://cdn.mysql.com//Downloads/MySQL-5.7/mysql-boost-5.7.37.tar.gz
[root@centos7 ~]# tar xf mysql-boost-5.7.37.tar.gz

2.2 解决依赖

[root@centos7 ~]# yum install -y gcc-c++ cmake ncurses ncurses-devel bison bison-devel perl perl-devel libaio libaio-devel

2.3 创建用户

[root@centos7 ~]# groupadd mysql
[root@centos7 ~]# useradd -r -g mysql -s /bin/false mysql
[root@centos7 ~]# id mysql
uid=997(mysql) gid=1001(mysql) groups=1001(mysql)

2.4 数据目录

[root@centos7 ~]# mkdir -p /data/mysql
[root@centos7 ~]# chown -R mysql:mysql /data/mysql/

2.5 编译安装

[root@centos7 ~]# cd mysql-5.7.37/
[root@centos7 ~/mysql-5.7.37]# cmake \
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql-5.7.37 \
-DMYSQL_DATADIR=/data/mysql \
-DMYSQL_UNIX_ADDR=/tmp/mysql.sock \
-DDEFAULT_CHARSET=utf8mb4 \
-DDEFAULT_COLLATION=utf8mb4_general_ci \
-DEXTRA_CHARSETS=all \
-DENABLED_LOCAL_INFILE=1 \
-DWITH_EMBEDDED_SERVER=1 \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_ARCHIVE_STORAGE_ENGINE=1 \
-DWITH_MYISAM_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITH_PARTITION_STORAGE_ENGINE=1 \
-DWITH_READLINE=1 \
-DWITH_BOOST=boost

  每一步完成后,执行echo $?检验是否出错。

[root@centos7 ~/mysql-5.7.37]# make

  接下来就是漫长的编译过程了~

  先来欣赏一下郭老师又勾勾又丢丢的单口吧!  

正月里来正月正,
兄弟三人去看灯。
聋子领着瞎子走,
瘸子后边紧跟行。
聋子说,
今年灯明炮不响。
瞎子说,
今年炮响灯不明。
瘸子说,
放你娘的狗臭屁,
灯明炮响路不平。

  编译时长 ≈ 揭瓦 + 返场!

[root@centos7 ~/mysql-5.7.37]# make install

2.6 启停脚本

[root@centos7 ~/mysql-5.7.37]# cd /usr/local/mysql/
[root@centos7 /usr/local/mysql]# cp support-files/mysql.server /etc/init.d/mysqld
[root@centos7 /usr/local/mysql]# vim /etc/init.d/mysqld
basedir=/usr/local/mysql
datadir=/data/mysql
[root@centos7 /usr/local/mysql]# chmod +x /etc/init.d/mysqld

  加入$PATH;

[root@centos7 /usr/local/mysql]# echo 'export PATH=$PATH:/usr/local/mysql/bin' >> /etc/profile
[root@centos7 /usr/local/mysql]# source /etc/profile

  MySQL版本;

[root@centos7 /usr/local/mysql]# mysql --version
./bin/mysql  Ver 14.14 Distrib 5.7.37, for Linux (x86_64) using  EditLine wrapper

2.7 数据初始化

  配置/etc/my.cnf;

[root@centos7 /usr/local/mysql]# vim /etc/my.cnf
[mysqld]
datadir=/data/mysql
socket=/tmp/mysql.sock
skip_name_resolve=on
innodb_file_per_table=on

[mysqld_safe]
log-error=/data/mysql/mysqld.log
pid-file=/data/mysql/mysqld.pid

  数据库初始化;

[root@centos7 /usr/local/mysql]# mysqld --initialize --datadir=/data/mysql --basedir=/usr/local/mysql --user=mysql

2022-03-18T11:33:56.791790Z 1 [Note] A temporary password is generated for root@localhost: x;.VwNJk7q7Q 

  systemctl管理;

[root@centos7 /usr/local/mysql]# vim /usr/lib/systemd/system/mysqld.service 
[Unit]
Description=mysqld
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
ExecStart=/etc/init.d/mysqld start
ExecStop=/etc/init.d/mysqld stop

[Install]
WantedBy=multi-user.target

  设置自启;

[root@centos7 /opt/mysql]# systemctl enable mysqld
[root@centos7 /opt/mysql]# systemctl start mysqld

  优化配置;

[root@centos7 /usr/local/mysql]# mysql_secure_installation

 三、PHP部署

 3.1 获取源码

[root@centos7 ~]# wget https://www.php.net/distributions/php-5.6.40.tar.gz
[root@centos7 ~]# tar zxf php-5.6.40.tar.gz

3.2 解决依赖

[root@centos7 ~]# yum -y install gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel libxml2 libxml2-devel \
libzip libzip-devel libcurl libcurl-devel libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel openldap \
openldap-devel libmcrypt libmcrypt-devel

3.3 编译安装

[root@centos7 ~]# cd php-5.6.40
[root@centos7 ~/php-5.6.40]# ./configure \
--prefix=/usr/local/php \
--with-config-file-path=/usr/local/php/etc \
--enable-ctype \
--with-mysql=mysqlnd \
--with-mysqli=mysqlnd \
--with-freetype-dir \
--with-jpeg-dir \
--with-png-dir \
--with-zlib \
--with-libxml-dir=/usr \
--enable-xml \
--disable-rpath \
--enable-bcmath \
--enable-shmop \
--enable-sysvsem \
--enable-inline-optimization \
--with-curl \
--enable-mbregex \
--enable-mbstring \
--with-mcrypt \
--with-gd \
--enable-gd-native-ttf \
--with-openssl \
--with-mhash \
--enable-pcntl \
--enable-sockets \
--with-ldap-sasl \
--with-xmlrpc \
--enable-zip \
--enable-soap \
--with-gettext \
--enable-fpm

[root@centos7 ~/php-5.6.40]# make && make install

  拷贝php配置文件;

[root@centos7 ~/php-5.6.40]# cp php.ini-production /usr/local/php/etc/php.ini

  配置环境变量;

root@centos7 ~/php-5.6.40]# echo 'export PATH=$PATH:/usr/local/php/bin:/usr/local/php/sbin' >> /etc/profile
root@centos7 ~/php-5.6.40]# source /etc/profile

root@centos7 ~/php-5.6.40]# php -v
PHP 5.6.40 (cli) (built: Mar 19 2022 13:40:00) 
Copyright (c) 1997-2016 The PHP Group
Zend Engine v2.6.0, Copyright (c) 1998-2016 Zend Technologies

  查看php的模块 php -m  

  查看php的编译参数,网站搬家要用到;

root@centos7 ~/php-5.6.40]# php -i|grep configure
Configure Command =>  './configure'  '--prefix=/usr/local/php' '--with-config-file-path=/usr/local/php/etc' 
'--enable-ctype' '--with-mysql=mysqlnd' '--with-mysqli=mysqlnd' '--with-freetype-dir' '--with-jpeg-dir' 
'--with-png-dir' '--with-zlib' '--with-libxml-dir=/usr' '--enable-xml' '--disable-rpath' '--enable-bcmath' 
'--enable-shmop' '--enable-sysvsem' '--enable-inline-optimization' '--with-curl' '--enable-mbregex' 
'--enable-mbstring' '--with-mcrypt' '--with-gd' '--enable-gd-native-ttf' '--with-openssl' '--with-mhash' 
'--enable-pcntl' '--enable-sockets' '--with-ldap-sasl' '--with-xmlrpc' '--enable-zip' '--enable-soap' 
'--with-gettext' '--enable-fpm'

 3.4 php-fpm配置

  nginx通过php-fpm这个接口程序来调用php程序,php-fpm专为nginx+php的架构开发。

  php-fpm默认配置提取;

[root@centos7 ~/php-5.6.40]# cat /usr/local/php/etc/php-fpm.conf.default|egrep -v '^;|^$'

[global]
 
[www]
user = nobody
group = nobody
listen = 127.0.0.1:9000
 
pm = dynamic
pm.max_children = 5
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 3 

  默认读取/usr/local/php/etc/php-fpm.conf,

  php-fpm是需要手动创建配置文件,默认安装完后是没有的。

[root@centos7 /usr/local/php/etc]# ls
pear.conf  php-fpm.conf.default  php.ini
[root@centos7 /usr/local/php/etc]# cp php-fpm.conf.default php-fpm.conf

  php-fpm启动和关闭;

/usr/local/php/sbin/php-fpm -t    #检查配置文件
/usr/local/php/sbin/php-fpm    #启动php-fpm
pkill -9 php-fpm    #闭关php-fpm

  php-fpm加入systemctl管理;

[root@centos7 /usr/local/php/etc]# vim /usr/lib/systemd/system/php-fpm.service

[Unit]
Description=php-fpm
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
ExecStart=/usr/local/php/sbin/php-fpm
ExecStop=pkill -9 php-fpm

[Install]
WantedBy=multi-user.target

  管理php-fpm;

[root@centos7 /usr/local/php/etc]# systemctl enable php-fpm
[root@centos7 /usr/local/php/etc]# systemctl start php-fpm

 四、Phpmyadmin部署

  phpmyadmin以web的方式去管理mysql数据库,使用php语言开发。

[root@centos7 /opt/nginx/html]# wget https://files.phpmyadmin.net/phpMyAdmin/4.8.1/phpMyAdmin-4.8.1-all-languages.zip
[root@centos7 /opt/nginx/html]# ls
50x.html  bbs  blog  index.html  index.php  phpMyAdmin-4.8.1-all-languages.zip  www
[root@centos7 /opt/nginx/html]# unzip phpMyAdmin-4.8.1-all-languages.zip
[root@centos7 /opt/nginx/html]# mv phpMyAdmin-4.8.1-all-languages phpMyAdmin-4.8.1
[root@centos7 /opt/nginx/html]# cd phpMyAdmin-4.8.1/
[root@centos7 /opt/nginx/html/phpMyAdmin-4.8.1]# cp libraries/config.default.php config.inc.php

  配置访问MySQL的账密;

  $cfg['Servers'][$i]['host'] = '127.0.0.1';

  $cfg['Servers'][$i]['user'] = 'niuyx';

  $cfg['Servers'][$i]['password'] = 'nihaoa';

 

mysql> create database if not exists wordpress character set utf8mb4 collate utf8mb4_bin;
Query OK, 1 row affected (0.00 sec)

mysql> grant all privileges on wordpress.* to wordpress@localhost identified by 'wordpress';
Query OK, 0 rows affected, 2 warnings (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)