Linux server build project operating environment

1 server configuration

1.1 Reset server password

  Buy a server (take Aliyun as an example), after the purchase is completed, a service will be generated in the server list, click on the server to enter the server management (different Linux versions may have different interfaces), the default server initialization password is empty, so you need to set it first A server password for easy remote connection.

1.2 Set up server firewall

Add rules to add allowed ports (centOS adds rules in the firewall), you can view the ports developed by the server through commands

netstat -ano | grep 8000

1.3 Connect to the server

After the setup is complete, download the tool finalShell or xShell

Use finalShell to remotely connect to the server, create a new connection, fill in the server’s external network ip for the host, and then enter the root and server password in the next step to successfully connect to the server

1.4 Test server network speed

Install command:

yum install python-pip –y
pip install speedtest-cli

Run the command:

speedtest-cli --bytes

2 Install JDK1.8

2.1 Configure environment variables

Execute the following yum command to install

1. First update the YUM source (it is slow and can be ignored)

yum -y update 

2. List JDK1.8 version

yum list Java* 

3. Install JDK1.8

yum install java-1.8.0-openjdk* -y

2.2 Verify whether the installation is successful ( if the java version appears, it means that the java environment deployment is successful )

java -version

3 Download and configure Tomcat

3.1 Configuration directory

1. First enter the usr directory:

cd /usr

2. Create a new folder:

mkdir project

3. Enter the new folder:

cd project/
mkdir tomcat
cd tomcat/

3.2 Download Tomcat

The version number can be found on the official website

1. Download and install Tomcat

#下载tomcat,如果网址失效则网上查找安装包,用Xftp传输到/usr/java再解压

wget https://mirrors.cnnic.cn/apache/tomcat/tomcat-8/v8.5.39/bin/apache-tomcat-8.0.53.tar.gz

或者使用: 

wget https://archive.apache.org/dist/tomcat/tomcat-8/v8.5.39/bin/apache-tomcat-8.5.39.tar.gz

wget https://archive.apache.org/dist/tomcat/tomcat-7/v7.0.107/bin/apache-tomcat-7.0.107.tar.gz

2. Unzip the downloaded compressed package

tar -xvf apache-tomcat-8.5.39.tar.gz

3. Rename the file

mv apache-tomcat-8.5.39 tomcat8

3.3 Running Tomcat

1. Switch to the bin directory of Tomcat

cd tomcat8/bin

2. Execute the following command to start Tomcat

./startup.sh

3. If there is no error message when starting, it means that Tomcat started successfully.

 4. Check the process by command

netstat -nltp

 5. Switch to the logs directory of Tomcat, and execute the command to view the running log of Tomcat

cd /usr/project/tomcat/tomcat8/logs/
tail -f catalina.out

6. Enter http://public network ip:8080 in the browser to access

3.4 Uninstall Tomcat

1. View the Tomcat process through the command

netstat -nltp

2. End the Tomcat process by command: kill -9 21991 (process number)

kill -9 21991

3. Enter the Tomcat directory and delete the file

cd /usr/project/tomcat
rm -rf 文件名

4 Install MySQL

4.1 Inspection before installation

1. First check whether MySQL is installed

yum list installed | grep mysql

2. If installed, uninstall first: yum -y remove the files found above

yum -y remove 文件名

3. Check MySQL installed in other ways

rpm -qa | grep mysql

4. If there is, uninstall it by rpm -e command or rpm -e --nodeps command

rpm -e 检查出的名称    #普通删除模式
rpm -e --nodeps 检查出的名称  #强力删除模式,如果使用上面命令删除时,提示有依赖的其它文件,则用该命令可以对其进行强力删除

5. Query and delete MySQL related directories through commands

find / -name mysql

 6. Delete the relevant directory through rm -rf file

 7. Check again, nothing shows that the uninstallation is successful

rpm -qa | grep -i mysql

4.2 Install MySQL

1. MySQL depends on libaio, so install libaio first

yum install libaio

2. Download the MySQL repo source

wget http://repo.mysql.com/mysql57-community-release-el7-7.noarch.rpm

// 选择第二种方式
wget http://dev.mysql.com/get/mysql57-community-release-el7-8.noarch.rpm

3. Install the mysql57-community-release-el7-8.noarch.rpm package

rpm -ivh mysql57-community-release-el7-7.noarch.rpm  --nodeps --force

// 上一步选择第二种方式,使用下面命令
yum localinstall mysql57-community-release-el7-8.noarch.rpm

4. After installing this package, you will get two MySQL yum repo sources, enter the yum.repos.d directory

cd /etc/yum.repos.d

5. View the files in the current directory

ll

6. Install MySQL

yum install -y mysql-server

