2021 阿里云 CentOS77 1核1G LNMP 装

阿里云 Centos 7.7 1核1G LNMP 装机

原文地址 : 3pub.cn/index.php/a…
之前写的 腾讯云 Centos 安装 PHP 开发环境 文章时间久了最近新买服务器重新装机踩坑

装机很艰辛呀 ! 最初操作系统选择Centos6.5 内存 512MB 在装机过程中遇到各种坑, 填起来太过费劲 yum源过期 . php7.4 sqllite3 还要有编译时各种内存不足问题. 上面说的问题最好的解决方案就是 加钱 升级配置

系统查看

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
shell复制代码$ lscpu
Architecture: x86_64
CPU op-mode(s): 32-bit, 64-bit
Byte Order: Little Endian
CPU(s): 1
On-line CPU(s) list: 0
Thread(s) per core: 1
Core(s) per socket: 1
Socket(s): 1
NUMA node(s): 1
Vendor ID: GenuineIntel
CPU family: 6
Model: 85
Model name: Intel(R) Xeon(R) Platinum 8269CY CPU @ 2.50GHz
Stepping: 7
CPU MHz: 2500.000
BogoMIPS: 5000.00
Hypervisor vendor: KVM
Virtualization type: full
L1d cache: 32K
L1i cache: 32K
L2 cache: 1024K
L3 cache: 36608K
NUMA node0 CPU(s): 0
Flags: fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ss ht syscall nx pdpe1gb rdtscp lm constant_tsc rep_good nopl nonstop_tsc eagerfpu pni pclmulqdq ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand hypervisor lahf_lm abm 3dnowprefetch invpcid_single fsgsbase tsc_adjust bmi1 hle avx2 smep bmi2 erms invpcid rtm mpx avx512f avx512dq rdseed adx smap avx512cd avx512bw avx512vl xsaveopt xsavec xgetbv1 arat avx512_vnni

$ cat /etc/redhat-release
CentOS Linux release 7.7.1908 (Core)
$ getconf LONG_BIT
64 #

Yum 安装依赖

1
2
3
shell复制代码$ yum install -y gcc gcc-c++ autoconf libjpeg libjpeg-devel enchant-devel pam-devel  libc-client libc-client-devel libpng libpng-devel freetype freetype-devel libpng libpng-devel libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel  bzip2 bzip2-devel ncurses curl openssl-devel gdbm-devel db4-devel libXpm-devel  libX11-devel gd-devel gmp-devel readline-devel libxslt-devel expat-devel  xmlrpc-c xmlrpc-c-devel sqlite-devel libaio

$ yum install -y glibc.i686 or yum install -y glibc.i386 # 可以不装

创建swap 分区

通过free -m来查看下内存使用状况

1
2
3
4
shell复制代码$ free -m
total used free shared buff/cache available
Mem: 990 271 616 0 102 597
Swap: 0 0 0

创建一个2GB大小的文件

1
2
3
4
5
shell复制代码$ mkdir -p /opt/images
$ dd if=/dev/zero of=/opt/images/swap bs=1024 count=2048000
2048000+0 records in
2048000+0 records out
2097152000 bytes (2.1 GB) copied, 18.6413 s, 113 MB/s

把创建的文件变成SWAP分区

1
2
3
shell复制代码$ mkswap /opt/images/swap
Setting up swapspace version 1, size = 2047996 KiB
no label, UUID=56051496-4f81-47f0-85bf-e4d850714726

启用这个SWAP文件

1
2
shell复制代码$ swapon /opt/images/swap
swapon: /opt/images/swap: insecure permissions 0644, 0600 suggested.
1
2
3
4
shell复制代码$ free -m
total used free shared buff/cache available
Mem: 990 274 62 0 653 572
Swap: 1999 0 1999

阿里云的源有坑需要按照下面方法修改

/etc/yum.repos.d/目录下的CentOS-Base.repoepel.repo文件进行修改

1
2
3
4
5
6
7
8
9
shell复制代码把CentOS-Base.repo文件中的以下网址
http://mirrors.aliyun.com/centos/
http://mirrors.aliyuncs.com/centos/
http://mirrors.cloud.aliyuncs.com/centos/
修改成
http://mirrors.aliyun.com/centos-vault/centos/

把epel.repo文件中的
enabled=1修改enabled=0

创建用户及用户组

1
2
shell复制代码$ groupadd www
$ useradd -g www www #创建www用户到www用户组

Nginx

1
2
3
shell复制代码$ cd /usr/local/src/
$ wget http://nginx.org/download/nginx-1.9.8.tar.gz
$ tar -xf nginx-1.9.8.tar.gz

下载 pcre 并编译: www.linuxfromscratch.org/blfs/view/s…

