In order to deploy and put the written java web program on Linux, you need to build the corresponding dependent software (environment) and install some necessary software programs
- JDK
- Tomcat
- MySqL
jdk is installed directly using the package manager (based on yum installation)
1. yum
1. Know yum
yum (Yellow dog Updater, Modified) is a very commonly used package manager under Linux. It is mainly used in distributions such as Fedora, RedHat, and Centos. The package manager is like an "app store", and we can download it from the app store some apps
The function of yum is similar to the dependency management function of Maven. Using Maven can help us install some third-party jar packages conveniently, and yum is convenient for us to install third-party programs. Similarly, Github can also serve as a "software warehouse
" Effect, and indeed some programming language package management tools are based on Github (such as Go language). However, Github can not only be used to distribute programs, but also can manage source code and carry out collaborative development, while both yum and maven are only used to distribute programs
View the list of packages:
yum list | grep [软件包关键字]
The yum list itself lists all the packages that can be installed, there are too many, use grep to filter the results containing the jdk keyword, otherwise the list will be too much, causing the machine to be very stuck
Pipeline: Pipeline is an ancient way of "interprocess communication", which can be used in Linux instructions |
as a pipe symbol. meaning isUse the content of the standard output of the previous command as the standard input content of the second command
Install the package (requires administrator privileges):
yum install [软件包名字]
Uninstall the package (requires admin privileges):
yum remove [软件包名字]
Precautions:
- All yum commands must ensure that the network is connected before they can be used (cloud servers generally have a good network)
- yum install / yum remove must have administrator privileges (root user)
- You can use ping www.baidu.com to check the smoothness of the network
2、git
1). View the git installation package
# 由于带 git 关键字的软件包很多, 可以在 grep 的时候加上 -w , 表示全字匹配.
yum list | grep git -w
2). Install git
yum install git.x86_64
3). The basic use of git (it is consistent with the Windows version of git. Just use the command line operation)
git clone
git add
git commit
git push
JDK
yum install java-1.8.0-openjdk-devel.x86_64
——Use yum to filter: devel: development. 64-bit system version. .i686 is 32-bit
——Copy package name, yum install package name. Whether to install, enter y
——Use javac
, or java -version
to verify whether the installation is successful, if it prompts "java command not found", it means that the installation failed
Note: The JDK on yum is OpenJDK, which is an open source version of JDK. It is slightly different from Oracle's official JDK. Here we can use OpenJDK. Installing Oracle JDK is troublesome
Tomcat
1. Installation
Since the default version of Tomcat 7 on the yum source is relatively old, Tomcat 8 needs to be installed manually, and yum cannot be used
noarch means not picking the system (based on java cross-platform)
1). Download the Tomcat compressed package
Installation and use of Tomcat and maven
2). Upload the compressed package to Linux
Drag and drop .zip directly into xshell. If it fails, you need yum install lrzsz
to drag and upload. In fact, you use the rz command on Linux. Some systems come with this command, and some don’t. You need to install it manually.
2). Decompress Tomcat
# 使用 unzip 命令解压缩
unzip apache-tomcat-8.5.47.zip
If the unzip command cannot be found, then yum install unzip
first
2. Modify executable permissions
Enter the bin directory, use chmod +x *.sh
, and .sh
grant executable permissions to all files with the suffix
chmod +x *.sh
3. Start Tomcat
sh startup.sh
The directory structure of Tomcat
The directory structure of Tomcat in Linux is exactly the same as that of Windows. (It is decompressed from the same zip package)
4. Verify that the startup is successful
Method 1: Check whether the tomcat process exists
ps aux | grep tomcat
Method 2: Check whether port 8080 is bound
netstat -anp | grep 8080
Method 3: Use the curl command to access the default demo
curl 127.0.0.1:8080
If the process exists or the port status is correct (LISTEN status) or the default home page can be accessed, the startup is successful
External network access tomcat default demo
in the browser address bar
http://[服务器外网ip]:8080/
4.1. Firewall
If this website cannot be accessed, the reason:
- The server did not start normally - netstat verification
- The firewall/security group of the server is not properly opened. The
cloud server manufacturer introduces a security mechanism for the safety of the machine. By default, only port 22 (ssh) of your machine can be accessed externally. If you want to allow other ports to be accessed by the outside access, you need to release it manually
Before accessing this page from the external network, you need to enable the "Security Group" function of the server.
Log in to your own cloud service account, find the console on the home page -> your own server -> security group,
you need to configure the security group to allow external hosts to access the 8080 of the server port
If you cannot find the security group configuration interface, you can consult the cloud server customer service at any time
Lightweight Application Server: Firewall. ECS server: security group
You can see the default page:
5. Modify the port number
If the startup is unsuccessful, cd logs/
check the log information in the logs directory, use vim to see the contents of these logs, and start from the large files
Example: The log shows that 8005 is occupied, but Tomcat needs 8080 and 8005 to start
So let's check who occupies 8005netstat -anp | grep 8005
tcp6 0 0 127.0.0.1:8005 :::* LISTEN 1700/java
This message shows that 8005 is occupied by a process named java with id 1700
Use ps -aux | grep 1700
to view specific information
Solution:
- Kill 1700
- Change the 8005 port number to something else (recommended), the modification method is as follows:
1). Switch to the conf directory, where the configuration file is placed
2). grep 8005
, see where the port number is written,
3). vim server.xml
, modify it to an unused port number
MySQL
1. Installation
MySQL can also be installed using yum. However, some configurations need to be modified for reference: CentOS 7 install MariaDB via yum
Note: In order to support Chinese when creating a database, the utf8mb4 character set is uniformly used
MariaDB [(none)]> create database demo_db charset utf8mb4;
MariaDB on yum is not the same as pure MySQL.
The relationship between MariaDB and MySQL is similar to the relationship between CentOS and RedHat. From the perspective of use, MariaDB and MySQL are compatible
2. Use
Modify the MySQL login password:
mysql_secure_installation
Enter, enter the new password, enter the new password again, four times y to confirm the password change
Log in to MySQL using the default port 3306 from the command line:
mysql -uroot -p
3. Verification
Use netstat -anp
the command to display all network information, use grep to filter out mysql related content,
mysql server; d => daemon, meaning daemon (background process); port number: 3306
Exit MySQL: Ctrl + d
If there is a problem during the operation of the database, you can view the error log of MySQL.
Through this command in MySQL, get the path to the log
mysql> show variables like 'log_error';
# 输出结果
+---------------+------------------------------+
| Variable_name | Value |
+---------------+------------------------------+
| log_error | /var/log/mariadb/mariadb.log |
+---------------+------------------------------+
Use vim or less to view the contents of the file
less /var/log/mariadb/mariadb.log
In addition to error logs, there are many other types of logs in MySQL. For details, please refer to
https://www.cnblogs.com/f-ck-need-u/p/9001061.html
4. Modify the encoding method
View the current encoding method:
show variables like 'character%';
Enter the etc directory to modify the my.cnf file (my.ini in win, my.cnf in Linux)
cd /etc
ll
vim my.cnf
Add under [client]:
[client]
default-character-set=utf8
Add at the bottom of [mysqld]:
default-storage-engine=INNODB
character-set-server=utf8
collation-server=utf8_general_ci
Restart the MySQL service:
service mysqld restart
Deploy the web project to Linux
deploy
The "environment" involved in the work
- Development environment: the machine used by programmers to write code.
- Test environment: the machine used by the tester to test the program. Generally, it is a decommissioned deployment machine.
- Production environment (online environment): the machine used when the final project is released. It has high requirements for stability
Copy and install the program to the production environment, this process is called "deployment", also called "online".
This process needs to copy the corresponding .class files and dependent resource files (various configuration files, html, etc.)
Once the program is successfully deployed, Then this program can be accessed by tens of thousands of ordinary users on the external network
. In other words, if the program has a bug, the bug will be seen by tens of thousands of users
The deployment process is very important and belongs to the most important part of program development. Once there is a problem with the deployment, it is very likely to cause serious accidents (server unavailability and the like). In order to prevent deployment errors, there are generally some automated deployment tools within the company
. (such as Jenkins, etc.), currently we use manual deployment to complete the deployment
1. Create a database table
In the mysql of the cloud server, build a database and a table
Enter the password to connect to the database, create the database, and copy the code of the project db.sql to the database of the cloud server
create databases java_blog charset utf8mb4; --记得字符集
use java_blog;
2. Fine-tune the code
The local database, port number, user name, password and cloud server database may be different
The local database, you set the password when you installed it
The database of the cloud server has no password by default. If the password has not been changed, set it to ""
3. Packing
Switch to the main directory of the project (the directory containing pom.xml), and use maven to package it into a war package
-
<!-- 设置包的类型,包的名字 --> <packaging>war</packaging> <build> <finalName>blog_system</finalName> </build>
-
Double click on the package
At this time, the corresponding war package will be generated in the target directory
We used to use maven mainly through graphical tools in IDEA. In fact, maven is essentially a command-line tool. The
maven operations that were performed on IDEA can also be completed through the command line in Linux.
4. Copy to Tomcat
Copy the war package to the webapps directory of the cloud server Tomcat
Note: If the project has been deployed before, you need to delete the previous package first
5. Verification
Visit the project's link on your browser. (Verified as per the project's functionality).
- Verify AccountRegistrationLogin
- Verify Display Blog List
- Verify new blog
- Verification Display Blog Content
Note: It can run correctly locally, but it may not run correctly when deployed on a cloud server!
The correctness of program execution depends not only on the code, but also on the dependent data, configuration, and other environmental information
Visit: http://ip:8080/blog_system/blog_login.html