Linux编译安装LNMP+redis+mongodb

Linux部署LNMP环境

安装Nginx

#安装nginx依赖
yum -y install gcc gcc-c++
yum -y install pcre pcre-devel
yum -y install openssl openssl-devel

cd /data/nginx
#官网下载nginx安装包    http://nginx.org/en/download.html
wget http://nginx.org/download/nginx-1.20.1.tar.gz
#解压安装包
tar -zxvf nginx-1.20.1.tar.gz

#编译安装
./configure --prefix=/data/nginx
make
make install

## 报错:./configure: error: the HTTP rewrite module requires the PCRE library.    执行yum -y install pcre pcre-devel
## 报错:./configure: error: the HTTP cache module requires md5 functionsfrom OpenSSL library.   You can either disable the module by using--without-http-cache option, or install the OpenSSL library into the system,or build the OpenSSL library statically from the source with nginx by using--with-http_ssl_module --with-openssl=<path> options.    执行yum -y install openssl openssl-devel

#创建并设置nginx运行账号
groupadd nginx
useradd -M -g nginx -s /sbin/nologin nginx
cd /data/nginx/conf
vim nginx.conf
#设置user
user root;
/data/nginx/sbin/nginx -t

#启动nginx
/data/nginx/sbin/nginx
#停止
nginx -s stop
#重启
nginx -s reload

#建立软连接
ln -s /data/nginx/sbin/nginx /usr/local/sbin
#启动
nginx

安装Mysql

#下载mysql安装包
wget http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm

rpm -ivh mysql-community-release-el7-5.noarch.rpm
yum update
yum install mysql-server
#权限设置
chown mysql:mysql -R /var/lib/mysql

#初始化 MySQL
mysqld --initialize

#启动 MySQL
service mysqld start
#关闭 Mysql
service mysql stop

#查看 MySQL 运行状态
service mysqld status

#修改root账户密码
mysqladmin -u root password “123456”
#或者
set password for root@localhost=password("123456");

#设置root所有sql权限
grant all privileges on *.* to root@"%" identified by "你的密码";
flush privileges;#强制刷新

安装PHP

#安装php依赖
yum install libxml2 libxml2-devel openssl openssl-devel bzip2 bzip2-devel libcurl libcurl-devel libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel gmp gmp-devel libmcrypt libmcrypt-devel readline readline-devel libxslt libxslt-devel

#下载PHP源码包
wget https://www.php.net/distributions/php-7.0.33.tar.gz

#解压
tar --zxvf php-7.0.33.tar.gz

cd /data/php

#编译安装
./configure --prefix=/data/php --disable-fileinfo --enable-fpm --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d --with-openssl --with-zlib --with-curl --enable-ftp --with-gd --with-xmlrpc --with-jpeg-dir --with-png-dir --with-freetype-dir --enable-gd-native-ttf --enable-mbstring --with-mcrypt=/usr/local/libmcrypt --enable-zip --enable-mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-mysql-sock=/data/mysql/mysql.sock --without-pear --enable-bcmath

#php.ini在/etc目录下    --with-config-file-path=/etc

make

make install

#软连接
ln -s /data/php/bin/php /usr/bin/php
ln -s /data/php/bin/phpize /usr/local/sbin
#启动
systemctl start php-fpm.service
#停止
systemctl stop php-fpm.service
#重启
systemctl restart php-fpm.service

安装redis

#下载
wget http://download.redis.io/releases/redis-4.0.8.tar.gz
#解压
tar xzf redis-4.0.8.tar.gz
#重命名
mv redis-4.0.8 redis
#进入目录 cd redis
#安装redis
sudo make && sudo make install
#修改配置
vim redis.conf
daemonize=no改为daemonize=yes

#远程连接
#bind 127.0.0.1
protected-mode no

#建立软连接
ln -s /data/redis/redis/src/redis-cli /usr/local/sbin
ln -s /data/redis/redis/src/redis-server /usr/local/sbin

#redis设置为系统服务并且设置开机启动
vim /etc/rc.d/init.d/redis
start() {  
/data/redis/redis/src/redis-server  /data/redis/redis/redis.conf 
}  
  
stop() {  
redis-cli shutdown
}  
case "$1" in  
  start)  
 start  
 ;;  
  
stop)  
 stop  
 ;;  
  
restart)  
 stop  
 start  
 ;;  
  *)  
 echo  
