LNMP源码编译

一、Nginx部署

1.1 获取源码

1
2
[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 创建用户

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

1.3 解决依赖

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

1.4 格式优化

1
2
3
[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 编译安装

1
2
[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 创建软链;

1
2
3
4
5
6
[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/

  1.6.2 环境变量;

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

  1.6.3 进程管理;

1
2
3
4
5
6
7
8
9
10
11
12
13
[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 开机自启;

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

  1.6.5 查看进程;

1
2
3
4
[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 日志配置;

1
2
3
4
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 虚拟主机;

  一台服务器可以配置多个域名,而且每个域名的访问都是相对独立的;

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

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

1
2
3
4
5
6
7
8
9
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;
        }
}

  1.6.8 安全配置

  Nginx基本安全配置,最基本的账密认证;

1
2
3
4
5
6
location / {
    root   html;
    index  index.html index.htm index.php;
    auth_basic "basic auth";
    auth_basic_user_file /opt/nginx/conf/htpasswd;
}

  定义用户名,生成密码,nginx/conf/htpasswd;

  格式:Account:Passwd

1
2
3
4
[root@centos7 /opt/nginx/conf]# openssl passwd -1 nihaoa
$1$PIN/X3xm$hdQq2.STBCSGrGomxnyE3.
[root@centos7 /opt/nginx/conf]# vim htpasswd
niuyx:$1$PIN/X3xm$hdQq2.STBCSGrGomxnyE3.

二、MySQL编译

2.1 获取源码

1
2
[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 解决依赖

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

2.3 创建用户

1
2
3
4
[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 数据目录

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

2.5 编译安装

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
[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 $?检验是否出错。

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

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

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

1
2
3
4
5
6
7
8
9
10
11
正月里来正月正,
兄弟三人去看灯。
聋子领着瞎子走,
瘸子后边紧跟行。
聋子说,
今年灯明炮不响。
瞎子说,
今年炮响灯不明。
瘸子说,
放你娘的狗臭屁,
灯明炮响路不平。

  编译时长 ≈ 揭瓦 + 批三国!

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

2.6 创建软链

1
2
3
4
5
6
[root@centos7 ~/mysql-5.7.37]# ln -s /usr/local/mysql-5.7.37/ /opt/mysql
[root@centos7 ~/mysql-5.7.37]# ll -h /opt/
total 0
drwx--x--x. 4 root root 28 Dec  4 18:12 containerd
lrwxrwxrwx  1 root root 24 Mar 21 18:28 mysql -> /usr/local/mysql-5.7.37/
lrwxrwxrwx  1 root root 24 Mar 21 17:29 nginx -> /usr/local/nginx-1.18.0/

  加入$PATH;

1
2
[root@centos7 ~/mysql-5.7.37]# echo 'export PATH=$PATH:/opt/mysql/bin' >> /etc/profile
[root@centos7 ~/mysql-5.7.37]# source /etc/profile

2.7 启停脚本

1
2
3
4
5
6
[root@centos7 ~/mysql-5.7.37]# cd /opt/mysql/
[root@centos7 /opt/mysql]# cp support-files/mysql.server /etc/init.d/mysqld
[root@centos7 /opt/mysql]# vim /etc/init.d/mysqld
basedir=/opt/mysql
datadir=/data/mysql
[root@centos7 /opt/mysql]# chmod +x /etc/init.d/mysqld

  MySQL版本;

1
2
[root@centos7 /opt/mysql]# mysql --version
mysql  Ver 14.14 Distrib 5.7.37, for Linux (x86_64) using  EditLine wrapper

2.8 数据初始化

  配置/etc/my.cnf;

1
2
3
4
5
6
7
8
9
10
[root@centos7 /opt/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

  数据库初始化;

1
2
3
[root@centos7 /opt/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管理;

1
2
3
4
5
6
7
8
9
10
11
12
[root@centos7 /opt/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

  设置启动;

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

  优化配置;

1
[root@centos7 /opt/mysql]# mysql_secure_installation

三、PHP部署

 3.1 获取源码

1
2
[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 解决依赖

1
2
3
[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 编译安装

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
[root@centos7 ~]# cd php-5.6.40
[root@centos7 ~/php-5.6.40]# ./configure \
--prefix=/usr/local/php-5.6.40 \
--with-config-file-path=/usr/local/php5.6.40/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配置文件;

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

  创建软链接;

1
2
3
4
5
6
7
[root@centos7 ~/php-5.6.40]# ln -s /usr/local/php-5.6.40/ /opt/php
[root@centos7 ~/php-5.6.40]# ll -h /opt/
total 0
drwx--x--x. 4 root root 28 Dec  4 18:12 containerd
lrwxrwxrwx  1 root root 24 Mar 21 18:28 mysql -> /usr/local/mysql-5.7.37/
lrwxrwxrwx  1 root root 24 Mar 21 17:29 nginx -> /usr/local/nginx-1.18.0/
lrwxrwxrwx  1 root root 22 Mar 21 19:25 php -> /usr/local/php-5.6.40/

  配置$PATH;

1
2
3
4
5
6
7
root@centos7 ~/php-5.6.40]# echo 'export PATH=$PATH:/opt/php/bin:/opt/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 -m查看php的模块;

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

1
2
3
4
5
6
7
8
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默认配置提取;

1
2
3
4
5
6
7
8
9
10
11
12
13
[root@centos7 ~/php-5.6.40]# cat /opt/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 

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

  php-fpm需要手动创建配置文件;

1
2
3
4
[root@centos7 ~/php-5.6.40]# cd /opt/php/etc/
[root@centos7 /opt/php/etc]# ls
pear.conf  php-fpm.conf.default  php.ini
[root@centos7 /opt/php/etc]# cp php-fpm.conf.default php-fpm.conf

  php-fpm启动和关闭;

1
2
3
[root@centos7 /opt/php/etc]# php-fpm -t     #检查配置文件
[root@centos7 /opt/php/etc]# php-fpm        #启动php-fpm
[root@centos7 /opt/php/etc]# pkill -9 php-fpm   #关闭php-fpm

  加入systemctl管理;

1
2
3
4
5
6
7
8
9
10
11
[root@centos7 /opt/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=/opt/php/sbin/php-fpm
ExecStop=pkill -9 php-fpm
 
[Install]
WantedBy=multi-user.target

  php-fpm启动管理;

1
2
[root@centos7 /opt/php/etc]# systemctl enable php-fpm
[root@centos7 /opt/php/etc]# systemctl start php-fpm

四、Nginx整合php-fpm

  配置Nginx支持PHP;

  /scripts$fastcgi_script_name要配置路径,否则无法解析;

1
2
3
4
5
6
7
location ~ \.php$ {
    root           html;
    fastcgi_pass   127.0.0.1:9000;
    fastcgi_index  index.php;
    fastcgi_param  SCRIPT_FILENAME  /opt/nginx/html$fastcgi_script_name;
    include        fastcgi_params;
}

  创建php测试文件;

1
2
3
4
[root@centos7 /opt/nginx/html]# vim phpinfo.php
<?php
phpinfo();
?>

  浏览器访问,查看是否解析。

五、Phpmyadmin部署

  Phpmyadmin是用 PHP 脚本写的 MySQL 数据库的管理软件,是使用 Web 图形模式直接管理 MySQL 数据库的工具。

  PhpMyAdmin 可以用来创建、修改、删除数据库和数据表、数据记录,导入和导出数据库,还可以完成许多其他的 MySQL 系统管理任务。

1
2
3
4
5
6
7
[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  index.html  phpinfo.php  phpMyAdmin-4.8.1-all-languages.zip
[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用户;

1
2
3
4
5
mysql> grant all privileges on *.* to 'niuyx'@'%' identified by 'nihaoa';
Query OK, 0 rows affected, 1 warning (0.01 sec)
 
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

  配置访问MySQL的账密;

1
2
3
$cfg['Servers'][$i]['host'] = '127.0.0.1';
$cfg['Servers'][$i]['user'] = 'niuyx';
$cfg['Servers'][$i]['password'] = 'nihaoa';

  登录http://10.100.1.100/phpMyAdmin-4.8.1/index.php

六、 WordPress建站

 6.1 获取wordpress

  wordpress下载地址:

  英文版 https://wordpress.org/download/

  中文版 https://cn.wordpress.org/download/

1
2
3
4
5
6
7
8
9
10
11
12
[root@centos7 /opt/nginx/html]# tar xf wordpress-5.9.2-zh_CN.tar.gz
[root@centos7 /opt/nginx/html]# ls
50x.html  index.html  phpinfo.php  phpMyAdmin-4.8.1  wordpress
[root@centos7 /opt/nginx/html]# cd wordpress/
[root@centos7 /opt/nginx/html/wordpress]# cp wp-config-sample.php wp-config.php
[root@centos7 /opt/nginx/html/wordpress]# vim wp-config.php
 
define( 'DB_NAME', 'wordpress' );
define( 'DB_USER', 'wordpress' );
define( 'DB_PASSWORD', 'wordpress' );
define( 'DB_HOST', 'localhost' );
define( 'DB_CHARACTER', 'utf8mb4' );

6.2 创建数据库

1
2
3
4
5
6
7
8
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)

6.3 访问与测试

  若出现“连接数据库出错”,检查wp-config.php配置及MySQL库表用户等。

 

posted @   zhenru  阅读(84)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· .NET10 - 预览版1新功能体验(一)
点击右上角即可分享
微信分享提示