1
2
3
4
5
shell复制代码$ wget https://downloads.sourceforge.net/pcre/pcre-8.41.tar.bz2
$ tar jxf pcre-8.41.tar.bz2
$ cd pcre-8.41
$ ./configure --prefix=/usr/local/pcre --enable-utf8 --enable-unicode-properties
$ make && make install

开始加载的nginx 模块及编译参数

1
2
3
shell复制代码$ cd /usr/local/src/nginx-1.9.8
$ ./configure --prefix=/usr/local/nginx --user=www --group=www --with-http_stub_status_module --with-http_ssl_module --with-pcre=/usr/local/src/pcre-8.41
$ make && make install

设置软链 nginx 支持全局调用

1
2
3
4
shell复制代码$ ln -s /usr/local/nginx/sbin/nginx  /usr/bin/nginx
$ 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

Redis

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
shell复制代码$ cd /usr/local/src
$ wget http://download.redis.io/releases/redis-6.2.2.tar.gz
$ tar xf redis-6.2.2.tar.gz
$ cd redis-6.2.2
$ mkdir -p /usr/local/redis/bin
$ mkdir -p /usr/local/redis/etc
$ make && make install

make[1]: Leaving directory `/usr/local/src/redis-6.2.2/src'

$ cp redis.conf /usr/local/redis/etc/
$ cd /usr/local/src/redis-6.2.2/src
$ mv mkreleasehdr.sh redis-benchmark redis-check-aof redis-cli redis-server redis-check-rdb redis-sentinel redis-trib.rb /usr/local/redis/bin

$ ln /usr/local/redis/bin/redis-server /usr/bin/redis-server
$ ln /usr/local/redis/bin/redis-cli /usr/bin/redis-cli
# 配置 守护进程及密码 vim /usr/local/redis/etc/redis.conf
# requirepass 密码 // 注释打开
# daemonize no 改 daemonize yes
$ redis-server /usr/local/redis/etc/redis.conf
$ netstat -tunple # 检测6379 进程是否被redis-server 占用

Mysql

卸载·mysql·

1
2
3
4
5
6
shell复制代码$  rpm -aq|grep -i 'mysql'; # 检索输出的所有mysql 相关 下面进行卸载
$ rpm -e mysql-community-libs-compat-5.7.21-1.el6.x86_64
$ rpm -e mysql-community-client-5.7.21-1.el6.x86_64
...
# 删除mysql的服务:chkconfig --list|grep -i mysql
$ chkconfig --del mysql
1
2
3
4
5
6
7
8
9
shell复制代码$ cd /usr/local/src
# 服务端及相关工具
$ wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-community-server-5.7.33-1.el7.x86_64.rpm;
# 客户端及相关工具
$ wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-community-client-5.7.33-1.el7.x86_64.rpm;
# 服务端和客户端的公共文件
$ wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-community-common-5.7.33-1.el7.x86_64.rpm;
# 客户端共享库
$ wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-community-libs-5.7.33-1.el7.x86_64.rpm;

按照以下顺序进行安装,因为它们之间存在依赖关系

1
2
3
4
5
shell复制代码# common --> libs --> clients --> server
$ rpm -ivh mysql-community-common-5.7.33-1.el7.x86_64.rpm --force --nodeps
$ rpm -ivh mysql-community-libs-5.7.33-1.el7.x86_64.rpm --force --nodeps
$ rpm -ivh mysql-community-client-5.7.33-1.el7.x86_64.rpm --force --nodeps
$ rpm -ivh mysql-community-server-5.7.33-1.el7.x86_64.rpm --force --nodeps

安装时抛错 –force –nodeps 完美解决 原因:这是由于yum安装了旧版本的GPG keys造成的

参考地址: www.cnblogs.com/royfans/p/7…

检测安装

1
2
3
4
5
6
7
8
shell复制代码$ which mysql
/usr/bin/mysql
$ mysql -V

mysql Ver 14.14 Distrib 5.7.33, for Linux (x86_64) using EditLine wrapper
Installing MySQL system tables..../bin/mysqld: error while loading shared libraries: libaio.so.1: cannot open shared object file: No such file or directory

解决办法:yum install -y libaio

启动mysql服务

1
shell复制代码$ systemctl start mysqld

启动服务 异常抛错 检测错误日志

1
2
3
shell复制代码$ tail -n 100 /var/log/mysqld.log
2021-04-26T20:03:15.474974Z 0 [Warning] Failed to open optimizer cost constant tables
2021-04-26T20:03:15.475065Z 0 [ERROR] Fatal error: Can't open and lock privilege tables: Table 'mysql.user' doesn't exist

修改 my.cnf datadir=/var/lib/mysql 改为 datadir=/opt/mysql/data

修改Mysql 密码

查看临时密码

1
2
3
4
shell复制代码$ grep password /var/log/mysqld.log
2021-04-23T21:34:34.260324Z 1 [Note] A temporary password is generated for root@localhost: Dn.Z-4srP1)A
$ mysql -h root -p
set password = password("asasd");

PHP

安装php7.4 和 php8

1
2
3
4
5
6
7
shell复制代码$ cd /usr/local/src
$ wget https://www.php.net/distributions/php-7.4.16.tar.gz --no-check-certificate
$ tar zxf php-7.4.16.tar.gz
$ wget https://www.php.net/distributions/php-8.0.3.tar.gz --no-check-certificate
$ tar xzf php-8.0.3.tar.gz
$ mkdir -p /usr/local/php7
$ mkdir -p /usr/local/php8

安装 libmcrypt 库

1
2
3
4
5
6
shell复制代码$ cd /usr/local/src
$ wget ftp://mcrypt.hellug.gr/pub/crypto/mcrypt/libmcrypt/libmcrypt-2.5.7.tar.gz
$ tar xf libmcrypt-2.5.7.tar.gz
$ cd libmcrypt-2.5.7
$ ./configure
$ make && make install

异常:configure: WARNING: unrecognized options: –with-mcrypt php7.2 不支持以上三个选项,删掉即可

解决安装问题: libxml2 configure: error: xml2-config not found. Please check your libxml2 installation.

1
2
3
4
5
6
shell复制代码$ cd /usr/local/src
$ wget ftp://xmlsoft.org/libxml2/libxml2-2.9.1.tar.gz
$ tar zxf libxml2-2.9.1.tar.gz
$ cd libxml2-2.9.1
$ ./configure
$ make && make install

安装tclsh方法

下载源码 到 www.tcl.tk/software/tc… 下载 tcl8.5.19-src.tar.gz
解压 tar -xzvf tcl8.5.19-src.tar.gz,比如下载放在了/usr/local/src目录
解压安装 如下,安装到usr/tcl目录下

1
2
3
4
5
6
7
shell复制代码$ wget https://prdownloads.sourceforge.net/tcl/tcl8.5.19-src.tar.gz
$ tar -xzvf tcl8.5.19-src.tar.gz
$ cd /usr/local/src/tcl8.5.19/unix
$ ./configure --prefix=/usr/tcl
$ make && make install

创建软链接:创建快捷名字tclsh,放到usr/bin下面 ln /usr/tcl/bin/tclsh8.5 /usr/bin/tclsh

开始编译PHP

PHP 7.4

1
2
3
shell复制代码$ cd /usr/local/src/php
$ cd /usr/local/src/php-7.4.16
$ ./configure --prefix=/usr/local/php7/ --with-config-file-path=/usr/local/php7/etc --with-config-file-scan-dir=/usr/local/php7/etc/conf.d --enable-fpm --with-fpm-user=www --with-fpm-group=www --enable-soap --with-openssl --with-openssl-dir --with-zlib --with-iconv --with-bz2 --enable-calendar --with-curl --with-cdb --enable-dom --enable-exif --enable-ftp --enable-gd --with-gettext --with-gmp --with-mhash --enable-mbstring --enable-pdo --with-pdo-mysql --with-zlib-dir --with-readline --enable-session --enable-shmop --enable-simplexml --enable-sockets --enable-sysvmsg --enable-sysvsem --enable-sysvshm --with-xsl --enable-mysqlnd --with-mysqli --without-pear --enable-pcntl --enable-bcmath

编译抛错 No package 'oniguruma' found 参考: www.limstash.com/articles/20…

解决方案 :

1
2
3
4
5
6
shell复制代码$ yum install https://rpms.remirepo.net/enterprise/7/remi/x86_64/oniguruma5php-6.9.6-1.el7.remi.x86_64.rpm
$ yum install https://rpms.remirepo.net/enterprise/7/remi/x86_64/oniguruma5php-devel-6.9.6-1.el7.remi.x86_64.rpm

$ make && make install

编译前一定保证内存足够, 内存不足

PHP 8.0

1
2
3
4
5
6
7
8
shell复制代码$ cd /usr/local/src/php-8.0.3
$ ./configure --prefix=/usr/local/php8/ --with-config-file-path=/usr/local/php8/etc --with-config-file-scan-dir=/usr/local/php8/etc/conf.d --enable-fpm --with-fpm-user=www --with-fpm-group=www --enable-soap --with-openssl --with-openssl-dir --with-zlib --with-iconv --with-bz2 --enable-calendar --with-curl --with-cdb --enable-dom --enable-exif --enable-ftp --enable-gd --with-gettext --with-gmp --with-mhash --enable-mbstring --enable-pdo --with-pdo-mysql --with-zlib-dir --with-readline --enable-session --enable-shmop --enable-simplexml --enable-sockets --enable-sysvmsg --enable-sysvsem --enable-sysvshm --with-xsl --enable-mysqlnd --with-mysqli --without-pear --enable-pcntl --enable-bcmath

$ make && make install
$ php8 --version
PHP 8.0.3 (cli) (built: Apr 27 2021 05:45:18) ( NTS )
Copyright (c) The PHP Group
Zend Engine v4.0.3, Copyright (c) Zend Technologies

安装PHP扩展

redis 扩展

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
shell复制代码$ wget https://pecl.php.net/get/redis-5.3.4.tgz
$ tar xf redis-5.3.4.tgz
$ cd redis-5.3.4
$ /usr/local/php7/bin/phpize
Configuring for:
PHP Api Version: 20190902
Zend Module Api No: 20190902
Zend Extension Api No: 320190902

$ ./configure --with-php-config=/usr/local/php7/bin/php-config
$ make && make install
...
Installing shared extensions: /usr/local/php7/lib/php/extensions/no-debug-non-zts-20190902/

$ make clean
$ /usr/local/php8/bin/phpize
$ ./configure --with-php-config=/usr/local/php8/bin/php-config
$ make && make install
...
Installing shared extensions: /usr/local/php8/lib/php/extensions/no-debug-non-zts-20200930/

mongodb扩展

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
shell复制代码$ wget https://pecl.php.net/get/mongodb-1.9.1.tgz
$ tar xf mongodb-1.9.1.tgz
$ cd mongodb-1.9.1

$ ./configure --with-php-config=/usr/local/php7/bin/php-config
$ make && make install
...
Installing shared extensions: /usr/local/php7/lib/php/extensions/no-debug-non-zts-20190902/

$ make clean
$ /usr/local/php8/bin/phpize
$ ./configure --with-php-config=/usr/local/php8/bin/php-config
$ make && make install
...
Installing shared extensions: /usr/local/php8/lib/php/extensions/no-debug-non-zts-20200930/

Swoole 扩展

1
2
3
4
5
6
7
8
9
10
11
12
13
shell复制代码$ wget https://pecl.php.net/get/swoole-4.6.6.tgz
$ /usr/local/php7/bin/phpize
$ ./configure --with-php-config=/usr/local/php7/bin/php-config
$ make && make install
Installing shared extensions: /usr/local/php7/lib/php/extensions/no-debug-non-zts-20190902/
Installing header files: /usr/local/php7/include/php/

$ make clean
$ /usr/local/php8/bin/phpize
$ ./configure --with-php-config=/usr/local/php8/bin/php-config
$ make && make install
Installing shared extensions: /usr/local/php8/lib/php/extensions/no-debug-non-zts-20200930/
Installing header files: /usr/local/php8/include/php/

php.ini 配置

1
2
3
4
5
6
7
8
9
10
11
12
13
shell复制代码$ vim /usr/local/php7/etc/php.ini

extension=/usr/local/php7/lib/php/extensions/no-debug-non-zts-20190902/swoole.so
extension=/usr/local/php7/lib/php/extensions/no-debug-non-zts-20190902/redis.so
extension=/usr/local/php7/lib/php/extensions/no-debug-non-zts-20190902/mongodb.so

$ vim /usr/local/php8/etc/php.ini

extension=/usr/local/php8/lib/php/extensions/no-debug-non-zts-20200930/swoole.so
extension=/usr/local/php8/lib/php/extensions/no-debug-non-zts-20200930/redis.so
extension=/usr/local/php8/lib/php/extensions/no-debug-non-zts-20200930/mongodb.so

disable_functions = pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,phpinfo,eval,passthru,exec,system,chroot,chgrp,chown,shell_exec,proc_open,proc_get_status,ini_alter,ini_alter,ini_restore,dl,openlog,syslog,readlink,symlink,popepassthru,stream_socket_server,fsocket,copy,extract,pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,phpinfo,eval,passthru,exec,system,chroot,chgrp,chown,shell_exec,proc_open,proc_get_status,ini_alter,ini_alter,ini_restore,dl,openlog,syslog,readlink,symlink,popepassthru,stream_socket_server,fsocket,copy,extract

启动服务

1
2
3
4
5
6
7
8
9
shell复制代码# 启动redis
$ redis-server /usr/local/redis/etc/redis.conf
# 启动php-fpm 默认9000端口
$ /usr/local/php7/sbin/php-fpm -c /usr/local/php7/etc/php.ini -y /usr/local/php7/etc/php-fpm.conf # -t 参数检测
# 启动nginx
$ nginx # nginx -t 检测

# 启动mysql 上面已经启动
$ systemctl start mysqld

本文转载自: 掘金

开发者博客 – 和开发相关的 这里全都有

0%