$"Usage: $0 {start|stop|restart}"  
 exit 1  
esac

#添加权限
chmod a+x /etc/rc.d/init.d/redis
#启动
service redis start
#关闭
service redis stop
#重启
service redis restart

安装phpredis扩展

#在redis官网下载安装包
#解压到服务器上
#进入安装目录  执行phpize
phpize
#若报错
Cannot find autoconf. Please check your autoconf installation and the
$PHP_AUTOCONF environment variable. Then, rerun this script.
#解决办法
yum install autoconf

#指定php配置路径
./configure --with-php-config=/data/php/bin/php-config
#编译安装
sudo make && sudo make install

#将扩展加入php.ini中
find / -name php.ini
vim /php/etc/php.ini
extension=/data/php/lib/php/extensions/no-debug-non-zts-20151012/redis.so
#重启php
systemctl restart php-fpm.service

安装mongodb

#在mongodb官网下载系统版本对应的包
https://www.mongodb.com/try/download/enterprise
#解压重命名...
#进入安装目录,创建数据库文件夹、日志文件夹、配置文件夹
mkdir data
mkdir logs    ->   touch mongodb.log
mkdir etc     ->   vim mongodb.conf

#配置文件中写入
dbpath=/data/mongo/mongo/data
logpath=/data/mongo/mongo/logs/mongodb.log
port=27017
fork=true
journal=false
#启动mongo
cd /data/mongo/mongo/bin
./mongod --config /data/mongo/mongo/etc/mongodb.conf

#报错:error while loading shared libraries: libnetsnmpmibs.so.31: cannot open shared object file: No such file or directory
#解决
yum install net-snmp

#添加管理员
./mongo
use admin
db.createUser({user:"root",pwd:"123456",roles:[{role:"userAdminAnyDatabase",db:"admin"}]});

#关闭mongoDB不要使用kill   使用db.shutdownServer();
#使用权限打开mongoDB,给配置中添加auth=true

#将mongo路径软链到/usr/bin路径下,方便随处执行mongo命令
ln -s /data/mongo/mongo/bin/mongo  /usr/bin/mongo

#关闭mongod服务,执行db.shutdownServer()会出错
#解决方法
db.updateUser("root",{roles : [{"role" : "userAdminAnyDatabase","db" : "admin"},{"role" : "dbOwner","db" : "admin"},{"role" : "clusterAdmin", "db": "admin"}]})
db.shutdownServer()

#MongoDB设置为系统服务并且设置开机启动
vim /etc/rc.d/init.d/mongod
start() {  
/data/mongo/mongo/bin/mongod  --config /data/mongo/mongo/etc/mongodb.conf 
}  
  
stop() {  
/data/mongo/mongo/bin/mongod --config /data/mongo/mongo/etc/mongodb.conf --shutdown  
}  
case "$1" in  
  start)  
 start  
 ;;  
  
stop)  
 stop  
 ;;  
  
restart)  
 stop  
 start  
 ;;  
  *)  
 echo  
$"Usage: $0 {start|stop|restart}"  
 exit 1  
esac

#添加权限
chmod a+x /etc/rc.d/init.d/mongod
#启动mongo
service mongod start
#关闭mongo
service mongod stop
#重启mongodb
service mongod restart

#mongodb远程连接配置
vim /data/mongo/mongo/etc/mongodb.conf
bind_ip=0.0.0.0

#防火墙开放27017端口
iptables -A INPUT -p tcp -m state --state NEW -m tcp --dport 27017 -j ACCEPT

#重启mongodb
service mongod restart

安装phpmongodb

参照phpredis安装即可

安装ftp

yum -y install vsftpd

#开启vsftp服务
service vsftpd start

#设置为开机启动
systemctl enable vsftp

#添加ftp帐号和目录
useradd -d /data/www -s /sbin/nologin pwftp

#修改用户密码
passwd pwftp

#修改指定目录的权限
chown -R pwftp.pwftp /alidata/www/wwwroot

#修改配置文件
anonymous_enable=NO
local_enable=YES
write_enable=YES
chroot_local_user=YES
chroot_list_enable=YES
chroot_list_file=/etc/vsftpd/chroot_list

#创建chroot_list
pwftp

#修改shell配置
vim /etc/shells
/usr/sbin/nologin
/sbin/nologin

#启动服务
service vsftpd start
原文地址:https://www.cnblogs.com/ljkltt/p/14981864.html