// 第二种方式使用下面命令
yum install mysql-community-server

Note: If the following error will be reported after installation (Mysql failed: GPG key is installed, but it is not applicable to this software package), you can refer to the modification ( documentation )

Solution: GPG verification fails, because the public key corresponding to the software package configured on this machine is incorrect, and the signature verification fails. I searched for the keyword GPG on the mysql official website and found a solution. The general idea is that if you use a version of rpm above 4.1, in addition to importing the public key of mysql to the configuration of the individual user, you also need to import the public key of mysql to the configuration of the RPM , and then reinstall.

rpm --import https://repo.mysql.com/RPM-GPG-KEY-mysql-2022

7. Add character-set-server=utf8 to [mysqld] in the MySQL configuration file /etc/my.cnf

vim /etc/my.cnf
character-set-server=utf8

(If prompted: bash: vim: command not found, use: yum -y install vim* to install)

8. Start the MySQL service and check whether the MySQL service is started

systemctl start mysqld

9. View the running status of MySQL

service mysqld status

4.3 Set MySQL password

1. Reset root password

MySQL5.7 will generate a random password for the root user after installation, instead of the empty password like previous versions. You can change the root login password in safe mode or log in with a random password to change the password.

Use the command to view the random password generated by MySQL for the root user

grep 'temporary password' /var/log/mysqld.log

2. Log in to MySQL through username and password, username: root, password: the random password you just queried

mysql -u root -p

Enter password: (输入刚才查询到的随机密码)

3. Modify the password of the root user: (MySQL's password policy is relatively complicated, and passwords that are too simple will be rejected). First modify the security policy to avoid rejection of too simple passwords

set global validate_password_policy=0;
set global validate_password_length=4;

4. Change the MySQL password

alter user 'root'@'localhost' identified by 'Liyh123';

5. After the modification is complete, refresh the MySQL system permissions, save and exit

flush privileges;
quit;

6. Log in with the new password you just set

mysql -u root -p

use mysql;

#如果进行操作出现下面的提示:

#You must reset your password using ALTER USER statement before executing this statement. 

#就再设置一遍密码:

set password = password('Liyh123');

4.4 Configuring MySQL

1. Open port 3306, allow user name: root, password: Liyh123 to connect to MySQL server from any host, refresh MySQL system permissions, save and exit

GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'Liyh123' WITH GRANT OPTION;
flush privileges;
quit;

2. Install the mysql jdbc driver

yum install -y mysql-connector-java

3. At this point, MySQL is installed and configured. Finally, log in and check whether the database encoding has been changed to UTF-8

mysql -u root -p
show variables like 'character%';

4. How to start/stop/restart MySQL? 
 
1. Start command
1、使用 service 启动:service mysqld start 
 
2、使用 mysqld 脚本启动:/etc/inint.d/mysqld start 
 
3、使用 safe_mysqld 启动:safe_mysqld& 
2. Stop command
1、使用 service 启动:service mysqld stop 
 
2、使用 mysqld 脚本启动:/etc/inint.d/mysqld stop 
 
3、mysqladmin shutdown 
3. Restart command
1、使用 service 启动:service mysqld restart 
 
2、使用 mysqld 脚本启动:/etc/inint.d/mysqld restart 

5. Use the command (netstat -nltp) to check whether MySQL is running

4.5 Use Navicat to manage database

After downloading and installing the tool Navicat for MySQL, create a new connection and connect to the server database

Baidu Netdisk - extraction code: 4mb8

4.6 Use Navicat to upload the local database to the server

Select Data Transfer from the toolbar

5 Install Redis

1. First enter the usr directory:

cd /usr  

2. Create a new folder:

mkdir project

3. Enter the new folder:

cd project/

mkdir redis

cd redis/

4. Get redis resources

wget http://download.redis.io/releases/redis-4.0.8.tar.gz

5. Unzip the downloaded compressed package

tar xzvf redis-4.0.8.tar.gz

6. Install, enter the command make to execute the compilation command

cd redis-4.0.8

make

cd src

make install PREFIX=/usr/project/redis

( If you use the make command to report an error, it is because Redis is implemented in C and requires gcc to compile, so the reason is that gcc is not installed in the system, and execute in order because gcc depends a lot, so execute it just in case )

yum install cpp

yum install binutils

yum install glibc

yum install glibc-kernheaders

yum install glibc-common

yum install glibc-devel

yum install gcc

yum install make

7. Move the configuration file to the installation directory

cd ../

mkdir /usr/project/redis/etc

mv redis.conf /usr/project/redis/etc

8. Configure redis to start in the background, change daemonize no to daemonize yes

vi /usr/project/redis/etc/redis.conf 

9. Add redis to boot, add /usr/project/redis/bin/redis-server /usr/project/redis/etc/redis.conf (meaning this command to start redis)

vi /etc/rc.local
/usr/project/redis/bin/redis-server
/usr/project/redis/etc/redis.conf

10. Open redis

/usr/project/redis/bin/redis-server /usr/project/redis/etc/redis.conf

11. Copy redis-server and redis-cli to bin, so that redis-cli commands can be used directly in any directory

cp /usr/project/redis/bin/redis-server  /usr/local/bin/ 
cp /usr/project/redis/bin/redis-cli  /usr/local/bin/

12. Set redis password (you can not set it)

redis-cli

View existing redis passwords (optional operation, you can have none)

config get requirepass

Set the redis password, run the command: config set requirepass **** (**** is the password you want to set), if the setting is successful, it will return the word 'OK', exit through exit

config set requirepass 123456

The following two commands can restart the redis service ( 123456 is the password you set)

redis-cli -h 127.0.0.1 -p 6379 -a 123456
redis-cli
auth 123456

13. Allow the external network (server ip) to access redis, ( firewall related commands )

Turn on the firewall and view the commands of the firewall

systemctl start firewalld

firewall-cmd --state

Configure firewall:   firewall-cmd --zone=public --add-port=6379/tcp --permanent (open port 6379 )

firewall-cmd --zone=public --add-port=6379/tcp --permanent

Restart the firewall for the configuration to take effect immediately

systemctl restart firewalld

View all open ports on the system

firewall-cmd --zone=public --list-ports

At this time, although the firewall has opened port 6379, the external network is still inaccessible, because redis listens to 127.0.0.1:6379 and does not listen to requests from the external network.

(1) Add # in front of bind 127.0.0.1 in the redis.conf configuration file in the folder directory to comment out

vi /usr/project/redis/etc/redis.conf

(2) Command: After redis-cli is connected to redis, check whether the protected-mode is no through config get, if not, use config set to change the configuration name attribute to no.

config get protected-mode
config set protected-mode no

(3) Access through the Linux server ip

redis-cli -h 66.66.66.66 -p 6379 -a 123456

14. Redis common commands  

redis-server /usr/project/redis/etc/redis.conf  //启动redis

pkill redis   //停止redis

rm -rf /usr/local/redis   //删除安装目录

6 Install Nginx

6.1 Install Nginx

1. Use yum to install nginx (the installation directory is in /etc/nignx)

yum install nginx

( If you use yum install nginx to prompt that it cannot be installed , use the command: yum list | grep nginx to view the available installation packages. From the query results, you can see that nginx is not in the centos official yum source list.

First install epel: sudo yum install epel-release, then update: yum -y update, and finally reinstall: yum install nginx)

2. Start the nginx service

sudo systemctl start nginx

3. Restart the nginx service

sudo service nginx restart

4. Stop the nginx service

sudo systemctl stop nginx

5. Check nginx status

sudo systemctl status nginx

6. nginx configuration

# For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 4096;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;

    server {
        listen       80;
        listen       [::]:80;
        server_name  _;
        root         /usr/share/nginx/html;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        error_page 404 /404.html;
        location = /404.html {
        }

        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
        }
    }

# Settings for a TLS enabled server.
#
#    server {
#        listen       443 ssl http2;
#        listen       [::]:443 ssl http2;
#        server_name  _;
#        root         /usr/share/nginx/html;
#
#        ssl_certificate "/etc/pki/nginx/server.crt";
#        ssl_certificate_key "/etc/pki/nginx/private/server.key";
#        ssl_session_cache shared:SSL:1m;
#        ssl_session_timeout  10m;
#        ssl_ciphers HIGH:!aNULL:!MD5;
#        ssl_prefer_server_ciphers on;
#
#        # Load configuration files for the default server block.
#        include /etc/nginx/default.d/*.conf;
#
#        error_page 404 /404.html;
#            location = /40x.html {
#        }
#
#        error_page 500 502 503 504 /50x.html;
#            location = /50x.html {
#        }
#    }

}

6.2 Uninstall Nginx

1. First, enter the command ps -ef | grep nginx to check whether the nginx service is running.

ps -ef | grep nginx

2. Stop the nginx service (both commands are fine)

/usr/sbin/nginx -s stop
sudo systemctl stop nginx

3. Find and delete nginx related files

whereis nginx
find / -name nginx

Delete all the directories found by find in turn: rm -rf /usr/sbin/nginx

4. Then use yum to clean up

yum remove nginx

6.3 View Nginx logs

cat /var/log/nginx/error.log
cat /var/log/nginx/access.log

Guess you like

Origin blog.csdn.net/liyh722/article/details/123378725